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

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/run-commands-local-remote-system-powershell-invoke-command-cmdlet/
PINTEREST
PINTEREST
INSTAGRAM

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.

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.

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.

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.

LEARN MORE  How To Logoff From Command Line In Windows

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

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

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.

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

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.

LEARN MORE  Remote Desktop Connection Manager (RDCMan) Tutorial

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.

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.

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

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.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/run-commands-local-remote-system-powershell-invoke-command-cmdlet/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

1 Response

  1. 22/07/2017

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

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)