How to Use Remote PowerShell: Syntax and Examples
Use
Enter-PSSession to start an interactive remote PowerShell session or Invoke-Command to run commands remotely. Ensure PowerShell remoting is enabled on the target machine with Enable-PSRemoting.Syntax
Enter-PSSession starts an interactive session with a remote computer.
Invoke-Command runs commands on one or more remote computers.
Enable-PSRemoting enables remoting on the target machine.
Enter-PSSession -ComputerName <Name>: Connect interactively.Invoke-Command -ComputerName <Name> -ScriptBlock { commands }: Run commands remotely.Enable-PSRemoting -Force: Turn on remoting on the remote PC.
powershell
Enable-PSRemoting -Force
Enter-PSSession -ComputerName Server01
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process }Example
This example shows how to enable remoting, start an interactive session, and run a command remotely to get the list of running processes.
powershell
Enable-PSRemoting -Force
Enter-PSSession -ComputerName Server01
# After connecting, run:
Get-Process
# Exit session with:
Exit-PSSession
# Or run a command remotely without interactive session:
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process | Select-Object -First 3 }Output
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
123 10 15000 30000 0.03 1234 1 notepad
234 15 25000 40000 0.10 2345 1 powershell
345 20 35000 50000 0.20 3456 1 explorer
Common Pitfalls
Common mistakes include not enabling remoting on the remote machine, firewall blocking the connection, or using wrong credentials.
Also, Enter-PSSession is for interactive use only, while Invoke-Command is better for scripts.
powershell
## Wrong: Trying to connect without enabling remoting Enter-PSSession -ComputerName Server01 ## Right: Enable remoting first Enable-PSRemoting -Force Enter-PSSession -ComputerName Server01
Quick Reference
| Command | Purpose |
|---|---|
| Enable-PSRemoting -Force | Enable PowerShell remoting on the machine |
| Enter-PSSession -ComputerName | Start an interactive remote session |
| Invoke-Command -ComputerName | Run commands remotely without interactive session |
| Exit-PSSession | Exit the interactive remote session |
Key Takeaways
Always enable PowerShell remoting on the remote machine before connecting.
Use Enter-PSSession for interactive remote sessions and Invoke-Command for running scripts remotely.
Check firewall and network settings if remote connection fails.
Use correct credentials and permissions to access the remote computer.
Exit-PSSession to close interactive remote sessions cleanly.