Run Commands In Local and Remote System With Powershell Invoke-Command Cmdlet – POFTUT

Run Commands In Local and Remote System With Powershell Invoke-Command Cmdlet


Running commands in local and remote systems have different methods. We can use psexec , commands own feature or Invoke-Command cmdlet. In this tutorial we will look how to use Invoke-Command in powershell. Invoke-Command runs commands remotely and returns the output like success or error messages.

Help

Detailed help about the Invoke-Command can be get like below.

PS> Run Powershell Script On Remote Computer
Help
Help

Also more detailed help can be get by adding -full option like below. This will provide enormous output and provide help like parameters details, usage examples etc.

PS> Get-Help Invoke-Command -full
Help
Help

Enable PSRemoting

In order to run Powershell command on the remote systems the remote systems should be configured to accept remote powershell commands via PSRemoting . Following post shows how to enable PSRemoting in windows operating systems.

How To Enable Powershell Remoting PSRemoting or WinRM?

Run Command On Remote Computer

One of the most used example of Invoke-Command is running command on remote systems. We will specify following parameters to run command remotely.

  • -ComputerName specifies the host name or IP address of the remote system
  • Credentials specifies the remote system login user with domain or computer name
  • -ScriptBlock specifies the command block that will be run on remote system in curly braces.
PS> Invoke-Command -ComputerName  192.168.122.66 -Credential Administrator  -ScriptBlock {Get-Culture}
Run Command On Remote Computer
Run Command On Remote Computer

If we do not provide the password or any credentials Powershell will currently available local credentials to authentication remote system.

Run Command On Remote Computer
Run Command On Remote Computer

As we can see from the given command output it is executed successfully on the remote system and the response is printed in the local system shell.

Run Powershell Script On Remote Computer

In previous example we provided a shell command to run on the remote system. Powershell also have the feature to run local powershell scripts on the remote system. We will provide the Powershell File to run content on the remote system.We have following Powershell script file named myscript.ps1

Get-Host
Get-Culture

We will use -FilePath option to provide script file path like below.

PS> Invoke-Command -ComputerName 192.168.122.66 -Credential Administrator  -FilePath c:\myscript.ps1
Run Powershell Script On Remote Computer
Run Powershell Script On Remote Computer

We can see from output that provided script file commands are executed in a row on the given remote system.

LEARN MORE  How to Setup Kubernetes 1.4 on Ubuntu

Run Command On Multiple Remote Computers

Now previous examples were useful but less effective because we have only provided single remote system. The power of the Invoke-Command comes from running commands multiple remote systems. We will specify more than one remote system in this step. We will use same syntax with previous commands but add more remote system IP address or hostname to the -ComputerName option by separating them with comma. We will provide remote system IP address as 192.168.122.66 and hostname localhost . We can add more remote systems just separating comma again

PS> Invoke-Command -ComputerName 192.168.122.66 , localhost -Credential Administrator  -ScriptBlock {Get-Culture}
Run Command On Multiple Remote Computers
Run Command On Multiple Remote Computers

We can see the out that each remote system output is listed as a new line in the same table and columns. This type of output makes filtering and searching easier and more readable.

Run Powershell Script On Multiple Remote Computer

We will merge previous examples and run given Powershell script file on the multiple remote systems. The command will be as you expect like below.

PS>  Invoke-Command -ComputerName 192.168.122.66 , localhost -Credential Administrator  -FilePath C:\myscript.ps1
Run Powershell Script On Multiple Remote Computer
Run Powershell Script On Multiple Remote Computer

Run Command From Local Variable On Remote Computer

Another useful resource for command to be run on the remote system is Powershell local variables. We can use Powershell local variable in the command block like a regular powershell command. We will use ScriptBlock again but we will specify intended variable name by prefixing with $Using: keyword. In this example we create a local variable name $dir and set its value Get-Host . Then we use this variable in ScriptBlock as stated previously.

PS> $dir="C:\"

We have setup our local variable which is the path we want to list files and folder.

PS> Invoke-Command -ComputerName 192.168.122.66  -Credential Administrator  -ScriptBlock {Dir $Using:dir}
Run Command From Local Variable On Remote Computer
Run Command From Local Variable On Remote Computer

As we can see remote systems C:\ path is listed as expected.

1 thought on “Run Commands In Local and Remote System With Powershell Invoke-Command Cmdlet”

Leave a Comment