Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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

 ♣

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.

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.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

Download your free copy of WMI Monitor

Contents of My PowerShell Syntax 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 Server 2008, 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 tutorials

PShell Home  • Introduction   • Dreams   • 3 Key Commands   • PowerShell v 3.0

Process example   • Backtick   • Get-Command   • PowerShell ISE   • Cmdlet scripts

Windows PowerShell    • Set-ExecutionPolicy   • PowerShell examples   • Get-Member

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  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

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.