Nmedia - Fotolia


Four features in PowerShell version 5 that deserve props

There are several lesser known but important improvements to PowerShell 5 features that will make life easier for sys admins who prefer the command-line interface.

Microsoft has invested a lot of time to add numerous features to Windows PowerShell version 5.  Some of the more prominent features in PowerShell 5 such as classes, the PowerShellGet module, the PackageManagement module and the new Write-Information cmdlet have gotten a lot of attention.

However, there were dozens of other useful updates to PowerShell that got lost in the buzz of the big updates. Here are four new PowerShell 5 features I believe should be getting more coverage.

A better file management cmdlet

Lee Holmes, senior software developer engineer for PowerShell at Microsoft, built a popular script years ago called Send-File.ps1 that sends files over PowerShell remoting sessions. The script is useful because it does not require Server Message Block to get a file from point A to point B.

Using that script, administrators didn't worry about opening up another port on the firewall. File copies could then use the PowerShell remoting TCP port. Microsoft decided to bring this functionality into the native codebase by implementing this functionality on the Copy-Item cmdlet using the ToSession parameter.

PowerShell copying
Figure 1: Microsoft added a more seamless way to copy files and folders between PowerShell sessions.

In Figure 1, the example shows how to establish a new PowerShell session to a remote computer, specify the source of a local file, the destination and the session to use.  No external script necessary.

Native .zip support added

In the previous version of PowerShell, there was no native function to create and expand .zip files. You were forced to scour the Internet for scripts to do this. PowerShell version 5 makes working with .zip files more convenient because this support is built right into the language with the Microsoft.PowerShell.Archive module. This module contains a cmdlet to create .zip files called Compress-Archive and a cmdlet to unzip or expand .zip files called Expand-Archive.

Support.zip file
Figure 2: The Compress-Archive cmdlet can take a group of files and compress them into a .zip file.

In Figure 2, you can see it's possible to gather a list of files in a folder then pass this to Compress-Archive to create a .zip file. Conversely, you can pass the .zip file to Expand-Archive which will expand the contents.

Native symbolic link support

The bash shell in Linux has the ability to create file system symbolic links for quite a while. Although this was technically possible in previous versions of PowerShell by developing code in .NET or by using command-line utilities such as mklink.exe, it is now a native feature in PowerShell version 5.

By adding a single parameter argument to the ItemType parameter of the New-Item cmdlet you can natively create symbolic links without the use of any external command-line utilities or .NET code.

Creating symbolic links
Figure 3: The ItemType parameter used with the New-Item cmdlet can create symbolic links natively in PowerShell 5.

In Figure 3, we create a symbolic link called MySymLinkFile.txt in the C:\Windows\Temp folder that is linked to the text.txt file in the current directory. Notice the Mode property and the last attribute letter "l" that signifies a symbolic link.

Limiting folder depth with Get-ChildItem

PowerShell has long had the Get-ChildItem cmdlet to list various items presented by providers. Using Get-ChildItem to list files and folders on the file system or registry keys in the registry are some of the popular uses of this cmdlet. The file system and registry both store information in a hierarchical form through folders or registry keys.

Sys admins could use the -Recurse parameter to find items stored in subfolders or keys; this parameter could "walk" the hierarchy of items to list all subfolders or registry keys. But limiting the depth of the hierarchy was always a challenge. Microsoft developed the –Depth parameter for Get-ChildItem to make this task easier.

Limiting folder depth
Figure 4: Microsoft developed the –Depth parameter on Get-ChildItem cmdlet to limit the listing of folders by specifying the depth.

Let's say we have a folder structure that is five levels deep; using Get-ChildItem with the –Recurse parameter I can list each of these folders. But if, in Figure 4, I want to see all the folders just two levels deep, I can specify this by using the –Depth parameter and the amount I want to see.

These are just four of the many useful updates to PowerShell 5. To see the entire list of new features, check out the What's New in PowerShell Technet article where Microsoft details the new updates and features.

Next Steps

How to deconstruct strings with PowerShell 5

Remote file editing and other PowerShell 5 improvements

Three advantages to using PowerShell Desired State Configuration

Dig Deeper on Windows administration tools