How to write a PowerShell script in PowerShell 5.0

By default, PowerShell 5 provides the ability to create PowerShell scripts and to manage them in a simple manner.

This tutorial shows how to write and use a PowerShell 5 script in the same session.

This includes: Creating a PowerShell cmdlet in the PowerShell session You can create PowerShell cmdlets in the session with the Get-Command cmdlet and Get-Service cmdlet.

Adding new PowerShell cmdlines in the script editor You can add new PowerShell commands to a PowerShell session with Get-New-Command and Add-New cmdlet syntax.

Managing PowerShell scripts in the Script Manager If you are working with a PowerShell Script that is created with a command such as New-PSSession cmdlet, you can use the Add-PSScript cmdlet to add the script to the session.

To add the cmdlet you need to create a script in a PowerShell Session, as follows: Run the following command to open a PowerShell window.

PowerShell session: Add-Script cmdlets.exe -Name cmdletAdd-PS Script cmdletName: Add new PowerShell script to session.

Get-PSHost cmdlet: Get the host of the session from the session state.

Set-PSModule cmdlet Name: Set the module to the script.

Set -PSSessionName cmdlets: Set PowerShell session state to the new PowerShell session.

Add-PSCustomObject cmdlet name: Add a new object to the PowerShell namespace.