Prof. Powershell

My Favorite PowerShell 4 Features (Part 1)

Jeffrey Hicks runs down a few of his top reasons why you should be running the latest version of PowerShell.

See Also:

Now that Windows 8.1 and Windows Server 2012 R2 have been released, this means PowerShell 4.0 is also finally here. You can also download and install it on older systems, if you consider Windows 7 and Windows Server 2008 R2 old. PowerShell 4 is part of the Windows Management Framework 4 which you can download here. But why should you? Let me share a few of my favorite new features.

If you are still on PowerShell 2.0, then you have been missing out on v3 features like the CIM cmdlets and default parameter values, which continue in v4. First, if you are ever in doubt about what version you are running, take a look at $PSVersionTable:

PS C:\> $psversiontable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.33440
BuildVersion                   6.3.9600.16384
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

Method Invocation
One handy feature, although I admit it may confuse PowerShell beginners, is method invocation. In the past you would have typed an expression like this:

PS C:\> get-service | where {$_.status -eq 'running'}

Status   Name               DisplayName
------   ----               -----------
Running  Appinfo            Application Information
Running  AppXSvc            AppX Deployment Service (AppXSVC)
Running  AudioEndpointBu... Windows Audio Endpoint Builder

Or in v3 you could do this:

PS C:\> get-service | where status -eq 'running'

But now in v4 we can do this and get the same result:

PS C:\> (get-service).where({$_.status -eq 'running'})

This also works with ForEach.

PS C:\> (1..5).foreach({$psitem * 2})
2
4
6
8
10

You can use either $_ or $psitem. Before you get too excited this only works with Where and ForEach. This syntax is a bit more compact to type which makes it great in an interactive session. But I would refrain from using it in a script as it is a bit more complicated to understand.

Require Admin
When creating a script in PowerShell 4.0, you can now require that the user have administrator rights. No more .NET coding to test if the user is an administrator. At the beginning of your script insert these lines:

#requires -version 4.0
#requires –runasadministrator

That's it. If the user is not running as an administrator in an elevated session, the script will throw an exception.

Bug Fixes
I am also happy to see a few bugs cleaned up that made life occasionally difficult. For example, Import-CSV will now skip blank lines. That means no more filtering or import hacks to accommodate less than perfect CSV files.

I liked the Web cmdlets that were introduced in 3.0 but they had a few problems. Invoke-RestMethod was known to not always return all results. Now it does. There was also a problem when running Invoke-WebRequest in the PowerShell ISE that could result in a nasty memory leak. That too has been corrected.

That's all for this lesson. I'll be back next time with some more PowerShell 4.0 goodies.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events