Overview
Recently, I was assigned a project to be implemented in PowerShell, the latest shell and scripting language from Microsoft. Having no experience at all with PowerShell, I dove into the voluminous documentation to learn what I needed to about the task at hand.
Here is a hopefully concise summary of the PowerShell basics, the minimum one needs to know in order to effectively write PowerShell script.
Online reference
Microsoft’s scripting reference for PowerShell is very thorough, although it oddly lacks a home page. To find it, go to this particular page and look at the table of contents on the left. Here is an overview of what we’ll be covering in this crash course:
Case
Comments
Operators
Variables
Data types
Strings
Arrays
Objects
Scopes
Control flow
if
do while
while
ForEach
try/catch/finally
Functions
Parameterized scripts
cmdlets
Piping
Case
The scripting language is case-insensitive in terms of both language keywords and data comparisons. For example:
$foo = 'foo' $foo -eq 'foo' True $foo -eq 'FOO' True $foo -EQ 'FOO' True
Comments
# is the single-line comment character.
<# … #> is the multi-line comment syntax.
Operators
Basic operators are:
Arithmetic: +, -, *, /, %
Assignment: =, +=, -=, *=, /=, %=
Comparison: -eq, -ne, -gt, -lt, -le, -ge
Logical: -and, -or, -xor, -not, !
Redirection: >, >>, 2>, 2>, and 2>&1
String: -split, -join
Unary: ++, --
Execution: &
Learn more about operators.
Variables
For example:
$foo = 'bar'
The leading $ is required. Stick with letters, numbers and underscores for the remainder of each variable name, even though more exotic characters are allowed.
Learn more about variables.
Data types
Many exist, but these are the basics:
[Array]
[Bool] ($true and $false are the constants)
[DateTime]
[Guid]
[HashTable]
[Int]
[Regex]
[Float]
[String]
Learn more about data types.
Strings
Double-quoted strings (") will evaluate inner variables. For example:
$foo = 'bar' "hello, $foo" hello, bar
Single-quoted strings (') will not evaluate inner variables. For example:
$foo = 'bar' 'hello, $foo' hello, $foo
Arrays
For example:
$foo = 'a','b','c' $foo[0] 'a' $foo[1] 'b' $foo[2] 'c' $foo[-1] 'c' $foo[-2] 'b' $foo[3] (nothing)
(Get-Service)[0] Status Name DisplayName ------ ---- ----------- Stopped Adobe LM Service Adobe LM Service (Get-Service)[0].name Adobe LM Service
Learn more about objects.
Scopes
Tips:
PowerShell maintains a hierarchy of scopes
The shell itself is the top of the hierarchy, the global scope
Each script runs in its own scope, which is discarded after it runs
Learn more about scopes.
Control flow
if
Example:
$foo = 2 if ( $foo -eq 0 ) { 'zero' } elseif ( $foo -eq 1 ) { 'one' } else { 'other' } other
$foo = 2 do { $foo $foo = $foo - 1 } while ( $foo -ne 0 ) 2 1
$foo = 2 while ( $foo -ne 0 ) { $foo $foo = $foo - 1 } 2 1
$services = Get-Service ForEach ($service in $services) { $service.name } Adobe LM Service AdobeARMservice AdobeFlashPlayerUpdateSvc AJRouter ALG AppIDSvc Appinfo ...
try { 'trying...' nonsense } catch { 'caught' } finally { 'finalizing' } trying... caught finalizing
function foo ( $a, $b ) { "a: $a, b: $b" } foo 1 2 a: 1, b: 2 foo 'one' 'two' a: one, b: two
Important: passed arguments are delimited by spaces, not commas! For example:
foo 1, 2 a: 1 2, b: foo 'one', 'two' a: one two, b:
Compare the output between those two examples closely.
Learn more about functions.
Parameterized scripts
If the file test.ps1 contains this:
param ( [int]$min, [int]$max ) $min..$max | ForEach-Object -process { $_ }
then:
./test 5 10 5 6 7 8 9 10
Learn more about script parameters.
Cmdlets
A cmdlet is a logically encapsulated piece of functionality. Many of them are provided by Microsoft as part of the PowerShell framework. See here for one example and look at the table of contents on the left for many more.
You can also write cmdlets of your own.
Piping
The pipeline is a core concept in the PowerShell environment. For example:
1..5 | ForEach-Object -process { $_ # the current item } 1 2 3 4 5
In this example, we're piping an array (1..5) into the ForEach-Object cmdlet.
Learn more about piping.