0
0
PowerShellscripting~10 mins

PSSession management in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PSSession management
Create PSSession
Use PSSession to run commands
Check session status
Remove PSSession
End
This flow shows how to create a PowerShell session, use it to run commands, check its status, and then remove it to clean up.
Execution Sample
PowerShell
$s = New-PSSession -ComputerName localhost
Invoke-Command -Session $s -ScriptBlock { Get-Process }
Remove-PSSession -Session $s
Creates a session to localhost, runs Get-Process inside it, then removes the session.
Execution Table
StepActionCommandResultSession State
1Create session$s = New-PSSession -ComputerName localhostSession created with Id 1Opened
2Run command in sessionInvoke-Command -Session $s -ScriptBlock { Get-Process }List of processes returnedOpened
3Remove sessionRemove-PSSession -Session $sSession removedClosed
4Check session state$s.StateClosedClosed
💡 Session removed and closed, no longer available for commands.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$snullPSSession object (Id 1, Opened)Same session (Opened)Session removed (Closed)Closed
Key Moments - 3 Insights
Why do we need to remove a PSSession after using it?
Removing the session frees resources and closes the connection. See execution_table step 3 where Remove-PSSession closes the session.
Can we run commands on a session after it is removed?
No, once removed, the session state is Closed and commands cannot run. See execution_table step 4 showing session state Closed.
What does New-PSSession do exactly?
It creates a persistent connection to a remote or local computer to run commands later. See execution_table step 1 where session is created and state is Opened.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the session state right after creating the session?
ADisconnected
BClosed
COpened
DNot created
💡 Hint
Check execution_table row 1, column 'Session State' shows 'Opened'.
At which step is the session removed?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
See execution_table row 3 where Remove-PSSession command is run.
If we skip Remove-PSSession, what would happen to the session state?
AIt stays Opened
BIt becomes Closed automatically
CIt becomes Disconnected immediately
DIt is deleted automatically
💡 Hint
Refer to variable_tracker showing session state remains Opened until removed.
Concept Snapshot
PSSession management:
- Use New-PSSession to create a session
- Run commands with Invoke-Command -Session
- Check session state with $session.State
- Remove session with Remove-PSSession to free resources
- Sessions keep connection open until removed
Full Transcript
This lesson shows how to manage PowerShell sessions called PSSessions. First, you create a session using New-PSSession to connect to a computer. Then you run commands inside that session with Invoke-Command. You can check the session's state anytime. When done, you remove the session with Remove-PSSession to close the connection and free resources. The execution table traces these steps showing session creation, command execution, removal, and final state. Variables track the session object state changes. Key moments clarify why removing sessions is important and what happens if you don't. The quiz tests understanding of session states and lifecycle. This helps beginners see how sessions open, work, and close step-by-step.