How to Run PowerShell Script on Remote Computer?

The functionality of remote command execution in PowerShell is called PowerShell Remoting (appeared in PowerShell 2.0) and is based on the capabilities of the Web Services for Management protocol (WS-Management). With PowerShell Remoting, you can run commands on one or several remote computers. You can use the interactive session mode with remote computers, temporary or permanent connection. In this article, we will take a look at several examples on how to execute PowerShell script remotely.

To connect to a computer remotely via PowerShell, the WinRM (Windows Remote Management service) must be enabled and configured on it (it is disabled by default). Communication between computers is performed over HTTP or HTTPS protocols, and all network traffic between computers is encrypted. You can use NTLM and Kerberos to authenticate on a remote computer.

To check the status of the WinRM service, run the Get-service command:

get-service winrm

run powershell script on remote computer

As you can see, the WS-Management service is running.

To interactively connect to a remote Server1 computer via PowerShell, run the following command:

Enter-PSSession Server1

The PoSh CLI view will change. At the beginning of the line there will be present the name of the remote computer to which you are connected via WinRM. After the session is established, all commands that are being entered in the PowerShell console are executed on the remote computer. PS Remoting works as follows: the commands entered on the local computer are transmitted to the remote computer and are executed there, then the result is transmitted back. Since all commands are executed locally, there is no need to worry about compatibility with PoSh version and modules.

READ ALSO  How to Create a GUI for PowerShell Scripts?

To end an interactive session run the command:


run powershell script remotely

Only the simple remote control tasks are typically performed on computers in the interactive mode. To run a complex command or run the PowerShell script remotely, use should the Invoke-Command cmdlet.

The following command will create a remote connection with the computer Server1 and run the block of commands specified in the ScriptBlock parameter. After that, the remote session will automatically close.

Invoke-Command -ScriptBlock {Restart-Service spooler} -ComputerName server1

You can run the task in the background by running Invoke-Command with the -AsJob parameter. But in this case, the command will not return the result to the PoSh console. To obtain background job information, use the Receive-Job cmdlet.

powershell run script on remote computer

PowerShell allows you to run local PS1 scripts on remote computers. The idea is that you store all PowerShell instructions in a local .PS1 file on your computer. To do this, use the -FilePath parameter in the Invoke-Command cmdlet instead of -ScriptBlock. For example, to run the c:\ps\tune.ps1 script on three remote servers, you can use the following command:

Invoke-Command -FilePath c:\ps\tune.ps1 -ComputerName server1,server2,server3

execute powershell script remotely

The main advantage of this way of running PowerShell scripts is that you don’t need to copy the script file to remote computers. You can use not only the local script, but also the PS script in a network shared folder that can be accessed from the local computer.

READ ALSO  Configure Legal Notices on Domain Computers using Group Policy

You can save the list of computers in a text file and execute PowerShell script remotely on all computers:

Invoke-command -comp (get-content c:\ps\servers.txt) -filepath c:\ps\tune.ps1

By default, it sends the PS1 script to 32 remote computers from the list at the same time. If there are more than 32 computers, then PoSh monitors the execution status of the script on the first 32 computers, and if the script is completed, the command is executed on the next computer. With the ThrottleLimit parameter, you can increase this limit, but be careful not to overload your network.

Cyril Kardashevsky
Latest posts by Cyril Kardashevsky (see all)


  1. Hi,
    My requirement is to delete few folders from Windows Server1 by executing script from Windows Server2.
    I tried running the Enter-PSSession Server1 and Invoke-Command but I get a permission denied error as shown below:
    You do not have sufficient access rights to perform this operation.
    + CategoryInfo : PermissionDenied [Remove-Item], IOException
    Can you please help?


  2. In this command

    Invoke-Command -FilePath c:pstune.ps1 -ComputerName server1,server2,server3

    Can you use a UNC path for the ps1 file?

    i.e. Invoke-Command -FilePath \\server0\share0\folder0\pstune.ps1 -ComputerName server1,server2,server3


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.