📖 Scenario: You want to run commands on a remote computer using PowerShell 7 or newer. This is useful when you manage servers or other computers from your own machine. SSH-based remoting lets you connect securely and run commands as if you were sitting at the remote computer.
🎯 Goal: Learn how to set up a simple SSH session in PowerShell 7+, run a command remotely, and see the output on your local computer.
📋 What You'll Learn
Create a variable with the remote computer name
Create a variable with the username for SSH login
Use New-PSSession with SSH transport to connect
Use Invoke-Command to run a command remotely
Display the output of the remote command
💡 Why This Matters
🌍 Real World
System administrators often need to manage multiple servers remotely. SSH-based remoting in PowerShell 7+ lets them run commands securely without logging in physically.
💼 Career
Knowing how to use SSH remoting with PowerShell is valuable for IT professionals, DevOps engineers, and anyone managing Windows or Linux servers remotely.
Progress0 / 4 steps
1
Set up remote computer and username variables
Create a variable called $remoteComputer and set it to the string "server01.example.com". Then create a variable called $userName and set it to the string "adminuser".
PowerShell
Hint
Use = to assign strings to variables. Strings go inside double quotes.
2
Create an SSH PowerShell session
Using the variables $remoteComputer and $userName, create a new PowerShell session called $session using New-PSSession with the -HostName parameter set to $remoteComputer, the -UserName parameter set to $userName, and the -SSHTransport switch.
PowerShell
Hint
Use New-PSSession with the parameters exactly as shown. The -SSHTransport switch enables SSH remoting.
3
Run a remote command using the session
Use Invoke-Command with the -Session parameter set to $session to run the command hostname on the remote computer. Save the result in a variable called $remoteHostName.
PowerShell
Hint
Use Invoke-Command with -Session and a script block containing hostname.
4
Display the remote hostname output
Print the value of the variable $remoteHostName to show the remote computer's hostname.
PowerShell
Hint
Use Write-Output to print the variable. The output should be the hostname of the remote computer, which is server01.
Practice
(1/5)
1. What is the main purpose of using SSH-based remoting in PowerShell 7+?
easy
A. To create graphical user interfaces in PowerShell scripts
B. To securely run commands on remote computers using PowerShell
C. To compile PowerShell scripts into executable files
D. To manage local files faster on your own computer
Solution
Step 1: Understand SSH-based remoting concept
SSH-based remoting allows secure communication to run commands on other computers remotely.
Step 2: Identify the main use case in PowerShell 7+
PowerShell 7+ uses SSH remoting to securely run commands or scripts on remote machines.
Final Answer:
To securely run commands on remote computers using PowerShell -> Option B
2. Which of the following is the correct syntax to start an interactive SSH-based PowerShell session to a remote computer named Server01?
easy
A. Enter-PSSession -HostName Server01
B. Invoke-Command -Session Server01 -SSH
C. Enter-PSSession -ComputerName Server01 -UseSSH
D. New-PSSession -ComputerName Server01
Solution
Step 1: Recall Enter-PSSession syntax for SSH remoting
PowerShell 7+ uses Enter-PSSession -HostName <name> to start SSH sessions.
Step 2: Compare options
Enter-PSSession -HostName Server01 uses -HostName Server01, which is correct for SSH remoting. Enter-PSSession -ComputerName Server01 -UseSSH uses -ComputerName with -UseSSH which is invalid syntax. Invoke-Command -Session Server01 -SSH mixes parameters incorrectly. New-PSSession -ComputerName Server01 creates a session but does not specify SSH or hostname properly.
Final Answer:
Enter-PSSession -HostName Server01 -> Option A
Quick Check:
SSH session uses -HostName parameter [OK]
Hint: Use Enter-PSSession with -HostName for SSH [OK]
Common Mistakes:
Using -ComputerName instead of -HostName for SSH
Mixing Invoke-Command with session parameters
Omitting SSH-specific parameters
3. What will be the output of this PowerShell command run locally if the remote computer Server01 is reachable and has PowerShell 7+ installed?
The error says: "The term 'Enter-PSSession' is not recognized." What is the most likely cause?
medium
A. SSH service is not running on Server01
B. The remote computer Server01 is offline
C. You forgot to specify the username
D. PowerShell version is older than 7 on the local machine
Solution
Step 1: Understand the error message
The error says 'Enter-PSSession' is not recognized, meaning the command is missing locally.
Step 2: Identify cause related to PowerShell version
Enter-PSSession with -HostName for SSH remoting is available only in PowerShell 7+. Older versions do not support it.
Final Answer:
PowerShell version is older than 7 on the local machine -> Option D
Quick Check:
Command not recognized = old PowerShell version [OK]
Hint: Check PowerShell version if command not recognized [OK]
Common Mistakes:
Assuming remote offline causes local command error
Blaming SSH service without checking local version
Thinking username is mandatory for command recognition
5. You want to run a script on multiple remote computers Server01 and Server02 using SSH remoting in PowerShell 7+. Which command correctly runs Get-Process on both and collects results?
B. Invoke-Command -ComputerName Server01,Server02 -UseSSH -ScriptBlock { Get-Process }
C. Invoke-Command -HostName Server01,Server02 -ScriptBlock { Get-Process }
D. Enter-PSSession -HostName Server01; Enter-PSSession -HostName Server02; Get-Process
Solution
Step 1: Understand running commands on multiple SSH hosts
Invoke-Command -HostName accepts an array of multiple host names directly in PowerShell 7+.
Step 2: Analyze options
Invoke-Command -HostName Server01,Server02 -ScriptBlock { Get-Process } runs Get-Process on both remote servers and collects the results.
Step 3: Why other options are incorrect
A uses New-PSSession -ComputerName, which targets WinRM not SSH. B uses -ComputerName with invalid -UseSSH. C enters sessions sequentially but Get-Process runs locally after.
Final Answer:
Invoke-Command -HostName Server01,Server02 -ScriptBlock { Get-Process } -> Option C