Remoting
Prerequisites: Parallelism and jobs
PowerShell Remoting runs commands on other computers — one or hundreds at once — and returns the results as objects.
Setting it up
- Windows uses WinRM. It's enabled by default on Windows Server;
on a workstation, run
Enable-PSRemotingas administrator. Domain computers authenticate with Kerberos automatically. - SSH remoting works across Windows, Linux, and macOS: install OpenSSH and add PowerShell as an SSH subsystem on the target.
Running commands remotely
# illustrative
Invoke-Command -ComputerName web01, web02, web03 -ScriptBlock {
Get-Service -Name W3SVC | Select-Object Status
}
Status PSComputerName RunspaceId
------ -------------- ----------
Running web01 5e1c...
Running web02 a37b...
Stopped web03 c902...
- Commands run in parallel on every computer (32 at a time by
default;
-ThrottleLimitchanges it). - Each result carries a
PSComputerNameproperty. - Results are deserialized — they're snapshots with properties, but methods are gone. Do the work (filtering, method calls) inside the script block, and return only what you need.
Passing local values
# illustrative
$serviceName = 'W3SVC'
Invoke-Command -ComputerName web01 -ScriptBlock {
Restart-Service -Name $using:serviceName -PassThru
}
Sessions
For several commands on the same computers, create sessions once and reuse them:
# illustrative
$sessions = New-PSSession -ComputerName web01, web02
Invoke-Command -Session $sessions -ScriptBlock { $env:COMPUTERNAME }
Copy-Item -Path .\app.zip -Destination C:\Deploy\ -ToSession $sessions[0]
$sessions | Remove-PSSession
Enter-PSSession -ComputerName web01 # interactive remote prompt
Exit-PSSession
Invoke-Command -HostName linux01 -UserName deploy -ScriptBlock { uname -a } # over SSH
Credentials
# illustrative
$cred = Get-Credential
Invoke-Command -ComputerName web01 -Credential $cred -ScriptBlock { whoami }
Never put passwords in scripts; use Get-Credential, a secret vault
(see Secrets Management), or a managed identity.
JEA
Just Enough Administration endpoints let users run only specific commands remotely — for example, helpdesk staff can restart one service without being administrators.