Run PowerShell Commands on Remote Computers or TFS server.
PowerShell Remoting allows to run individual PowerShell commands/scripts or access full PowerShell sessions on
remote Windows systems. It is similar to SSH for accessing remote terminals on other operating systems.
PowerShell is locked-down (restricted) by default, so you need to enable PowerShell Remoting before using it.
This setup process is a bit more complex if you are using a workgroup - for e.g, on a home network —
instead of a domain.
Enabling PowerShell Remoting
On the computer you want to access remotely, open a PowerShell window as Administrator - right click
the PowerShell shortcut and select Run as Administrator.
To enable PowerShell Remoting, run the following command (known as a cmdlet in PowerShell):
Workgroup Setup
If your computers aren't on a domain - say, if you're doing this on a home network - you'll need to
perform a few more steps. First, run the Enable-PSRemoting -Force command on the computer you want to connect from, as well. (Remember to launch PowerShell as Administrator before running this command.)
On both computers, configure the TrustedHosts setting so the computers will trust each other.
For example, if you're doing this on a trusted home network, you can use this command to allow any computer to connect:
Testing the Connection
On the computer you want to access the remote system from, use the Test-WsMan cmdlet to test your
configuration.
This command tests whether the WinRM service is running on the remote computer -
if it completes successfully, you'll know that WinRM is enabled and the computers can communicate with each other.
Use the following cmdlet, replacing COMPUTER with the name of your remote computer:
Executing a Remote Command
To run a command on the remote system, use the Invoke-Command cmdlet.
The syntax of the command is as follows:
Example
PS> Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND }
-credential USERNAME
Example
PS> Invoke-Command -ComputerName MYMACHINENAME -ScriptBlock { Get-ChildItem C:\ }
-credential john
Starting a Remote Session
Use the Enter-PSSession cmdlet to start a remote PowerShell session,
where you can run multiple commands, instead of running a single command: