Updated March 24, 2023
Introduction to PowerShell Invoke-Command
Invoke-Command is used to run any command on a local or remote computer and return its output. It allows us to write a script or a command block and invoke that script or command block. It will return the result of command execution. This command can be run on local or remote. Let us understand it by some basic example. In very simple if We have some commands {command1 command 2 commands 3} and we want to execute them and get out of their execution than we can use PowerShell Invoke-Command.
Syntax of PowerShell Invoke-Command
Execute the below command and you will get the output as all its syntax and parameters. We show all the available syntax for PowerShell Invoke-Command. Every syntax will be used for particular situations.
Syntax:
Get-Help Invoke-Command -full
Below is the syntax we got after running the above command.
1. With Passing Argument to a Remote Computer
Invoke-Command
[-ScriptBlock] <scriptblock>
[-NoNewScope]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
2. With Defining Job Names
Invoke-Command
[[-Session] <PSSession[]>]
[-FilePath] <string>
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <understandable user defined names>]
[-RemoteDebug] [-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
3. Running the Commands in Remote Debugging Mode
Invoke-Command
[[-Session] <PSSession[]>]
[-ScriptBlock] <scriptblock>
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <string>]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[[-ComputerName] <string[]>]
[-FilePath] <string>
[-Credential <pscredential>]
[-Port <int>] [-UseSSL]
[-ConfigurationName <string>]
[-ApplicationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-SessionName <string[]>]
[-HideComputerName]
[-JobName <string>]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[[-ComputerName] <string[]>]
[-ScriptBlock] <scriptblock>
[-Credential <pscredential>]
[-Port <port where remote computer is listening>]
[-UseSSL]
[-ConfigurationName <string>]
[-ApplicationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-SessionName <string[]>]
[-HideComputerName]
[-JobName <string>]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[-CertificateThumbprint <string>]
[<CommonParameters>]
Invoke-Command
[[-ConnectionUri] <uri[]>]
[-ScriptBlock] <scriptblock>
[-Credential <pscredential>]
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-HideComputerName]
[-JobName <string>]
[-AllowRedirection]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[-CertificateThumbprint <string>]
[<CommonParameters>]
Invoke-Command
[[-ConnectionUri] <uri[]>]
[-FilePath] <string>
[-Credential <pscredential>]
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-HideComputerName]
[-JobName <string>]
[-AllowRedirection]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <Object[]>]
[<CommonParameters>]
Invoke-Command
[-VMId] <guid[]>
[-ScriptBlock] <scriptblock>
-Credential <pscredential>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-ScriptBlock] <scriptblock>
-Credential <pscredential>
-VMName <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-VMId] <guid[]>
[-FilePath] <string>
-Credential <pscredential>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-FilePath] <path of the file>
-Credential <pscredential>
-VMName <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-ScriptBlock <scriptblock>
-HostName <string[]>
[-Port <port number where remote computer is listening>]
[-AsJob]
[-HideComputerName]
[-UserName <string>]
[-KeyFilePath <string>]
[-SSHTransport {true}]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[-Subsystem <string>]
[<CommonParameters>]
Invoke-Command
[-ScriptBlock] <scriptblock>
-ContainerId <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <string>]
[-RunAsAdministrator]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-FilePath] <path of the file>
-ContainerId <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <string>]
[-RunAsAdministrator]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-ScriptBlock <scriptblock>
-SSHConnection <hashtable[]>
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-FilePath <path of the file>
-HostName <string[]>
[-AsJob]
[-HideComputerName]
[-UserName <string>]
[-KeyFilePath <string>]
[-SSHTransport {true}]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-FilePath <path of the file>
-SSHConnection <hashtable[]>
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Top 18 Parameters of PowerShell Invoke-Command
The top 18 parameters of PowerShell invoke-command explained below.
1. AllowRedirection: Sometime when you are trying to access the remote computer, it is possible that the remote computer returns another URI for alternate redirection. Remember PowerShell will redirect automatically if we want to redirect to the remote return URL than we should use this parameter.
2. ApplicationName: Here we can define the application name of our ConnectionURI. We should use this parameter only if we are not using ConnectionURI.
3. ArgumentList: These are the variables that we can pass to remote commands, as remote commands variables will be replaced with these variables. Pass the arguments with comma-separated.
4. AsJob: Suppose you are running some commands on the remote computers and that command or job is taking too much time, in that case, AsJob command will return some object giving the details of the running job. Simple it will allow us to run some background in case if the previous job is taking extensive time to execute.
5. Authentication: This defines a mechanism for authentication for accessing remote computers. But it is only available for Windows, Vista and Windows Server 2008.
6. CertificateThumbprint: It allows us to use some digital public key certificate for establishing a connection with remote computers.
7. ComputerName: This command defined the computers where we are going to run our commands. If we do not define –ComputerNameit will run the command on the local computer.
8. ConnectionUri: Defines a Uniform Resource Identifier (URI), which contains the endpoint of the session.
Below is the format for that:
<Transport>://<ComputerName>:<Port>/<ApplicationName>
The default value will be like:
http://localhost:8081/Ranjan
In case you do not have endpoint you can use UseSSL and Port to define your connection URI values.
9. ContainerId: Defines an array of IDs.
10. Credential: Display all user accounts which have permission to do certain actions. If we do not define any then it will give the current user only.
11. EnableNetworkAccess: Suppose we want to copy some XML files from one computer (one session) and paste it to a local computer then we can use this command. It adds a security token to loopback session which gets data from another session.
12. FilePath: If we want to access a file or script on the remote computer then we can use this command. We just have to specify the path for it.
13. HostName: Specifies an array of computer names for a Secure Shell (SSH) based connection. This is similar to the ComputerName parameter except that the connection to the remote computer is made using SSH rather than Windows WinRM.
14. JobName: It allows us to write user-friendly job names. Its simple syntax is Job<name>.
15. NoNewScope: It defines that given cmdlet will run in the current scope, if we do not specify this parameter then it will run in its own scope.
16. Port: It defines the port on which a remote computer is running. A remote port is a user remote computer. So to connect any computer we need to get a remote computer port on which it is listening.
17. RemoteDebug: It allows us to put some debugger on the remote computer for commands which we are going to execute for checking or debugging.
18. UserName: Defines the username for a remote account for which we are running the commands So for example if for are running the command for remote computer XYX than there must be a user with “XYZuser”.
Examples to Implement PowerShell Invoke-Command
Following are different examples to implement PowerShell invoke-command.
Example #1
Invoke-Command -ScriptBlock {Get-Process}
Output:
We can run the same command on remote computers like,
Invoke-Command -ComputerName “name of host or remote computer”-ScriptBlock {$p = Get-Process PowerShell}
Example #2
Below is the example for a local computer for getting a version of computers.
invoke-command -scriptblock {(get-host).version}
Output:
For a remote computer,
$version = Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {(Get-Host).Version}
Example #3
In the below example, we are putting Get-Host inside curly braces and on executing this command we are getting output as complete host details.
Invoke-Command -ScriptBlock {Get-Host}
Output:
Example #4
In the below example, we are putting Get-Culture inside curly braces and on executing this command we are getting the output.
Invoke-Command -ScriptBlock {Get-Culture}
Output:
If you want to run the same command for any remote computers you can use,
Invoke-Command -ComputerName “remote computer name” -Credential domain\username -ScriptBlock {Get-Culture}
This command is also available for Linux and macOS. So if anyone of you is using Linux or macOS you can follow Documentations for that.
Conclusion
In case of certain defined commands(block of commands) which we want to run on either on local(same computer) or on the remote computer, we can use Invoke-Command.
Recommended Articles
This is a guide to PowerShell Invoke-Command. Here we discuss the introduction and top 18 parameters of PowerShell invoke-command with its examples. You may also look at the following articles to learn more-
- Parameters of PowerShell Move-Item
- How does Echo Work in Shell Scripting?
- Architecture of Kubernetes
- Examples of Powershell Write-Host
- PowerShell Get-Location | Syntax | Parameters
- Guide to PowerShell Test-Path
- PowerShell New-Item | Examples
- Guide to PowerShell Rename-Item
- Examples of PowerShell Get-Content
- Examples of User Defined Data Types in C++