about_PSSession_Details
Applies To: Windows PowerShell 2.0
TOPIC
about_PSSession_Details
SHORT DESCRIPTION
Provides detailed information about Windows PowerShell sessions and the
role they play in remote commands.
LONG DESCRIPTION
A session is an environment in which Windows PowerShell runs. A session is
created for you whenever you start Windows PowerShell. You can create
additional sessions, called "Windows PowerShell sessions" or "PSSessions"
on your computer or another computer.
Unlike the sessions that Windows PowerShell creates for you, you control
and manage the PSSessions that you create.
PSSessions play an important role in remote computing. When you create a
PSSession that is connected to a remote computer, Windows PowerShell
establishes a persistent connection to the remote computer to support the
PSSession. You can use the PSSession to run a series of commands,
functions, and scripts that share data.
This topic provides detailed information about sessions and PSSessions
in Windows PowerShell. For basic information about the tasks that you
can perform with sessions, see about_PSSessions.
ABOUT SESSIONS
Technically, a session is an execution environment in which Windows
PowerShell runs. Each session includes an instance of the
System.Management.Automation engine and a host program in which Windows
PowerShell runs. The host can be the familiar Windows PowerShell console
or another program that runs commands, such as Cmd.exe, or a program built
to host Windows PowerShell, such as Windows PowerShell Integrated Scripting
Environment (ISE). From a Windows perspective, a session is a Windows
process on the target computer.
Each session is configured independently. It includes its own properties,
its own execution policy, and its own profiles. The environment that exists
when the session is created persists for its lifetime even if you change
the environment on the computer. All sessions are created in a global
scope, even sessions that you create in a script.
You can run only one command (or command pipeline) in a session at one
time. A second command run synchronously (one at a time) waits up to four
minutes for the first command to be completed. A second command run
asynchronously (concurrently) fails.
ABOUT PSSESSIONS
A session is created each time that you start Windows PowerShell. And,
Windows PowerShell creates temporary sessions to run individual commands.
However, you can also create sessions (called "Windows PowerShell sessions"
or "PSSessions") that you control and manage.
PSSessions are critical to remote commands. If you use the ComputerName
parameter of the Invoke-Command or Enter-PSSession cmdlets, Windows
PowerShell establishes a temporary session to run the command and then
closes the session as soon as the command or the interactive session
is complete.
However, if you use the New-PSSession cmdlet to create a PSSession, Windows
PowerShell establishes a persistent session on the remote computer in which
you can run multiple commands or interactive sessions. The PSSessions that
you create remain open and available for use until you delete them or until
you close the session in which they were created.
When you create a PSSession on a remote computer, the system creates a
PowerShell process on the remote computer and establishes a connection
from the local computer to the process on the remote computer. When you
create a PSSession on the local computer, both the new process and the
connections are created on the local computer.
WHEN DO I NEED A PSSESSION?
The Invoke-Command and Enter-PSSession cmdlets have both ComputerName and
Session parameters. You can use either to run a remote command.
Use the ComputerName parameter to run a single command or a series of
unrelated commands on one or many computers.
To run commands that share data, you need a persistent connection to the
remote computer. In that case, create a PSSession, and then use the Session
parameter to run commands in the PSSession.
Many other cmdlets that get data from remote computers, such as
Get-Process, Get-Service, Get-EventLog, and Get-WmiObject have only a
ComputerName parameter. They use technologies other than Windows PowerShell
remoting to gather data remotely. These cmdlets do not have a Session
parameter, but you can use the Invoke-Command cmdlet to run these commands
in a PSSession.
HOW DO I CREATE A PSSESSION?
To create a PSSession, use the New-PSSession cmdlet. You can use
New-PSSession to create a PSSession on a local or remote computer.
CAN I CREATE A PSSESSION ON ANY COMPUTER?
To create a PSSession that is connected to a remote computer, the computer
must be configured for remoting in Windows PowerShell. The current user
must be a member of the Administrators group on the remote computer, or
the current user must be able to supply the credentials of a member of
the Administrators group. For more information,
see about_Remote_Requirements.
CAN I SEE THE PSSESSIONS THAT OTHERS HAVE CREATED ON MY COMPUTER?
No. You can get and manage only the PSSessions that you have created in the
current session. You cannot see PSSessions that others have created, even
if they run commands on the local computer.
CAN I RUN A BACKGROUND JOB IN A PSSESSION?
Yes. A background job is a command that runs asynchronously in the
background without interacting with the current session. When you submit
a command to start a job, the command returns a job object, but the job
continues to run in the background until it is complete.
To start a background job on a local computer, use the Start-Job command.
You can run the background job in a temporary connection (by using the
ComputerName parameter) or in a PSSession (by using the Session parameter).
To start a background job on a remote computer, use the Invoke-Command
cmdlet with its AsJob parameter, or use the Invoke-Command cmdlet to run a
Start-Job command on a remote computer. When using the AsJob parameter,
you can use the ComputerName or Session parameters.
When using Invoke-Command to run a Start-Job command, you must run the
command in a PSSession. If you use the ComputerName parameter, Windows
PowerShell ends the connection when the job object returns, and the job is
interrupted.
For more information, see about_Jobs.
CAN I RUN AN INTERACTIVE SESSION?
Yes. To start an interactive session with a remote computer, use the
Enter-PSSession cmdlet. In an interactive session, the commands that you
type run on the remote computer, just as if you typed them directly on the
remote computer.
You can run an interactive session in a temporary session (by using the
ComputerName parameter) or in a PSSession (by using the Session parameter).
If you use a PSSession, the PSSession retains the data from previous
commands, and the PSSession retains any data generated during the
interactive session for use in later commands.
When you end the interactive session, the PSSession remains open and
available for use.
For more information, see Enter-PSSession and Exit-PSSession.
MUST I DELETE THE PSSESSIONS?
Yes. A PSSession is a process, which is a self-contained environment that
uses memory and other resources even when you are not using it. When you are
finished with a PSSession, delete it. If you create multiple PSSessions,
close the ones that you are not using, and maintain only the ones currently
in use.
To delete PSSessions, use the Remove-PSSession cmdlet. It deletes the
PSSessions and releases all of the resources that they were using.
You can also use the TimeOut parameter of New-PSSession to close an idle
PSSession after an interval that you specify. For more information,
see New-PSSession.
If you do not delete the PSSession or set a time-out, the PSSession remains
open and available for use until you close it, until you close the session
in which it was created, or until you exit Windows PowerShell. However, a
PSSession on a remote computer will be disconnected if the remote computer
does not respond for four minutes. (The remote computer is configured to
send a heartbeat pulse every three minutes.)
If you save a PSSession object in a variable and then delete the PSSession
or let it time out, the variable still contains the PSSession object, but
the PSSession is not active and cannot be used or repaired.
ARE ALL SESSIONS AND PSSESSIONS ALIKE?
No. Developers can create custom sessions that include only selected
providers and cmdlets. If a command works in one session but not in
another, it might be because the session is restricted.
SEE ALSO
about_Jobs
about_PSSessions
about_Remote
about_Remote_Requirements
Invoke-Command
New-PSSession
Get-PSSession
Remove-PSSession
Enter-PSSession
Exit-PSSession