Exploring New-PSTransportOption in PowerShell
Welcome back to Wahmans PowerShell blog! Today we’re diving into a somewhat lesser-known cmdlet that can be incredibly useful when setting up advanced PowerShell session configurations – New-PSTransportOption.
What does New-PSTransportOption do?
According to Microsoft, it “Creates an object that contains advanced options for a session configuration.” Essentially, it allows you to control specific aspects of PowerShell remoting, such as idle timeouts, maximum number of concurrent users, and more. It is commonly used with Register-PSSessionConfiguration or Set-PSSessionConfiguration when you want to fine-tune your session settings.
Example 1: Creating a Basic Transport Option Object
Let’s start with a simple example. Here we create a transport option object with a maximum idle timeout of 30 minutes (1800000 milliseconds).
$transportOption = New-PSTransportOption -IdleTimeout 1800000
$transportOption
This will output an object where the IdleTimeout property is set. You can now use this object when registering or modifying a session configuration.
Example 2: Setting Up Multiple Options
Here, we configure several session limits: idle timeout, maximum concurrent shells per user, and total concurrent users.
$options = New-PSTransportOption -IdleTimeout 1800000 -MaxConcurrentUsers 5 -MaxConcurrentCommandsPerSession 4
Register-PSSessionConfiguration -Name "CustomShell" -TransportOption $options
This creates a new session configuration named CustomShell with our specified transport settings.
Example 3: Enforcing Idle Disconnection
If you want to automatically disconnect idle sessions rather than leave them hanging, you can use OutputBufferingMode.
$opt = New-PSTransportOption -IdleTimeout 600000 -OutputBufferingMode Drop
Set-PSSessionConfiguration -Name "PSCustom" -TransportOption $opt
In this case, any output generated after the session goes idle will not be buffered for the client to fetch later – it will be discarded.
Example 4: Advanced Configuration for Security-Conscious Environments
Let’s say you’re setting up an environment where you want to tightly control resource usage per session. You could combine several parameters:
$SecureOptions = New-PSTransportOption `
-IdleTimeout 300000 `
-MaxConcurrentUsers 3 `
-MaxIdleTimeout 3600000 `
-MaxConcurrentCommandsPerSession 2 `
-MaxSessionsPerUser 1
Register-PSSessionConfiguration -Name "RestrictedShell" -RunAsCredential (Get-Credential) -TransportOption $SecureOptions
This script sets tight restrictions on how long sessions can be idle, how many commands and sessions are allowed, and even prompts for a credential to restrict execution contexts.
As you can see, New-PSTransportOption gives you fine-grained control over how PowerShell sessions behave. Whether you’re just learning PowerShell remoting or setting up highly restricted environments, this cmdlet is one to have in your toolkit.
Happy scripting, and I will see you in the next post!
Leave a Reply