17 Jan 2013

Windows PowerShell 3.0 simplified syntax

A tip showing an example of how the simplified syntax of Windows PowerShell 3.0 makes commands and scripts easier to read.

One of the improvements in version 3.0 of Windows PowerShell is the simplification of the syntax for the Where-Object cmdlet.  For example, to display a list of all installed roles and features on a server, pipe the output of the Get-WindowsFeature cmdlet into the Where-Object cmdlet and use Where-Object to filter out everything except roles and features whose InstallState property is equal to Installed:

Get-WindowsFeature -ComputerName SEA-SRV-1 | Where-Object InstallState -eq Installed

You may have wondered why the above command didn't look like this:

Get-WindowsFeature - ComputerName SEA-SRV-1 | Where-Object {$_.InstallState -eq Installed}

The reason is because Windows PowerShell 3.0 lets you eliminate the script block notation (the curly braces), the current object placeholder ($_), and the dot property notation. These improvements make PowerShell code easier to understand.

No comments:

Post a Comment