Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.
 

How to Use the Microsoft PowerShell Cmdlets Utility Module

How to Use the Microsoft PowerShell Cmdlets Utility Module
By

Step-by-step instructions for using the PowerShell cmdlets Utility module that comes with Microsoft's Windows 10.

Credit: ShutterstockCredit: ShutterstockOne of the most exciting aspects of a new Windows operating system is the new PowerShell cmdlets that come along. Being an automation junkie, I'm always excited to see the new PowerShell cmdlets that Microsoft provides. In Server 2016, a few of these cmdlets come natively with the Microsoft PowerShell Utility module.

For Server 2016, they've added 12 new cmdlets in the Utility module.

  • ConvertFrom-String
  • Convert-String
  • Debug-Runspace
  • Disable-RunspaceDebug
  • Enable-RunspaceDebug
  • Format-Hex
  • Get-Runspace
  • Get-RunspaceDebug
  • New-Guid
  • New-TemporaryFile
  • Wait-Debugger
  • Write-Information

Here I've detailed a few of the more common ones and show you how they can save you a ton of time.

New-Guid

If you've ever wanted to generate a GUID to represent a unique object before New-Guid came along, you know the effort took longer and was more convoluted than you could possible want it to be. Since we didn't have a command to generate a GUID, we were forced to do this:

[guid]::NewGuid()

It wasn't a big deal, but you not only had to know how to invoke a static method (::), the name of the Guid object and the name of the method itself. Now, it's just as easy as running New-Guid and you're done.

New-TemporaryFile

The New-TemporaryFile shows that Microsoft is listening to the community, and trying to remove time-draining tasks as much as possible. In this instance, it's just creating a temporary file.

Before PowerShell v5, we had to create temporary files using two commands; one to find a temporary file name and the other to actually create the file.

$fileName = [System.IO.Path]::GetTempFileName()
Add-Content –Path $filePath –Value ''

Now, we simply need to execute New-TemporaryFile.

Write-Information

This command was built to use an entirely new feature in PowerShell v5 and Server 2016; the information stream. Previously, PowerShell had five different "streams"; Debug, Verbose, Warning and Error and Output. And, some would even mention a "host stream" in which Write-Host wrote to which was technically inaccurate. Write-Host was always a running joke in the PowerShell community and many including Jeffrey Snover warned against its use. Write-Host was meant to output messaging to the console but was engineering so that it's output could not be captured.

After a large community effort, Microsoft decided to introduce a sixth "stream" known as the information stream which can be written to by the Write-Information command. Similar to other Write commands, Write-Information wrote to the host as Write-Host did but wrote it a more structured way.
Using Write-Information, you could now redirect it's output somewhere else. In this case, I'm redirecting to a file and showing how you can redirect to a variable. None of this was possible with Write-Host.

You could also use Write-Information to assign tags to the information records that it returns. This is useful if you have a lot of Write-Information references in a script and would like to query all of them somehow at the in for reporting purposes.

As you can see Write-Information was much more advanced than Write-Host was.

Get-Runspace

A Runspace in PowerShell is a specific namespace where command are run. If you open up a PowerShell console, that's a single Runspace. If you open up a PowerShell remoting session that's another Runspace. Runspaces are sessions or containers that are separate from one another and are used for separating different sessions.

Before Get-Runspace came around, PowerShell scripters had to learn a little .NET by getting comfortable with the GetRunspaces() method and passing in specific arguments. This was cumbersome to those that weren't developers. Get-Runspace eliminates the need to use .NET and lets you find all of the Runspaces that have been created.

You can see above I have standard runspace number 1, and I've also got some others available to me since I've been opening up a few PowerShell Remoting sessions.

Most of these commands don't require Server 2016, but just PowerShell v5. To get started using them, get upgraded to PowerShell v5, and get started with Server 2016 to learn what else it has to offer.