PowerShell Genie

Invoke-Command

Use Invoke-Command to run a command or script block on one or more remote computers and return the results.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — PowerShell module reference

Syntax

1PS> Invoke-Command [-ComputerName <String[]>] [-ScriptBlock] <ScriptBlock> [-Credential <PSCredential>] [-ArgumentList <Object[]>] [-ThrottleLimit <Int32>] [-AsJob] [-Session <PSSession[]>] [<CommonParameters>]
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>]
  1. Stage 1Invoke-Command-ComputerName -ScriptBlock -Credential -ArgumentList -ThrottleLimit -AsJob -Session

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Invoke-Command -ScriptBlock {

    1PS> Invoke-Command -ScriptBlock { Get-Service | Select-Object -First 2 Name, Status }
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Name     Status
    ----     ------
    BITS     Running
    WinRM    Stopped

    Invoke-Command -ComputerName SRV01

    1PS> Invoke-Command -ComputerName SRV01,SRV02 -ScriptBlock { Get-Service Spooler }
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Status   Name     DisplayName
    -------  -------  -------------
    Stopped  Spooler  Print Spooler

    Invoke-Command -ComputerName SRV01 (2)

    1PS> $name = "Spooler"; Invoke-Command -ComputerName SRV01 -ScriptBlock { Get-Service $using:name }
    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

    PowerShell module reference on Microsoft Learn

    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.

    Invoke-Command reference on Microsoft Learn

    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.

    Review the lesson

    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
    Genie guideInvoke-Command

    Use Invoke-Command to run a command or script block on one or more remote computers and return the results. The console below is pre-loaded — run it, then change a value and run it again.