Bird
Raised Fist0
PowerShellscripting~20 mins

PSSession management in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
PSSession Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell command?
You create a new PSSession to a remote computer and then list all active sessions. What will this command output?
PowerShell
New-PSSession -ComputerName Server01
Get-PSSession | Select-Object -Property ComputerName, State
A@{ComputerName=Server01; State=Opened}
B@{ComputerName=Server01; State=Disconnected}
CNo output, because Get-PSSession requires a session ID
DError: New-PSSession requires credentials
Attempts:
2 left
💡 Hint
Think about what state a newly created PSSession has before you disconnect it.
📝 Syntax
intermediate
2:00remaining
Which command correctly closes and removes all PSSessions?
You want to close all active PSSessions and remove them from your session list. Which command does this correctly?
AGet-PSSession | Close-PSSession
BGet-PSSession | Remove-PSSession
CClose-PSSession -All
DRemove-PSSession -All
Attempts:
2 left
💡 Hint
Remember the cmdlet used to remove sessions is Remove-PSSession.
🔧 Debug
advanced
2:00remaining
Why does this script fail to reconnect to a disconnected PSSession?
You run this script but get an error when trying to reconnect: $session = New-PSSession -ComputerName Server01 Disconnect-PSSession -Session $session Connect-PSSession -Session $session
PowerShell
$session = New-PSSession -ComputerName Server01
Disconnect-PSSession -Session $session
Connect-PSSession -Session $session
AConnect-PSSession cmdlet does not exist; use Connect-PSSession only in PowerShell 7+
BConnect-PSSession requires the session to be in Disconnected state, but the session was never disconnected properly
CConnect-PSSession is not a valid cmdlet; use Connect-PSSession only with disconnected sessions
DThe cmdlet name is incorrect; it should be 'Connect-PSSession' replaced with 'Connect-PSSession -Id <id>'
Attempts:
2 left
💡 Hint
Check if Connect-PSSession is a valid cmdlet in PowerShell.
🚀 Application
advanced
2:00remaining
How many PSSessions remain after running this script?
You run this script: $s1 = New-PSSession -ComputerName Server01 $s2 = New-PSSession -ComputerName Server02 Remove-PSSession -Session $s1 Get-PSSession | Measure-Object | Select-Object -ExpandProperty Count What is the output number?
PowerShell
$s1 = New-PSSession -ComputerName Server01
$s2 = New-PSSession -ComputerName Server02
Remove-PSSession -Session $s1
Get-PSSession | Measure-Object | Select-Object -ExpandProperty Count
AError: Remove-PSSession requires session ID
B2
C0
D1
Attempts:
2 left
💡 Hint
Think about how many sessions you created and how many you removed.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using PSSession over Invoke-Command for multiple commands?
You want to run several commands on a remote computer efficiently. Why choose PSSession instead of multiple Invoke-Command calls?
APSSession automatically retries commands on failure, Invoke-Command does not
BInvoke-Command cannot run multiple commands on the same remote computer
CPSSession keeps a persistent connection, reducing overhead for multiple commands
DInvoke-Command requires credentials every time, PSSession does not
Attempts:
2 left
💡 Hint
Think about connection setup and reuse.

Practice

(1/5)
1. What is the primary purpose of a PSSession in PowerShell?
easy
A. To store variables locally on your computer
B. To create a persistent connection to a remote computer for running commands
C. To display graphical user interfaces
D. To compile PowerShell scripts into executables

Solution

  1. Step 1: Understand what PSSession does

    A PSSession creates a persistent connection to a remote computer, allowing you to run commands there without reconnecting each time.
  2. Step 2: Compare options to this definition

    Only To create a persistent connection to a remote computer for running commands describes this purpose. Other options describe unrelated tasks.
  3. Final Answer:

    To create a persistent connection to a remote computer for running commands -> Option B
  4. Quick Check:

    PSSession = persistent remote connection [OK]
Hint: PSSession = persistent remote connection [OK]
Common Mistakes:
  • Confusing PSSession with local variable storage
  • Thinking PSSession creates GUIs
  • Assuming PSSession compiles scripts
2. Which of the following is the correct syntax to create a new PSSession to a computer named 'Server01'?
easy
A. Get-PSSession -ComputerName Server01
B. Invoke-Command -ComputerName Server01
C. New-PSSession -ComputerName Server01
D. Remove-PSSession -ComputerName Server01

Solution

  1. Step 1: Identify the cmdlet to create a session

    The cmdlet to create a new session is New-PSSession.
  2. Step 2: Check the syntax for specifying the remote computer

    The parameter -ComputerName followed by the computer's name is correct for New-PSSession.
  3. Final Answer:

    New-PSSession -ComputerName Server01 -> Option C
  4. Quick Check:

    Create session = New-PSSession [OK]
Hint: New-PSSession creates sessions; use -ComputerName for target [OK]
Common Mistakes:
  • Using Invoke-Command instead of New-PSSession to create session
  • Using Remove-PSSession to create session
  • Confusing Get-PSSession with creation
3. What will be the output of this PowerShell code?
 $session = New-PSSession -ComputerName localhost
Invoke-Command -Session $session -ScriptBlock { Get-Process | Select-Object -First 1 }
Remove-PSSession -Session $session 
medium
A. Displays the first process running on the local computer
B. Creates a session but does not display any output
C. Throws an error because Remove-PSSession is called too early
D. Displays all processes running on the local computer

Solution

  1. Step 1: Understand the commands

    The code creates a session to localhost, runs a command to get the first process, then removes the session.
  2. Step 2: Analyze the Invoke-Command output

    The Invoke-Command runs Get-Process | Select-Object -First 1 remotely, so it outputs the first process object.
  3. Final Answer:

    Displays the first process running on the local computer -> Option A
  4. Quick Check:

    Invoke-Command outputs first process [OK]
Hint: Invoke-Command outputs scriptblock result before Remove-PSSession [OK]
Common Mistakes:
  • Thinking Remove-PSSession stops output
  • Assuming no output from Invoke-Command
  • Confusing output with all processes
4. You run this script:
 $session = New-PSSession -ComputerName Server01
Invoke-Command -Session $session -ScriptBlock { Get-Date }
Remove-PSSession -Session 

But it throws an error: "Remove-PSSession : Cannot bind argument to parameter 'Session' because it is null." What is the problem?
medium
A. Invoke-Command syntax is incorrect
B. New-PSSession failed to create a session
C. Get-Date cannot run inside a PSSession
D. Remove-PSSession is missing the -Session parameter with the session variable

Solution

  1. Step 1: Identify the error cause

    The error says Remove-PSSession has a null argument for -Session, meaning it was called without specifying which session to remove.
  2. Step 2: Check the Remove-PSSession command usage

    The script calls Remove-PSSession -Session without the $session value, so PowerShell doesn't know which session to close.
  3. Final Answer:

    Remove-PSSession is missing the -Session parameter with the session variable -> Option D
  4. Quick Check:

    Remove-PSSession needs -Session argument [OK]
Hint: Always specify -Session when removing sessions [OK]
Common Mistakes:
  • Forgetting to pass the session variable to Remove-PSSession
  • Assuming Remove-PSSession closes all sessions by default
  • Blaming Invoke-Command or New-PSSession incorrectly
5. You want to run a command on multiple remote computers and keep the sessions open for later use. Which approach correctly creates sessions for 'Server01' and 'Server02' and stores them for reuse?
hard
A. $sessions = New-PSSession -ComputerName Server01 $sessions += New-PSSession -ComputerName Server02 Invoke-Command -Session $sessions -ScriptBlock { hostname }
B. $sessions = New-PSSession -ComputerName Server01, Server02 Invoke-Command -ComputerName Server01, Server02 -ScriptBlock { hostname }
C. Invoke-Command -ComputerName Server01, Server02 -ScriptBlock { hostname } $sessions = $null
D. $sessions = New-PSSession -ComputerName Server01 Invoke-Command -Session $sessions -ScriptBlock { hostname } Remove-PSSession -Session $sessions

Solution

  1. Step 1: Recall how to create persistent sessions for multiple computers

    New-PSSession can take multiple ComputerName values or create separately and collect into an array with +=. Invoke-Command must use -Session $sessions (array) to run on persistent sessions.
  2. Step 2: Evaluate each option

    A creates sessions but uses -ComputerName on Invoke-Command, creating temporary connections instead of using persistent ones. B builds the array explicitly and uses -Session. C uses temporary only. D removes sessions.
  3. Final Answer:

    $sessions = New-PSSession -ComputerName Server01 $sessions += New-PSSession -ComputerName Server02 Invoke-Command -Session $sessions -ScriptBlock { hostname } -> Option A
  4. Quick Check:

    Build session array with +=, use -Session [OK]
Hint: Create and store sessions in a variable array for reuse [OK]
Common Mistakes:
  • Removing sessions immediately before reuse
  • Not storing sessions in a variable
  • Using Invoke-Command without sessions for reuse