Taking on PowerShell one cmdlet at a time

Share this post: This is part of an ongoing blog series by Adam Gordon. Adam will walk through each PowerShell command every week, explaining when and how to use them. This week Adam will be covering New-PSSessionOption.

When should you use New-PSSessionOption
The New-PSSessionOption cmdlet creates a session object with advanced options. The object can be used as the value for the -SessionOption parameter in cmdlets that create PSSessions such as New-PSSession or Enter-PSSession.
New-PSSessionOption generates a single object without parameters that contains all the default values for all options. You can edit all properties and use the object as a template to create standard option objects in your enterprise.
You can also save session option objects in the $PSSessionOption preference variables. These variables set default values for session options. They are applicable when no session options have been set for the session. However, they can be overridden by specifying session options in a cmdlet that creates sessions.
NOTE: The session option objects in cmdlets that create sessions take precedence over the default session settings in the $PSSessionOption preference variables and the session configuration. They do not override any maximum values, limits, or quotas set in the session configuration.
What version of PowerShell do I use?
Get the PowerShell Version for your machine
$PSVersionTable
This command displays the PowerShell version information for your machine.

How to use New-PSSessionOption
You can create a default session option.
New-PSSessionOption

This command creates a session object with all the default values.
You can create a session using a session option object.
$pso = New-PSSessionOption -Culture “en-en” -MaximumReceivedObjectSize 10MB
New-PSSession -ComputerName ITPTV01 -SessionOption $pso

The first command creates a session option object and saves it to the $pso variable.
The second command uses New-PSSession cmdlet for creating a session on remote ITPTV01 computer. The session option object is used in the $pso value as the value for the -SessionOption parameter.
Start an interactive session
Enter-PSSession -ComputerName ITPTV01 -SessionOption (New-PSSessionOption -NoEncryption -NoCompression)

The value of -SessionOption is a New -PSSessionOption command with the -NoEncryption or -NoCompression parameters.
NOTE: The New–PSSessionOption command has been enclosed in parentheses so that it runs before Enter-PSSession.
Modify a session object:
$a = New-PSSessionOption$a.OpenTimeout$a.UICulture = (Get-UICulture)$a.OpenTimeout = (New-Timespan -Minutes 4)$a.MaximumConnectionRedirectionCount = 1$a

This method allows you to create a standard session object and then create customized versions for specific uses.
Make a preference variable
$PSSessionOption = New-PSSessionOption -OpenTimeOut 120000

The $PSSessionOption preference value is created when the session’s session begins. It sets default values for the options that are created using the New-PSSession and Enter-PSSession cmdlets.
NOTE: The $PSSessionOption option is now available in all sessions. Add it to your PowerShell session.
Learn the command last week: New-PSSession
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.