powershellscripts.com

Tutorials  PowerShell Cmdlet Help for Get-PSSession



NAME
Get-PSSession

SYNOPSIS
Gets the Windows PowerShell sessions (PSSessions) in the current session.

SYNTAX
Get-PSSession [[-ComputerName] ] []

Get-PSSession [-Id] []

Get-PSSession [-InstanceId ] []

Get-PSSession [-Name ] []


DESCRIPTION
The Get-PSSession cmdlet gets the Windows PowerShell sessions (PSSessions) that were created in the current session
.

Without parameters, Get-PSSession gets all of the PSSessions created in the current session. You can use the parame
ters of Get-PSSession to get the sessions that are connected to particular computers, or you can identify sessions
by their names, IDs, or instance IDs.

For more information about Windows PowerShell sessions, see about_PSSessions.


PARAMETERS
-ComputerName
Gets only the PSSessions that are connected to the specified computers. Wildcards are permitted.

Type the NetBIOS name, an IP address, or a fully-qualified domain name of one or more computers. To specify the
local computer, type the computer name, "localhost", or a dot (.).

Required? false
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? true

-Id
Gets only the PSSessions with the specified IDs. Type one or more IDs (separated by commas), or use the range o
perator (..) to specify a range of IDs.

An ID is an integer that uniquely identifies the PSSession in the current session. It is easier to remember and
type than the InstanceId, but it is unique only within the current session. To find the ID of a PSSession, us
e Get-PSSession without parameters.

Required? true
Position? 1
Default value All sessions in the shell
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-InstanceId
Gets only the PSSessions with the specified instance IDs.

The instance ID is a GUID that uniquely identifies a PSSession on a local or remote computer. The InstanceID is
unique, even when you have multiple sessions running in Windows PowerShell.

The InstanceID is stored in the InstanceID property of the object that represents a PSSession. To find the Inst
anceID of the PSSessions in the current session, type "get-pssession | format-table Name, ComputerName, Instanc
eId".

Required? false
Position? named
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? true

-Name
Gets only the PSSessions with the specified friendly names. Wildcards are permitted.

To find the names of the PSSessions in the current session, type "get-pssession" without parameters.

Required? false
Position? named
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? true


This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer and OutVariable. For more information, type,
"get-help about_commonparameters".

INPUTS
None
You cannot pipe input to this cmdlet.


OUTPUTS
System.Management.Automation.Runspaces.PSSession
Get-PSSession returns a PSSession object for each PSSession that it gets.


NOTES


Get-PSSession gets the PSSessions that were created in the current session. It does not get the session that is
created when you open Windows PowerShell, and it does not get PSSessions that were created in other sessions o
r on other computers, even if they connect to the local computer.


-------------------------- EXAMPLE 1 --------------------------

C:\PS>get-pssession


Description
-----------
This command gets all of the PSSessions that were created in the current session.

It does not get PSSessions that were created in other sessions or on other computers, even if they connect to this
computer.





-------------------------- EXAMPLE 2 --------------------------

C:\PS>$s = get-pssession -computername Server02


Description
-----------
This command gets the PSSessions that are connected to the Server02 computer and saves them in the $p variable.





-------------------------- EXAMPLE 3 --------------------------

C:\PS>new-pssession -computername Server01, Server02, Server03

C:\PS> $s1, $s2, $s3 = get-pssession


Description
-----------
This example shows how to save the results of a Get-PSSession command in multiple variables.

The first command uses the New-PSSession cmdlet to create PSSessions on three remote computers.

The second command uses a Get-PSSession cmdlet to get the three PSSessions. It then saves each of the PSSessions in
a separate variable.

When Windows PowerShell assigns an array of objects to an array of variables, it assigns the first object to the fi
rst variable, the second object to the second variable, and so on. If there are more objects than variables, it ass
igns all remaining objects to the last variable in the array. If there are more variables than objects, the extra v
ariables are not used.





-------------------------- EXAMPLE 4 --------------------------

C:\PS>get-pssession | format-table -property computername, InstanceID

C:\PS> $s = get-pssession -InstanceID a786be29-a6bb-40da-80fb-782c67f7db0f

C:\PS> remove-pssession -session $s


Description
-----------
This example shows how to get a PSSession by using its instance ID, and then to delete the PSSession.

The first command gets all of the PSSessions on the local computer. It sends the PSSessions to the Format-Table cmd
let, which displays the ComputerName and InstanceID properties of each PSSession.

The second command uses the Get-PSSession cmdlet to get a particular PSSession and to save it in the $s variable. T
he command uses the InstanceID parameter to identify the PSSession.

The third command uses the Remove-PSSession cmdlet to delete the PSSession in the $s variable.





-------------------------- EXAMPLE 5 --------------------------

C:\PS>get-pssession -computername Serv*


Description
-----------
This command gets all the PSSessions that connect to computers that have computer names that begin with "Serv".





-------------------------- EXAMPLE 6 --------------------------

C:\PS>get-pssession -name Test*, Ux*


Description
-----------
This command gets PSSessions that have names that begin with "Test" or "Ux".





-------------------------- EXAMPLE 7 --------------------------

C:\PS>get-pssession 2


Description
-----------
This command gets the PSSession with ID 2.






RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=135219
about_PSSessions
about_Remote
New-PSSession
Remove-PSSession
Enter-PSSession
Exit-PSSession
Invoke-Command