Bootstrap FreeKB - PowerShell - Configure PowerShell for Remote Connections
PowerShell - Configure PowerShell for Remote Connections

Updated:   |  PowerShell articles

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.

The Get-Service cmdlet can be used to determine if the winrm (Windows Remote Management) service is stopped or running.

PS C:\> Get-Service winrm

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

 

If the winrm service is stopped, the Start-Service cmdlet can be used to start the winrm service.

Start-Service winrm

 

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

 

 


New-PSSession can be used to connect to a Remote Windows computer.

New-PSSession -ComputerName server1.example.com -credential JohnDoe

 

Invoke-Command can be used to run one or more commands on a remote system.

Invoke-Command -computername server1.example.com -credential JohnDoe -command { Test-Path -Path 'C:\Temp' }

 

Remove-PSSession can be used to disconnect from a Remote Windows Computer.

Remove-PSSession 6f7985fc-6f62-48d0-bd4f-742f131081b3



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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