Microsoft PowerShell Syntax
Introduction to Microsoft PowerShell's SyntaxThis
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
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 BracketsParenthesis 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"
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 {$_.extension -eq ".exe"} $List | Format-Table Name,
CreationTime -auto
See more examples of
comparison operators »
SolarWinds Firewall Browser

Here is an utility where you can review firewall settings such as
access control lists (ACL), or troubleshoot problems with network
address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include
managing requests to change your firewall settings, testing firewall
rules before you go live, and querying settings with the browser's
powerful search options.
Guy recommends that you download a copy of
the SolarWinds
free Firewall Browser.
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'.
|