PowerShell Remote Sessions and Invoke-Command

PowerShell Remote Sessions:

PowerShell Remote Sessions and Invoke-Command: PowerShell Remoting lets you run PowerShell commands or access full PowerShell sessions on remote Windows systems. It’s similar to SSH for accessing remote terminals on other operating systems.

Enable PowerShell Remoting

Enable PowerShell Remoting on the PC You Want to Access Remotely
Your first step is to enable PowerShell Remoting on the PC to which you want to make remote connections. There are several methods to do this.

On that PC, you’ll need to open PowerShell with administrative privileges.
Enable-PSRemoting -Force

PS C:\> Enable-PSRemoting -Force

WinRM has been updated to receive requests.
WinRM service started.

WinRM has been updated for remote management.
Enabled remote access.​

winrm quickconfig

PS C:\> winrm quickconfig
WinRM is not set up to receive requests on this machine.
The following changes must be made:

Start the WinRM service.
Make these changes [y/n]? y
WinRM has been updated to receive requests.
WinRM service started.
WinRM is already set up for remote management on this computer.

PS C:\>​

Using server manager – GUI

Configure TrustedHosts:

Next, you need to configure the TrustedHosts setting on both the PC to which you want to connect and the PC (or PCs) you want to connect from, so the computers will trust each other. You can do this in one of two ways.

PS C:\Windows\system32> get-Item wsman:\localhost\client\trustedhosts


   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   TrustedHosts

There are no Trustedhosts defined here. If the target server is in the same domain, there is no need to cinfigure trustedhosts. It will be trusted automatically.

To add trusted hosts, use the command Set-Item wsman:\localhost\client\trustedhosts *

PS C:\Windows\system32> Set-Item wsman:\localhost\client\trustedhosts *

WinRM Security Configuration.
This command modifies the TrustedHosts list for the WinRM client. The computers in the TrustedHosts list might not be authenticated. The client might send credential information to these computers. Are you sure that you want to modify this list?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
PS C:\Windows\system32> get-Item wsman:\localhost\client\trustedhosts


   WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   TrustedHosts                                   *


PS C:\Windows\system32>

Now, once the configuration is completed successfully, restart WinRM service.

Restart-Service WinRM

Now you should be able to connect to the remote server.

PS C:\> Enter-PSSession -ComputerName vc01

[vc01]: PS C:\Users\wintel\Documents> hostname
vc01
[vc01]: PS C:\Users\wintel\Documents>

If you anticipate making multiple connections to a remote system, use the New-PSSession cmdlet to create a remote Windows PowerShell session. New-PSSession permits you to store the remote session in a variable, and it provides you with the ability to enter and leave the remote session as often as required, without the additional overhead of creating and destroying remote sessions.


PS C:\> $s = New-PSSession -ComputerName vc01
PS C:\> Invoke-Command -Session $s -ScriptBlock {hostname}
vc01
PS C:\> Invoke-Command -Session $s -ScriptBlock {hostname; Get-WmiObject win32_computersystem;}
vc01

Domain              : winadmin.org
Manufacturer        : VMware, Inc.
Model               : VMware Virtual Platform
Name                : VC01
PrimaryOwnerName    : Windows User
TotalPhysicalMemory : 8589328384
PSComputerName      : vc01

If the server is a non-domain server or other domain server, you should specify the Credentials.

PS C:\> $cred = Get-Credential
PS C:\> $s = New-PSSession -ComputerName vc01 -Credential $cred
PS C:\> Invoke-Command -Session $s -ScriptBlock {hostname; Get-WmiObject win32_computersystem;}
vc01

Domain              : winadmin.org
Manufacturer        : VMware, Inc.
Model               : VMware Virtual Platform
Name                : VC01
PrimaryOwnerName    : Windows User
TotalPhysicalMemory : 8589328384
PSComputerName      : vc01

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on skype
Share on telegram
Share on whatsapp
Share on email
Share on print

Leave a Reply

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

Post comment