Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

131 changes: 71 additions & 60 deletions reference/7.5/Microsoft.PowerShell.Core/About/about_PSSessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ title: about_PSSessions

## Short description

Describes PowerShell sessions (PSSessions) and explains how to
establish a persistent connection to a remote computer.
Describes PowerShell sessions (PSSessions) and explains how to establish a
persistent connection to a remote computer.

## Long description

To run PowerShell commands on a remote computer, you can use the
**ComputerName** parameter of a cmdlet, or you can create a PowerShell
session (PSSession) and run commands in the PSSession.
**ComputerName** parameter of a cmdlet, or you can create a PowerShell session
(PSSession) and run commands in the PSSession.

When you create a PSSession, PowerShell establishes a persistent
connection to the remote computer. Use a PSSession to run a series of related
commands on a remote computer. Commands that run in the same PSSession can
share data, such as the values of variables, aliases, and functions.
When you create a PSSession, PowerShell establishes a persistent connection to
the remote computer. Use a PSSession to run a series of related commands on a
remote computer. Commands that run in the same PSSession can share data, such
as the values of variables, aliases, and functions.

You can also create a PSSession on the local computer and run commands in it.
A local PSSession uses the PowerShell remoting infrastructure to
create and maintain the PSSession.
A local PSSession uses the PowerShell remoting infrastructure to create and
maintain the PSSession.

Beginning in Windows PowerShell 3.0, PSSessions on Windows are independent
of the sessions in which they are created. Active PSSessions are maintained
Expand All @@ -36,36 +36,33 @@ on a remote Windows computer and reconnect to it at a later time from the
same computer or from a different Windows computer.

This topic explains how to create, use, get, and delete PSSessions. For more
advanced information, see
[about_PSSession_Details](about_PSSession_Details.md).
advanced information, see [about_PSSession_Details][01].

Note: PSSessions use the PowerShell remoting infrastructure. To use
PSSessions, the local and remote computers must be configured for remoting.
For more information, see
[about_Remote_Requirements](about_Remote_Requirements.md).
Note: PSSessions use the PowerShell remoting infrastructure. To use PSSessions,
the local and remote computers must be configured for remoting. For more
information, see [about_Remote_Requirements][02].

In Windows Vista and later versions of Windows, to create a PSSession on a
local computer, you must start PowerShell with the "Run as
administrator" option.
local computer, you must start PowerShell with the "Run as administrator"
option.

## What Is a Session?

A session is an environment in which PowerShell runs.

Each time you start PowerShell, a session is created for you, and you
can run commands in the session. You can also add items to your session, such
as modules and snap-ins, and you can create items, such as variables,
functions, and aliases. These items exist only in the session and are deleted
when the session ends.

You can also create user-managed sessions, known as " PowerShell
sessions" or "PSSessions," on the local computer or on a remote computer. Like
the default session, you can run commands in a PSSession and add and create
items. However, unlike the session that starts automatically, you can control
the PSSessions that you create. You can get, create, configure, and remove
them, disconnect and reconnect to them, and run multiple commands in the same
PSSession. The PSSession remains available until you delete it or it times
out.
Each time you start PowerShell, a session is created for you, and you can run
commands in the session. You can also add items to your session, such as
modules and snap-ins, and you can create items, such as variables, functions,
and aliases. These items exist only in the session and are deleted when the
session ends.

You can also create user-managed sessions, known as "PowerShell sessions" or
"PSSessions," on the local computer or on a remote computer. Like the default
session, you can run commands in a PSSession and add and create items.
However, unlike the session that starts automatically, you can control the
PSSessions that you create. You can get, create, configure, and remove them,
disconnect and reconnect to them, and run multiple commands in the same
PSSession. The PSSession remains available until you delete it or it times out.

Typically, you create a PSSession to run a series of related commands on a
remote computer. When you create a PSSession on a remote computer, PowerShell
Expand All @@ -74,15 +71,14 @@ session.

If you use the **ComputerName** parameter of the `Invoke-Command` or
`Enter-PSSession` cmdlet to run a remote command or to start an interactive
session, PowerShell creates a temporary session on the remote computer
and closes the session as soon as the command is complete or as soon as the
session, PowerShell creates a temporary session on the remote computer and
closes the session as soon as the command is complete or as soon as the
interactive session ends. You cannot control these temporary sessions, and you
cannot use them for more than a single command or a single interactive
session.
cannot use them for more than a single command or a single interactive session.

In PowerShell, the "current session" is the session that you are
working in. The "current session" can refer to any session, including a
temporary session or a PSSession.
In PowerShell, the "current session" is the session that you are working in.
The "current session" can refer to any session, including a temporary session
or a PSSession.

## Why Use a PSSession?

Expand All @@ -96,10 +92,9 @@ You can run remote commands without creating a PSSession. Use the
or a series of unrelated commands on one or many computers.

When you use the **ComputerName** parameter of `Invoke-Command` or
`Enter-PSSession`, PowerShell establishes a temporary connection to
the remote computer and then closes the connection as soon as the command is
complete. Any data elements that you create are lost when the connection is
closed.
`Enter-PSSession`, PowerShell establishes a temporary connection to the remote
computer and then closes the connection as soon as the command is complete.
Any data elements that you create are lost when the connection is closed.

Other cmdlets that have a **ComputerName** parameter, such as `Get-Eventlog`
and `Get-WmiObject`, use different remoting technologies to gather data. None
Expand All @@ -108,8 +103,8 @@ create a persistent connection like a PSSession.
## How to Create a PSSession

To create a PSSession, use the `New-PSSession` cmdlet. To create the PSSession
on a remote computer, use the **ComputerName** parameter of the
`New-PSSession` cmdlet.
on a remote computer, use the **ComputerName** parameter of the `New-PSSession`
cmdlet.

For example, the following command creates a new PSSession on the Server01
computer.
Expand Down Expand Up @@ -151,7 +146,8 @@ To get the PSSessions that were created in your current session, use the
`Get-PSSession` cmdlet without the **ComputerName** parameter. `Get-PSSession`
returns the same type of object that `New-PSSession` returns.

The following command gets all the PSSessions that were created in the current session.
The following command gets all the PSSessions that were created in the current
session.

```powershell
Get-PSSession
Expand Down Expand Up @@ -239,7 +235,7 @@ until it times out.

You can also use the **IdleTimeout** parameter of the `New-PSSessionOption`
cmdlet to set an expiration time for an idle PSSession. For more information,
see [New-PSSessionOption](xref:Microsoft.PowerShell.Core.New-PSSessionOption).
see [New-PSSessionOption][03].

## The PSSession Cmdlets

Expand All @@ -263,22 +259,37 @@ Get-Help *-PSSession
> Disconnected sessions are only supported on Windows. The `Connect-PSSession`,
> `Disconnect-PSSession`, and `Receive-PSSession` cmdlets are only available
> on Windows. For more information about disconnected sessions, see
> [about_Remote_Disconnected_Session](about_Remote_Disconnected_Sessions.md)
> [about_Remote_Disconnected_Session][04].

## For More Information

For more information about PSSessions, see [about_PSSession_Details](about_PSSession_Details.md).
For more information about PSSessions, see [about_PSSession_Details][01].

## See also

- [about_Remote](about_Remote.md)
- [about_Remote_Disconnected_Sessions](about_Remote_Disconnected_Sessions.md)
- [about_Remote_Requirements](about_Remote_Requirements.md)
- [Invoke-Command](xref:Microsoft.PowerShell.Core.Invoke-Command)
- [Connect-PSSession](xref:Microsoft.PowerShell.Core.Connect-PSSession)
- [Disconnect-PSSession](xref:Microsoft.PowerShell.Core.Disconnect-PSSession)
- [Enter-PSSession](xref:Microsoft.PowerShell.Core.Enter-PSSession)
- [Exit-PSSession](xref:Microsoft.PowerShell.Core.Exit-PSSession)
- [Get-PSSession](xref:Microsoft.PowerShell.Core.Get-PSSession)
- [New-PSSession](xref:Microsoft.PowerShell.Core.New-PSSession)
- [Remove-PSSession](xref:Microsoft.PowerShell.Core.Remove-PSSession)
- [about_Remote][05]
- [about_Remote_Disconnected_Sessions][04]
- [about_Remote_Requirements][02]
- [Invoke-Command][06]
- [Connect-PSSession][07]
- [Disconnect-PSSession][08]
- [Enter-PSSession][09]
- [Exit-PSSession][10]
- [Get-PSSession][11]
- [New-PSSession][12]
- [Remove-PSSession][13]

<!-- link references -->
[01]: about_PSSession_Details.md
[02]: about_Remote_Requirements.md
[03]: xref:Microsoft.PowerShell.Core.New-PSSessionOption
[04]: about_Remote_Disconnected_Sessions.md
[05]: about_Remote.md
[06]: xref:Microsoft.PowerShell.Core.Invoke-Command
[07]: xref:Microsoft.PowerShell.Core.Connect-PSSession
[08]: xref:Microsoft.PowerShell.Core.Disconnect-PSSession
[09]: xref:Microsoft.PowerShell.Core.Enter-PSSession
[10]: xref:Microsoft.PowerShell.Core.Exit-PSSession
[11]: xref:Microsoft.PowerShell.Core.Get-PSSession
[12]: xref:Microsoft.PowerShell.Core.New-PSSession
[13]: xref:Microsoft.PowerShell.Core.Remove-PSSession
22 changes: 12 additions & 10 deletions reference/7.5/Microsoft.PowerShell.Core/About/about_Pwsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ Running `pwsh -File -` without redirected standard input starts a regular
session. This is the same as not specifying the `File` parameter at all. When
reading from standard input, the input statements are executed one statement at
a time as though they were typed at the PowerShell command prompt. If a
statement doesn't parse correctly, the statement isn't executed. The process exit code is
determined by status of the last (executed) command within the input. With
normal termination, the exit code is always `0`. When the script file
terminates with an `exit` command, the process exit code is set to the numeric
argument used with the `exit` command.
statement doesn't parse correctly, the statement isn't executed. The process
exit code is determined by status of the last (executed) command within the
input. With normal termination, the exit code is always `0`. When the script
file terminates with an `exit` command, the process exit code is set to the
numeric argument used with the `exit` command.

Similar to `-Command`, when a script-terminating error occurs, the exit code is
set to `1`. However, unlike with `-Command`, when the execution is interrupted
Expand Down Expand Up @@ -152,7 +152,7 @@ script block curly braces are often not required in the first place when
running from `cmd.exe`. To execute an inline script block defined inside a
string, the [call operator][03] `&` can be used:

```
```powershell
pwsh -Command "& {Get-WinEvent -LogName security}"
```

Expand Down Expand Up @@ -289,7 +289,7 @@ For example:
$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
pwsh -encodedcommand $encodedCommand
pwsh -EncodedCommand $encodedCommand
```

### -ExecutionPolicy | -ex | -ep
Expand Down Expand Up @@ -326,7 +326,8 @@ To set up `pwsh` as the login shell on UNIX-like operating systems:
- Verify that the full absolute path to `pwsh` is listed under `/etc/shells`
- This path is usually something like `/usr/bin/pwsh` on Linux or
`/usr/local/bin/pwsh` on macOS
- With some installation methods, this entry will be added automatically at installation time
- With some installation methods, this entry will be added automatically at
installation time
- If `pwsh` isn't present in `/etc/shells`, use an editor to append the path
to `pwsh` on the last line. This requires elevated privileges to edit.
- Use the [chsh][04] utility to set your current
Expand Down Expand Up @@ -408,7 +409,8 @@ non-Windows platforms results in an error.

### -Version | -v

Displays the version of this PowerShell executable. Additional parameters are ignored.
Displays the version of this PowerShell executable. Additional parameters are
ignored.

### -WindowStyle | -w

Expand All @@ -431,6 +433,6 @@ prepend the command parameters with a hyphen (`-`), not a forward slash (`/`).
<!-- link references -->
[01]: /powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1&preserve-view=true
[02]: about_Automatic_Variables.md#lastexitcode
[03]: about_operators.md#special-operators
[03]: about_Operators.md#special-operators
[04]: https://linux.die.net/man/1/chsh
[05]: about_Parsing.md#passing-arguments-that-contain-quote-characters
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ For more information about this behavior, see the [about_Parsing][03] article.
[01]: #including-quote-characters-in-a-string
[02]: about_Parsing.md#argument-mode
[03]: about_Parsing.md#passing-arguments-that-contain-quote-characters
[04]: about_preference_variables.md#ofs
[04]: about_Preference_Variables.md#ofs
[05]: about_Special_Characters.md
[06]: https://en.wikipedia.org/wiki/Quotation_marks_in_English#Smart_quotes
[07]: xref:Microsoft.PowerShell.Utility.ConvertFrom-StringData
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Explains how to redirect output from PowerShell to text files.
## Long description

By default, PowerShell sends output to the PowerShell host. Usually this is the
console application. However, you can redirect the output to a text file and you
can redirect error output to the regular output stream.
console application. However, you can redirect the output to a text file and
you can redirect error output to the regular output stream.

You can use the following methods to redirect output:

Expand Down Expand Up @@ -79,8 +79,7 @@ PowerShell 7.4 changed the behavior of the redirection operators when used to
redirect the **stdout** stream of a native command. The redirection operators
now preserve the byte-stream data when redirecting output from a native
command. PowerShell doesn't interpret the redirected data or add any additional
formatting. For more information, see
[Example #7](#example-7-redirecting-binary-data-from-a-native-command).
formatting. For more information, see [Example #7][17].

## Examples

Expand Down Expand Up @@ -134,8 +133,7 @@ file called `script.log`.
### Example 5: Suppress all Write-Host and Information stream data

This example suppresses all information stream data. To read more about
**Information** stream cmdlets, see
[Write-Host][11] and
**Information** stream cmdlets, see [Write-Host][11] and
[Write-Information][12]

```powershell
Expand All @@ -154,27 +152,27 @@ particular stream. The script in this example shows how the value of
```powershell
$ErrorActionPreference = 'Continue'
$ErrorActionPreference > log.txt
get-item /not-here 2>&1 >> log.txt
Get-Item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'SilentlyContinue'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt
Get-Item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'Stop'
$ErrorActionPreference >> log.txt
Try {
get-item /not-here 2>&1 >> log.txt
Get-Item /not-here 2>&1 >> log.txt
}
catch {
"`tError caught!" >> log.txt
}
$ErrorActionPreference = 'Ignore'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt
Get-Item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'Inquire'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt
Get-Item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'Continue'
```
Expand Down Expand Up @@ -387,7 +385,7 @@ used. For more information, see the `-gt` operator in
[03]: about_Operators.md
[04]: about_Output_Streams.md
[05]: about_Path_Syntax.md
[06]: about_preference_variables.md#psdefaultparametervalues
[06]: about_Preference_Variables.md#psdefaultparametervalues
[07]: xref:Microsoft.PowerShell.Utility.Out-File
[08]: xref:Microsoft.PowerShell.Utility.Tee-Object
[09]: xref:Microsoft.PowerShell.Utility.Write-Debug
Expand All @@ -398,3 +396,4 @@ used. For more information, see the `-gt` operator in
[14]: xref:Microsoft.PowerShell.Utility.Write-Progress
[15]: xref:Microsoft.PowerShell.Utility.Write-Verbose
[16]: xref:Microsoft.PowerShell.Utility.Write-Warning
[17]: #example-7-redirecting-binary-data-from-a-native-command
Loading