Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

PowerShell basics part1

Avatar for Margarita Margarita
November 30, 2016

PowerShell basics part1

PowerShell basics part1

Avatar for Margarita

Margarita

November 30, 2016
Tweet

More Decks by Margarita

Other Decks in Programming

Transcript

  1. Commandlets Special commands (known as cmdlets) are typed in after

    the prompt, and are executed when you press the Enter key or execute your PowerShell script.
  2. Variables $myVar = 2016 [string] $myString = "any string“ $myString

    = $myString + " was changed" $myString = "Create $($myString)" [int[]] $myArray = 12,64,8,64,12 foreach($num in $myArray){Write-Host $num} $myArray = @(1,"Hello",3.5,"World")
  3. Print in the Host • Write-Host; • Write-Debug; ($DebugPreference =

    "SilentlyContinue" | "Continue" ) • Write-Error; • Write-Warning;
  4. Execution Policy • Before you can write and run scripts,

    you should be aware that Windows is not configured to allow the execution of unsigned scripts because they can be used to damage the system. • To configure your execution policy so that you can run scripts you have written, you can use the command set-executionpolicy. • To execute your command without warning or restriction use -Force
  5. PipeLine A pipeline occurs when one takes the output of

    one command and directs it to the input of another command.
  6. Scripts and Modules • Scripts are text files that contain

    sequences of calls to cmdlets, and these files have the extension .ps1. • Unlike working in the console, each operation does not have to be typed in and executed immediately. • A script module is essentially any valid PowerShell script saved in a .psm1 extension. • You can use the module cmdlets, such as Import-Module MyModule
  7. Comments • In PowerShell single line comments start with a

    hash symbol, everything to the right of the # will be ignored. • In PowerShell 2.0 and above multi-line block comments can be used: <# comment #>
  8. Parameters • You’ll often write a script or function that

    needs to accept some kind of input. • You can tell Windows PowerShell to expect these parameters, collect them from the command line, and put their values into variables within your script or function. • Full-fledged syntax lets you define parameters as mandatory, specify a position and more. ([Parameter(Mandatory=$True)])