Commands / System

Invoke-Command

Run a command on another computer

intermediate System Aliases icm

Invoke-Command
 Invoke-Command -ComputerName PC01 -ScriptBlock { Get-Service -Name Spooler }
Works on
WindowsLinuxmacOS
PowerShell
5.1 and later
Reference
Microsoft Learn: Invoke-Command

Runs the script block on the remote computer and brings the output back. Each result gets a PSComputerName property, so you can tell machines apart.

-ComputerName takes a list, so one line can check many machines at once.

On a work network, remoting is often already set up by IT. At home, it is usually off.

Try this

Invoke-Command -ComputerName PC01, PC02 -ScriptBlock { Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object LastBootUpTime }

Use a different account:

$cred = Get-Credential
Invoke-Command -ComputerName PC01 -Credential $cred -ScriptBlock { hostname }

Pass a local value into the remote block with $using::

$name = 'Spooler'
Invoke-Command -ComputerName PC01 -ScriptBlock { Get-Service -Name $using:name }

Coming from another shell

ShellWhat you might type
bash / zshssh pc01 'systemctl status cups'

More in From bash and From cmd.

Related commands