What's [With] {All} (the) Punctuation in PowerShell?

One of the common complaints I'm hearing these days is about the complexity of PowerShell's punctuation scheme. Ignoring the help files for a moment (they have their own syntax rules), I can see exactly where folks are coming from. The profusion of picky punctuation can be prickly indeed. So here's a primer.

Don Jones

June 9, 2010

2 Min Read
ITPro Today logo in a gray background | ITPro Today

One of the common complaints I'm hearing these days is about the complexity of PowerShell's punctuation scheme. Ignoring the help files for a moment (they have their own syntax rules), I can see exactly where folks are coming from. The profusion of picky punctuation can be prickly indeed. So here's a primer.



[BRACKETS]

Square brackets are used for two things. First, to set off object types. For example, when you define a variable, you can specify that it be of a certain type:


[string]$var = "Howdy"


Other valid types include [int], [boolean], [float], [xml], and many more; you can also include most valid .NET Framework types.


Second, square brackets are used to access specific objects in a collection through their ordinal number. Try running these commands:


$services = Get-Service

$services[0]

$services[1].name

$services[-1].displayname



{BRACES}

Curly braces (someone in class last week called them "pigtails," which is adorable) are used to set off blocks of code, which are - oddly enough - called "script blocks" or "scriptblocks." You'll see these used as part of a scripting construct:


if ($something -eq $else) {

 # this is a script block

}


But you can also see them as part of a cmdlet:


Invoke-Command -scriptblock { Dir } -computername server-r2


And you even use them as part of filtering:


Get-Service | Where { $_.Status -eq "Running" }



(PARENTHESES)

These are used the same way you did in Algebra class: To group expressions' priority or precedence. Here's a simple math example:


(5 + 5) * 2


Which results in 20; without the parentheses:


5 + 5 * 2


The result would be 15 (5*2 is done first, then 5+10). So very different results with the parentheses. They can also be used to group logical expressions:


($this -eq $that) -or ($those -gt $them)


Finally, they can also be used to execute "sub-commands" or "sub-pipelines," returning the results of an entire command or pipeline, and enabling that result to be passed to a parameter:


Get-WmiObject Win32_BIOS -computername (get-content names.txt)



.PERIOD.

A single period can be used in two ways. First, it signals that you want to access a property or method of an object:


$services[0].name

$services[1].start()


When by itself at the beginning of a command line, it indicates a "dot sourcing," meaning you are loading another script into the current scope:


. c:scriptslibrary.ps1


Two periods together is the range operator, which produces a set of sequential objects in the range specified:


1..10 | ForEach { Write $_ }



Hopefully, these can serve as a useful "cheat sheet" for your future PowerShell work!


Want more PowerShell articles, along with FAQs and more? Visit http://windowsitpro.com/go/DonJonesPowerShell.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like