How to make a remote connection using Windows PowerShell

Home > Search > How-to
  by

Let's say we have two Windows computers in our network, PC1 and PC2, and we want to be able to use PC1 to make a remote connection to PC2 using Windows PowerShell. This is often referred to as PowerShell Remoting, which lets you execute PowerShell commands on a remote computer.


Configure PC1 and PC2 for remoting

Ensure the WinRM service is Runnning. If the WinRM service is not running, Start-Service WinRM.

PS C:\> Get-Service WinRM

Status    Name     DisplayName
------    ----     -----------
Running   winrm    Windows Remote Management

 

Configure WinRM to for remote management using the Set-WSManQuickConfig command.

PS C:\> Set-WSManQuickConfig

WinRM Quick Configuration
. . .
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
WinRM is set up to receive requests on this machine.
WinRM is set up for remote management on this machine.

 

Configure WinRM for remote management using the Enable-PSRemoting command:

PS C:\> Enable-PSRemoting

WinRM Quick Configuration
. . .
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
WinRM is set up to receive requests on this machine.
WinRM is set up for remote management on this machine.

 

Configure WinRM to trust the target computer. Hostname can be a single PC, numerous PCs, or a wildcard.

PS C:\> Set-Item WSMan:\localhost\Client\TrustedHosts -Value "hostname" -Force

 

 


Connect to target PC

  1. Type Enter-PSSession -computername target_hostname -credential username (example: Enter-PSSession -computername Server2 -credential Administrator)
    • Replace target_hostname with the hostname of the target computer
    • Replace username with the username of the target computer
  2. You will be presented with a pop-up box that lists the username and asks for the password. Enter the password and press enter.
  3. Once connected to the remote computer, the name of the remote computer will display in the current PowerShell console. You can now run the script as if you were physically at the remote computer.
  4. Type exit to disconnect from the remote computer

When writing a script using PowerShell ISE, the following can be used so that the interactive pop-up box to authenticate does not appear.

$password = ConvertTo-SecureString "SuperSecretPassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("username", $password )

Enter-PSSession -computername target_hostname -credential $cred

 


Send command to target PC

  1. Type Invoke-Command -computername1 hostname,-computername2 hostname,-computername3 hostname -credential hostname -command {the-command}
    • Replace computername1,computername2,computername3 with the names of the target computers. You can list as many target computers as you would like.
    • Replace hostname with the name of the local computer
    • Replace username with the username of the target computer
    • Replace the-command with the command you wish to run on the target computers

Note: If you plan to run remote commands on remote computers often, instead of having to manually type the remote computer names, you can store the remote computer names in a text file, and then using the following syntax to get the computer names from the text file: Invoke-Command -computername (Get-Content computer-names.txt)  -command {hostname}

A variable can be used for the username and password of the target PC.

$password = ConvertTo-SecureString "SuperSecretPassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("username", $password )

Invoke-Command -computername hostname -credential $cred -command {the-command}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments