PowerShell V5, PowerShell V5.1, and Windows Server 2016 also added new features.
Discovering new cmdlets in PowerShell 5/5.1 and Windows Server 2016
Getting ready
Run the commands in the following recipe on a Windows Server 2016 with Desktop Experience version.
PowerShellGet module
PowerShellGet, formerly known as OneGet, is a module that provides you with a simple way to discover, install, and update PowerShell modules and scripts. It has dependencies on the PackageManagement module, which relies on NuGet. It is an open source project, located at https://github.com/powershell/powershellget.
Refer to Explore PowerShellGet recipe.
PackageManagement module
The cmdlets in the PackageManagement module provide a single interface for software publication, discovery, installation, and inventory.
Refer to the following recipe:
- Explore PackageManagement
- Create a PackageManagement repository
Microsoft.PowerShell.Archive module
The Microsoft.Powershell.Archive module contains two useful functions: Compress-Archive and Expand-Archive. These enable you to create and extract ZIP files. With previous versions of PowerShell versions, you managed archives by using the System.IO.Compression namespace from the .Net framework, the Shell.Application com object or software like 7-Zip.
Microsoft.PowerShell.Utility module
The Microsoft.PowerShell.Utility module contains several new cmdlets useful for debugging interactively and within runspaces.
Debugging and runspace Cmdlets include: Get-Runspace, Debug-Runspace, Get-RunspaceDebug, Enable-RunspaceDebug, and Disable-RunspaceDebug, Wait-Debugger, Debug-Job.
These cmdlets enable debugging PowerShell scripts within runspaces and jobs and add additional debugging features for debugging production PowerShell interactively.
Other new modules
Other new modules in this version of PowerShell (and where to find more information about each module) include:
Module |
Description |
Documentation |
ConfigCI |
Manage the configurable code integrity policy for Windows |
|
Defender |
Manage Windows defender |
|
EventTracingManagement |
Manage event tracing for Windows providers and sessions |
|
HgsClient, ShieldedVMDataFile, and ShieldedVMTemplate |
Manage the host guardian service, for shielded Hyper-V guest machines. |
https://technet.microsoft.com/en-us/library/dn914505.aspx |
IISAdministration |
Manage IIS replaces WebAdministration cmdlets |
|
NetworkController |
Manage the new network controller role in Server 2016 |
|
NetworkSwitchManager |
Manage supported network switches in Server 2016 |
|
Pester |
Manage unit tests for PowerShell modules and cmdlets |
|
PnpDevice |
Cmdlets for managing plug and play devices |
|
StorageQoS and StorageReplica |
Support new storage functionality in Server 2016. |
https://technet.microsoft.com/en-us/library/mt608557.aspx |
Other new cmdlets
Some other useful cmdlets included are:
- Write-Information : A replacement for the Write-Host cmdlet that is consistent with the other Write-* cmdlets in the Microsoft.PowerShell.Utility namespace. See https://blogs.technet.microsoft.com/heyscriptingguy/2015/07/04/weekend-scripter-welcome-to-the-powershell-information-stream/.
- ConvertFrom-String and Convert-String: The new string parsing functions that create structured data from strings, or parse out string data into structured data. See https://blogs.msdn.microsoft.com/powershell/2014/10/31/convertfrom-string-example-based-text-parsing/.
- Format-Hex: This cmdlet formats information into hexadecimal.
- Get-Clipboard and Set-Clipboard: A cmdlet to simplify working with the clipboard, replacing piping to clip.exe.
- Clear-RecycleBin: This cmdlet empties the Recycle Bin.
- New-TemporaryFile: Simplifies the creation of temporary files within PowerShell scripts.
- New-Guid: A wrapper for [GUID]::NewGuid() to simplify the creation of Globally Unique Identifiers (GUIDs). A GUID is an identifier, unique in space and time, that you use in a variety of scenarios. System Center Virtual Machine Manager, for example, uses GUIDs in jobs created by the UI.
- Enter-PSHostProcess and Exit-PSHostProcess: These enable you to debug PowerShell processes outside the current host process.
- Export-ODataEndpointProxy: This cmdlet generates a wrapper module for working with an OData endpoint. See https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.odatautils/microsoft.powershell.odatautils.
Explore some of these cmdlets here and in later chapters as well.
How to do it...
- Investigate Write-Information by looking at the Write-* commands, and help for the about_Redirection topic:
Get-Command -Verb Write -Module *Utility Get-Help about_Redirection -ShowWindow
- Use Write-Information:
Write-Information "Test"
- This produces no output. To resolve, you should inspect and change the $InformationPreference variable:
Get-Variable "InformationPreference" Set-Variable -Name "InformationPreference" -Value "Continue"
- Use Write-Information again:
Write-Information "Test"
- Next, set $InformationPreference back to default value:
$InformationPreference = "SilentlyContinue"
- Review the information-related options in the CommonParameters of each command:
Show-Command Get-Item
- Use ConvertFrom-String to get objects from strings; NoteProperties are created with default names:
"Here is a sentence!" | ConvertFrom-String "Here is a sentence!" | ConvertFrom-String | Get-Member
- Use -PropertyNames to control the names:
"Here is a sentence!" |
ConvertFrom-String -PropertyNames First,Second,
Third,Fourth
- Use -Delimiter to get items from a list:
"Here,is,a,list!" |
ConvertFrom-String -PropertyNames First,Second,
Third,Fourth `
-Delimiter ','
- You next test the template capabilities of ConvertFrom-String:
$TextToParse = @'
Animal, Bird
Shape like Square
Number is 42
Person named Bob
'@$Template1 = @'
{[string]Category*:Animal}, {[string]Example:Bird}
'@ConvertFrom-String -TemplateContent $Template1 `
-InputObject $TextToParse
- ConvertFrom-String recognizes only one line from the text—the template needs more examples to train the function, so add a second example to the template and test:
$Template2 = @'
{[string]Category*:Animal}, {[string]Example:Bird}
{[string]Category*:Country} like {[string]Example:Italy}
'@
ConvertFrom-String -TemplateContent $Template2 `
-InputObject $TextToParse
- Note three lines are recognized, even the last line that is unusual. Adding another example to our template trains the function enough to recognize all four lines:
$Template3 = @'
{[string]Category*:Animal}, {[string]Example:Bird}
{[string]Category*:Country} like {[string]Example:Italy}
{[string]Category*:Number} like {[int]Example:99}
'@
ConvertFrom-String -TemplateContent $Template3 `
-InputObject $TextToParse
- Experiment with Format-Hex to output values in hexadecimal:
$TestValue = @" This is line 1 and line 2 "@ $TestValue | Format-Hex
- Experiment with Get-ClipBoard and Set-Clipboard by selecting some text, then press Ctrl+C to copy to clipboard, then inspect the clipboard:
#Select this line and press Control-C to copy to clipboard $Value = Get-Clipboard $Value
- Use Set-Clipboard to replace the clipboard value, then Ctrl+V to paste that new value:
$NewValue = "#Paste This!" $NewValue | Set-Clipboard #Press Control-V to paste!
How it works...
In step 1, you get the commands with the Write verb in the Microsoft.PowerShell.Utility module. Write-Information is an addition to this module that writes out to a new information stream, which the about_Redirection help topic describes in detail:
In steps 2-5, note that messages from Write-Information are not displayed by default. The $InformationPreference variable controls this behaviour within your PowerShell session.
In step 6, you'll see the CommonParameters now include InformationAction and InformationVariable
More information is available in Get-Help about_CommonParameters:
In step 7 you create a PSCustomObject using ConvertFrom-String with NoteProperties named P1, P2, P3, and P4 that correspond to words separated by whitespace from the input text, with string or char data types:
In step 8, you control the names of the NoteProperties. In step 9 you change the delimiter from the default of whitespace to a comma, thus parsing a comma separated list:
In step 10, you investigate the -TemplateObject parameter to parse inconsistently formatted data. Here you provide one or more patterns by example in the TemplateObject and provide the template along with the text to parse. The template starts with one line as an example, and initially recognizes only one line out of four in the text to match:
In steps 11 and steps 12, you improve the template with each attempt, achieving complete matching results from the Convert-FromString:
In step 13, you use Format-Hex on a here string that contains two lines of text. Note the 0D 0A bytes corresponding to carriage return and line feed (CRLF) between lines:
In step 14 and step 15, you work with Set-Clipboard and Get-Clipboard. By copying any text with Ctrl+C, you then capture that value into a variable with Get-Clipboard. You use Set-Clipboard to change that value, and use Ctrl+V to verify the change.
There's more...
Each PowerShell release comes with release notes that dive into the details of changes introduced with that version. These pages are updated with community contributions, as PowerShell is now partially open source:
- WMF 5.0 Release Notes: https://msdn.microsoft.com/en-us/powershell/wmf/5.0/releasenotes
- WMF 5.1 Release Notes: https://msdn.microsoft.com/en-us/powershell/wmf/5.1/release-notes
The documentation is published on GitHub and accepts contributions from users via pull-requests so users may help improve the documentation. You'll find PowerShell documentation on GitHub at https://github.com/PowerShell/PowerShell-Docs.
Complete documentation is available on TechNet, see the Windows 10 and Server 2016 PowerShell module reference at https://technet.microsoft.com/en-us/library/mt156917.aspx.