Introduction to Microsoft PowerShell’s Syntax
This section is dedicated to explaining PowerShell’s individual syntactic elements. I also regard these topics as reference material for that time when you forget how to choose the correct type of bracket, or when a single quote does not get the job done.
Microsoft PowerShell Syntax Topics
Pure Syntax
- Introduction Syntax
- Backtick `
- Brackets () {} and []
- Comparison Operators: -eq, -gt
- Conditional Operators: -Like -Match – contains
- … -AS Type Operator
- Format-Table (FT)
- Format-List (FL)
- -f Format Operator
- Quotes ‘Single’ or "Double"?
- Pipeline (|)or (¦)s
Pipeline $_.property - Parameters (or -switches) – Guy’s Top Ten
- Variables
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
The Significance of PowerShell's Brackets
Parenthesis Brackets ()
Curved () parenthesis stype brackets are used for compulsory arguments. Take for example a ForEach loop the first section is like Foreach ($Item in $Items). Microsoft call this 'control structure'.
Braces Brackets {}
These curly brackets are typically employed in block statements; for example:
| Where {$_.name -Like "Network"}
Square Brackets []
This is probably the least important, and least used brackets, that's because they define optional items, for example:
Get-Process [W]*
See more on PowerShell's brackets »
PowerShell Conditional Operators
PowerShell has three main conditional operators, -Match, -Like and -Contains.
# PowerShell Syntax example to demonstrate -Match
# Author: Guy Thomas
# May 2010 tested on PowerShell v 1.0 and 2.0
$WMI = Get-WmiObject -List | Where-Object {$_.name -Match "network"}
$WMI | Format-Table name, Properties -auto
Write-Host $WMI.count "WMI objects contain the word network"
Note 1: I make -Match my default when looking for an item, or wishing to filter a long list.
See more on PowerShell’s Conditional Operators »
Example of PowerShell’s Comparison Operators
At first glance, one of the strangest comparison operator in PowerShell is -eq. While the equals sign ‘=’ is still needed for declaring variables, in most other cases you need -eq, or for PowerShell not equal -ne. Once you warm to this theme, then -gt -lt seem a logical continuation. Consequently, abandon > and <, instead employ -gt (greater than) or -lt (less than).
# PowerShell script to list .exe under the Windows folder
$Dir = Get-Childitem C:\Windows\ -recurse
$List = $Dir | Where-Object {$_.extension -eq ".exe"}
$List | Format-Table Name, CreationTime -auto
See more examples of comparison operators »
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
More PowerShell Syntax
Contents of My PowerShell Cmdlet Pages
Each topic has its own ‘how to’ instructions and also ‘Learning points’. As a result you will be able to modify my examples to suit your situation. The only pre-requisite is that you must download the correct version of PowerShell and .Net Framework for your operating system. Microsoft’s site has separate versions of PowerShell for XP, Windows Server 2003 and Vista. In the case of Windows 7 and later, PowerShell is built-in but you need to ‘Add Feature’.