If you need to run commands or scripts against one or more remote computers, you can use PowerShell Remoting. In this article, we will look at several examples of how to use PowerShell Remoting interactive session mode and persistent connections to run PS1 a script on a remote computer.
Configuring WinRM for Remote PowerShell Connections
To connect to a remote computer using a PowerShell session, WinRM (Windows Remote Management Service) must be enabled and configured. Keep in mind that PowerShell Remoting is not configured by default on most Windows client OSs and must be enabled before you can use remote PowerShell connections.
The following command allows you to quickly enable and configure WinRM on the computer:
Enable-PSRemoting -Force

Note. You should use the following command to enable WinRM on Windows computers with the Public network connection profile:
Enable-PSRemoting โSkipNetworkProfileCheck -Force
Check that the WinRM service is running by using the Get-Service command:
Get-Service WinRM

Check that the Windows Defender rule that allows remote connection via WinRM is enabled (by default HTTP port 5985 is used for connection):
Get-NetFirewallRule -Name "WINRM-HTTP-In-TCP-NoScope"|select name, Enabled

Workgroup computers should have another Windows Firewall rule enabled that allows inbound WinRM traffic:
Set-NetFirewallRule -Name "WINRM-HTTP-In-TCP-PUBLIC" -RemoteAddress Any -Enabled true
Now you test the WinRM connectivity to target computer using the command:
Test-WSMan wks11

If you get a reply, then the remote computer can be accessed using PowerShell Remoting.
PowerShell 7 and SSH-based remoting. The examples we are showing in this article use the traditional WinRM-based PowerShell Remoting transport, which remains the standard management method for Windows environments. However, PowerShell 7 also supports SSH-based remoting, which can be useful in cross-platform environments and when managing Linux systems. For example:
Invoke-Command -HostName server01 -UserName admin -ScriptBlock { Get-Process }PowerShell 7 supports both WinRM-based and SSH-based remoting. In most Windows enterprise environments, WinRM remains the preferred option because it integrates with AD, Kerberos authentication, Group Policy, and Just Enough Administration (JEA).
In this case, you need to install an HTTPS certificate for PowerShell Remoting on the remote computer (the long way), or add this host to the trusted ones on your management computer:
Set-Item WSMan:\localhost\Client\TrustedHosts `
-Concatenate `
-Value "192.168.1.70"
You should only add trusted hosts that you control. TrustedHosts disables mutual identity verification and you should not use it as a substitute for proper Kerberos/HTTPS config.
Then restart the WinRM service:
Restart-Service WinRM
Hint. If you use an IP address (instead of a name) to connect to a remote computer via PS Remoting, you may get an error:
Connecting to remote server 192.168.1.70 failed with the following error message: The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided.
Using PowerShell Remoting Interactive Mode to Run Remote Commands
You can run any command on a remote host in real time by using an interactive PowerShell remoting session. Specify a remote host name (wks11 in this example) in the Enter-PSSession command:
Enter-PSSession wks11
The PowerShell prompt will change. The name of the remote computer you are connected to now appears at the beginning of the command prompt. You can run any command that will be executed on the remote host and return the result back to your command prompt.
To end the remote interactive PowerShell session:
Exit-PSSession

Only simple management tasks are typically performed on remote computers in interactive mode. To run a complex command or to run the PowerShell script file (PS1) remotely, use the Invoke-Command cmdlet.
Using Invoke-Command to Run PowerShell Scripts Remotely
The Invoke-Command cmdlet allows you to connect to a remote computer using PowerShell Remoting and run the block of commands specified in the ScriptBlock parameter:
Invoke-Command -ScriptBlock {Restart-Service spooler} -ComputerName wks11 To run multiple PowerShell commands from a ScriptBlock parameter, separate them with semicolons. The commands you specify will be executed sequentially, one after the other, on the remote computer:
Invoke-Command -ScriptBlock {
Restart-Service spooler;
Get-Date;
Get-CimInstance Win32_QuickFixEngineering
} -ComputerName wks11 
Note. Older examples may use the WMIC utility (for example, wmic qfe list) to retrieve installed updates. WMIC has been deprecated by Microsoft and is removed from modern Windows releases. You should use Get-HotFix/Get-CimInstance Win32_QuickFixEngineering instead.
PowerShell allows you to run local PS1 scripts on remote computers. Specify the full path to the PS1 script file in the -FilePath parameter. To run a local PowerShell script against three remote computers:
Invoke-Command -FilePath c:\ps\tune.ps1 -ComputerName server1,server2,server3

Note that you do not need to manually copy the PS1 script file to the remote computers. PowerShell Remoting passes the script code to the remote session and runs it.
You can save the list of computers to a text file and run the PowerShell script remotely on all the computers at once.
Invoke-command -ComputerName (get-content c:\ps\servers.txt) -filepath c:\ps\tune.ps1
When running commands against dozens/hundreds of remote machines, you may want to limit the number of concurrent remote connections. To do this, use the ThrottleLimit parameter to control how many remote operations run simultaneously:
$Servers = Get-Content C:\PS\servers.txt
Invoke-Command `
-ComputerName $Servers `
-ScriptBlock { Get-Service } `
-ThrottleLimit 20
In the example above, PowerShell processes a maximum of 20 remote machines at the same time. You can adjust the value based on the available network bandwidth, WinRM capacity, and server resources.
When executing commands against a large number of remote machines, you can also run the operation as a background job. This allows PowerShell to return control immediately while the remote commands continue to execute. Here is an example:
Invoke-Command `
-ComputerName server1,server2 `
-ScriptBlock { Get-Service } `
-AsJob
You can use Get-Job to monitor the job status and Receive-Job to retrieve the results after completion:
Get-Job
Receive-Job -Id 1
If you need to run PowerShell scripts with credentials other than the current user, you can get the user’s credentials and save them in a variable:
$cred = Get-Credential Invoke-Command -FilePath c:\ps\tune.ps1 -ComputerName server1 -Credential $cred

To pass local variables to a remote PowerShell session, use the $Using prefix:
$name="script1.ps1"
Invoke-Command -ComputerName dc03 -Scriptblock{write-host $using:Name} Using Persistent Remote PowerShell Connections (Sessions)
A new session is created with the remote computer each time you run Invoke-Command. This takes time and system resources. Use the New-PSSession cmdlet to create persistent PowerShell sessions with a remote computer.
For example, letโs create sessions with three computers and save them in the $PSSess variable:
$PSSess = New-PSSession -ComputerName server1, server2, server3
Once you have established a session, you can use it to run commands and scripts. For example, the following command will get a list of processes on remote computers and save them in the $RemoteProcesses variable:
Invoke-Command -Session $PSSess {
Get-Process
} Now you can use this variable in other commands in the same sessions.

The persistent remote PowerShell session remains active until you close the PowerShell console/ explicitly remove the session by using the Remove-PSSession cmdlet.
Note that PowerShell also supports disconnected sessions through the Disconnect-PSSession cmdlet. This feature allows you to disconnect from a remote session while keeping it running on the remote machine and reconnect later. Keep in mind that disconnected sessions are only supported in specific remoting cases and are not commonly used for day-to-day admin tasks.
Or, you can remotely run a PS1 script file when WinRM is not available:
psexec -s \\SrvNY01 Powershell -ExecutionPolicy Bypass -File \\dc1\netlogon\scripts\Get-Info.ps1
As you can see, PowerShell provides a wide range of options for running scripts and commands on remote computers.
Security note. In highly secured environments, you should consider using Just Enough Administration (JEA) to limit admin permissions for remote PowerShell sessions. JEA allows you to delegate specific management tasks without granting full admin access to remote machines.
Hint. If you canโt enable and configure WinRM on the remote host, you can execute PowerShell commands against a remote computer using the PsExec tool. For example, run several PowerShell commands in sequence:
psexec.exe \\SrvNY01 powershell.exe -Command "& { Get-Process; Get-Service; Get-CimInstance Win32_OperatingSystem | Select-Object LastBootUpTime }"
What is PowerShell Remoting?
PowerShell Remoting is a feature that allows administrators to run commands and scripts on remote computers using PowerShell sessions. It is typically based on Windows Remote Management (WinRM).
What is required to run PowerShell scripts remotely?
The target computer must have WinRM enabled and configured. Network connectivity, firewall rules, and appropriate permissions are also required.
Which network port does PowerShell Remoting use?
By default, WinRM uses:
- TCP 5985 for HTTP
- TCP 5986 for HTTPS
Why do I get a WinRM authentication error when connecting by IP address?
Kerberos authentication requires hostnames. When connecting by IP address, you must either:
- Use HTTPS transport, or
- Add the target system to TrustedHosts and provide explicit credentials.
What are disconnected PowerShell sessions?
Disconnected sessions allow you to leave a remote session running on the target computer and reconnect later using Disconnect-PSSession and Connect-PSSession.
