Microsoft PowerShell Syntax

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

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds 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)Review of Orion NPM v11.5 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'.

Other Microsoft PowerShell Topics

Summary of PowerShell Syntax

The pages in this section will help you with PowerShell's individual syntactic elements.  I 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.

Another use of this page is to investigate new parameters or switches for well-known commands, for example -recurse as in: Get-Childitem -recurse.

If you like this page then please share it with your friends

 


See more Windows PowerShell sections

PowerShell Home   • Introduction  • Dreams   • 3 Key Commands   • Free Permissions Analyzer

PowerShell Real-life Examples   • PowerShell Techniques   • PowerShell Syntax   • Contact

PowerShell ISE  • PowerShell 2.0   • PowerShell v 3.0   • PowerShell v 4.0   • PowerShell v 4.0

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2016 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: