Sessions and Session Configuration¶
PowerShell Remoting becomes significantly more powerful when you understand how to create, manage, and configure PowerShell sessions, often referred to as PSSessions. A PSSession is a persistent remote PowerShell environment that maintains state across multiple commands. Unlike one‑off remote executions, a session allows you to build multi‑step workflows, load modules once and reuse them, preserve variables, and interact with remote systems in a stable and predictable manner.
This section explains what sessions are, how they behave, and how session configuration controls what users can do inside them.
1. Understanding PowerShell Sessions¶
When you run a remote command without creating a session, PowerShell establishes a temporary connection, executes the command, returns the results, and immediately tears down the remote environment. This is efficient for simple tasks, but it does not preserve state.
A PSSession, created with New-PSSession, behaves differently:
- It creates a persistent remote PowerShell host.
- The host remains active until you explicitly close it.
- Variables, imported modules, and working directories remain available across commands.
- You can enter the session interactively or use it programmatically.
Conceptually, a PSSession is similar to opening a remote PowerShell console and leaving it open for repeated use.
2. Creating and Using PSSessions¶
A session is created using New-PSSession. Once created, you can run commands inside it or enter it interactively.
Creating a session¶
$session = New-PSSession -ComputerName "SERVER01"
This command establishes a persistent connection to the remote machine. The $session variable now represents a remote PowerShell environment.
Running commands inside a session¶
Invoke-Command -Session $session -ScriptBlock {
Get-Date
}
The script block executes inside the remote session. If you run another command using the same session, it will execute in the same remote environment.
Entering a session interactively¶
Enter-PSSession -Session $session
Your prompt changes to reflect the remote machine:
[SERVER01]: PS C:\>
Every command you run now executes on the remote system until you exit:
Exit-PSSession
Closing a session¶
Remove-PSSession $session
This terminates the remote host and frees system resources.
3. Why Persistent Sessions Matter¶
Persistent sessions are essential when:
- You need to run multiple related commands on the same remote machine.
- You want to load a module once and reuse it.
- You are performing multi‑step administrative tasks.
- You want to maintain state, such as variables or temporary files.
- You are working with remote systems interactively.
For example, if you import a module inside a PSSession, it remains available for subsequent commands:
Invoke-Command -Session $session -ScriptBlock {
Import-Module ActiveDirectory
}
Any later command in the same session can use the module without reloading it.
4. Session Configuration: Controlling What Users Can Do¶
PowerShell includes a powerful mechanism called session configuration, which defines the environment and permissions available inside a remote session. Session configurations determine:
- Which cmdlets and functions are available.
- Which language features are allowed (full language, restricted language, no language).
- Which modules are loaded automatically.
- Which users are allowed to connect.
- What the session’s working environment looks like.
Session configurations are the foundation of Just Enough Administration (JEA), but even outside JEA, they provide fine‑grained control over remote access.
5. Viewing Available Session Configurations¶
PowerShell stores session configurations as endpoints. You can list them with:
Get-PSSessionConfiguration
Each configuration represents a different type of remote environment. For example:
Microsoft.PowerShell— the default full‑language endpoint.Microsoft.PowerShell32— a 32‑bit endpoint on 64‑bit systems.- Custom endpoints created for restricted administration.
Each endpoint defines what the remote session allows.
6. Creating Custom Session Configurations¶
Administrators can create custom endpoints to restrict or tailor remote access. This is done using a session configuration file, which is a PowerShell data file that defines the environment.
A configuration file can specify:
- Which cmdlets are available.
- Which modules are loaded.
- Which variables are visible.
- Whether the user can run scripts.
- Whether the user can use the full PowerShell language or a restricted subset.
Once the configuration file is created, it is registered as an endpoint:
Register-PSSessionConfiguration -Name "LimitedAdmin" -Path "C:\Config\LimitedAdmin.pssc"
Users can then connect to this endpoint:
Enter-PSSession -ComputerName SERVER01 -ConfigurationName "LimitedAdmin"
This creates a remote session with the exact restrictions defined in the configuration file.
7. Using Sessions with Invoke-Command¶
Invoke-Command can operate on one or many sessions simultaneously.
Running a command on multiple sessions¶
$s1 = New-PSSession -ComputerName "SERVER01"
$s2 = New-PSSession -ComputerName "SERVER02"
Invoke-Command -Session $s1,$s2 -ScriptBlock {
Get-Service -Name "Spooler"
}
This approach is more efficient than repeatedly creating and destroying temporary connections.
8. Removing and Cleaning Up Sessions¶
It is important to close sessions when they are no longer needed. Leaving unused sessions open consumes resources on both the local and remote machines.
Get-PSSession | Remove-PSSession
This command closes all active sessions.
9. Summary¶
PowerShell sessions provide a persistent, stateful remote environment that enables sophisticated administrative workflows. Session configuration allows administrators to define exactly what users can do inside those sessions, from full administrative access to tightly restricted, task‑specific environments.
Together, PSSessions and session configurations form the backbone of secure, scalable remote management in PowerShell. They allow you to:
- Maintain state across remote commands.
- Execute multi‑step workflows efficiently.
- Control the capabilities and permissions of remote users.
- Build predictable and secure remote environments.
These capabilities are essential for modern Windows administration and form the basis for advanced remoting scenarios such as JEA.