![]() Powershell Basics Introduction to Windows PowerShell Part 1 Introduction to Windows PowerShell Part 2 Introduction to Windows PowerShell Part 3 PowerShell Script Signing PowerShell Scheduled Tasks PowerShell Networking Tasks |
Powershell Advanced Tutorials PowerShell and VMware Introduction |
In part one of this series we took a quick look at some of the basic commands and features available within PowerShell. In part 2 we are going to try to cover basic syntax (i.e. what is the escape character?), and also look at the different selection (if... then) and iteration (while, for, etc..) constructs available to the PowerShell programmer.
Variables[int]$a = 10A Partial Listing of PowerShell Automatic Variables
$$ | Last token of the previous command line |
$? | Boolean status of last command |
$^ | First token of the previous command line |
$_ | Current pipeline object |
$Args | Arguments to a script or function |
$Error | Array of errors from previous commands |
$Foreach | Reference to the enumerator in a foreach loop |
$Home | The user’s home directory; usually set to %HOMEDRIVE%\%HOMEPATH% |
$Host | Reference to the application hosting the POWERSHELL language |
$Input | Enumerator of objects piped to a script |
$LastExitCode | Exit code of last program or script |
$Matches | Hash table of matches found with the –match operator |
$PSHome | The installation location of Windows PowerShell |
$profile | The standard profile (may not be present) |
$StackTrace | Last exception caught by Windows PowerShell |
$Switch | Enumerator in a switch statement |
PS C:\Documents and Settings\D> Get-Variable -scope global Name Value ---- ----- Error {MissingArgument,Microsoft.Powe... DebugPreference SilentlyContinue... PROFILE C:\Documents and Settings\D\My... ...Concatenation
PS c:\scripts\> $a=1 PS c:\scripts\> $b=2 PS c:\scripts\> $c=$a+$b PS c:\scripts\> $c 3 PS c:\scripts\> [string]$a="1" PS c:\scripts\> $c=$a+$b PS c:\scripts\> $c 12Strings and Quotes
PS C:\scripts> $string = "I am a string!"
PS C:\scripts> $string
I am a string!
PS C:\scripts> $string = 'I am a string!'
PS C:\scripts> $string
I am a string!
PS C:\scripts> $string = "I "am" a string!"
Unexpected token 'am a string!' in expression or statement.
At line:1 char:28
+ $string = "I "am" a string!" <<<<
PS C:\scripts> $string = "I `"am`" a string!"
PS C:\scripts> $string
I "am" a string!
PS C:\scripts> $string = 'I "am" a string!'
PS C:\scripts> $string
I "am" a string!
Likewise strings containing variables should be double quotedPS C:\scripts> "What are you? $string" What are you? I "am" a string!Operators
PS C:\scripts> $myarray = ("a","b","c",1,2,3) PS C:\scripts> $myarray a b c 1 2 3 PS C:\scripts> $myarray[2] c PS C:\scripts> $myarray[2] = 10 PS C:\scripts> $myarray[2] 10 PS C:\scripts> $myarray a b 10 1 2 3 PS C:\scripts> $myarray.length 6 PS C:\scripts> $myarray += ,"newone" PS C:\scripts> $myarray.length 7Hashtables
PS C:\scripts> $myhash =@{ Ford = "Mustang"; Dodge = "Charger"; Chevy = "Camero" } PS C:\scripts> $myhash Name Value ---- ----- Dodge Charger Ford Mustang Chevy CameroAccessing the data can be done with both property and array notation, which is nice.
PS C:\scripts> $myhash["Chevy"] Camero PS C:\scripts> $myhash.Chevy Camero PS C:\scripts> $myhash.keys Dodge Ford Chevy PS C:\scripts> $myhash.remove("Dodge") PS C:\scripts> $myhash Name Value ---- ----- Ford Mustang Chevy CameroSelection
PS C:\scripts> $x=0; if ($x = 0) {"looks like x equals 0"} ` >> else {"looks like x equals 1"} >> looks like x equals 1And here is a switch statement:
PS C:\scripts> switch ("abc") { "abc" {"abc matches"} "def" {"def does not"}} abc matchesIteration
PS C:\scripts> $x=0 PS C:\scripts> while($x -le 3)` >> {"x is still less than or equal to 3";$x++} >> x is still less than or equal to 3 x is still less than or equal to 3 x is still less than or equal to 3 x is still less than or equal to 3 PS C:\scripts> do {$x;$x++} while ($x -le 3) 1 2 3 PS C:\scripts> for ($i=0; $i -lt 3; $i++) {$i} 0 1 2