My Favorite PowerShell 4 Features (Part 1)

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:

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 a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 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 works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.