Home > Sample chapters > Programming > Visual Studio and .NET

Using Windows PowerShell Remoting

Troubleshooting Windows PowerShell remoting

The first tool to use to see if Windows PowerShell remoting is working or not is the Test-WSMan cmdlet. Use it first on the local computer (no parameters are required). The following example shows the command and associated output:

PS C:\> Test-WSMan


wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor   : Microsoft Corporation
ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0

To test a remote computer, specify the -ComputerName parameter. The following example shows the command running against a Windows Server 2012 domain controller named dc3:

PS C:\> Test-WSMan -ComputerName dc3


wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor   : Microsoft Corporation
ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0

However, the Test-WSMan cmdlet also works against a computer running Windows PowerShell 2.0. The following example shows the command running against a Windows Server 2008 domain controller named dc1:

PS C:\> Test-WSMan -ComputerName dc1


wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor   : Microsoft Corporation
ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 2.0

To examine a specific Windows PowerShell session, use the Get-PSSession cmdlet. The easiest way to do this is to pipeline the variable containing the Windows PowerShell session to the Get-PSSession cmdlet. The key items to pay attention to are the computer name, the state of the session, and the availability of the session. The following example shows this technique:

PS C:\> $ps | Get-PSSession

 Id Name            ComputerName    State         ConfigurationName     Availability
 -- ----            ------------    -----         -----------------     ------------
  3 Session3        ex1             Opened        Microsoft.PowerShell     Available
  4 Session4        dc3             Opened        Microsoft.PowerShell     Available

To focus on a specific session, reference the session by either ID or by Name. Send the returned session object over the pipeline to the Format-List cmdlet and select all the properties. The following example shows this technique (using fl as an alias for the Format-List cmdlet):

PS C:\> Get-PSSession -Name Session4 | fl *


State                  : Opened
IdleTimeout            : 7200000
OutputBufferingMode    : None
ComputerName           : dc3
ConfigurationName      : Microsoft.PowerShell
InstanceId             : c15cc80d-64f0-4096-a010-0211f0188aec
Id                     : 4
Name                   : Session4
Availability           : Available
ApplicationPrivateData : {PSVersionTable}
Runspace               : System.Management.Automation.RemoteRunspace

You can remove a remote Windows PowerShell session by pipelining the results of Get-PSSession to the Remove-PSSession cmdlet, as shown in the following example:

Get-PSSession -Name Session4 | Remove-PSSession

You can also remove a PS session directly by specifying the name to the Remove-PSSession cmdlet, as shown in the following example:

Remove-PSSession -Name session3