Invoke-Command
Use Invoke-Command to run a command or script block on one or more remote computers and return the results.
Documentation: Microsoft Learn — PowerShell module reference
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Invoke-Command [-ComputerName <String[]>] [-ScriptBlock] <ScriptBlock> [-Credential <PSCredential>] [-ArgumentList <Object[]>] [-ThrottleLimit <Int32>] [-AsJob] [-Session <PSSession[]>] [<CommonParameters>]- Stage 1Invoke-Command
-ComputerName -ScriptBlock -Credential -ArgumentList -ThrottleLimit -AsJob -Session
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Invoke-Command -ScriptBlock {
Show reference output
Sample output · fictional data, shaped like the real thing
Name Status ---- ------ BITS Running WinRM Stopped
Invoke-Command -ComputerName SRV01
Show reference output
Sample output · fictional data, shaped like the real thing
Status Name DisplayName ------- ------- ------------- Stopped Spooler Print Spooler
Invoke-Command -ComputerName SRV01 (2)
Show reference output
Sample output · fictional data, shaped like the real thing
Status Name DisplayName ------- -------- --------------------------------------- Running BITS Background Intelligent Transfer Service Running Dnscache DNS Client Stopped Spooler Print Spooler Running WinRM Windows Remote Management
Parameters
Parameter definitions:
-ComputerName-ScriptBlock-Credential-ArgumentList-ThrottleLimit-AsJob-Session
Practical use and troubleshooting
Practical scenario: Use Invoke-Command when you need to run a command or script block on one or more remote computers and return the results. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: Access denied during a remote second-hop operation can involve credential delegation. Identify which machine needs the second connection and use an approved delegation design rather than embedding passwords. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: Invoke-RestMethod. Invoke-Command executes a script block locally or through PowerShell remoting; Invoke-RestMethod calls an HTTP endpoint and processes supported response formats. They use different authentication and transport mechanisms. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Put this command to work
Read remote services
Read all service records from fictional lab-server. Use Invoke-Command -ComputerName lab-server -ScriptBlock. No network is contacted.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.
Inspect starting data
Supported syntax
Virtual files
Recap
Invoke-Command Use Invoke-Command to run a command or script block on one or more remote computers and return the results.
Next: Invoke-WebRequest