How can I find out if I am in a remote PowerShell session?

I would like to execute code specific to remote PSSessions. That is, the code is not applied locally, but applies to all remote sessions.

Is there any environment variable, function, or cmdlet that would effectively return true if I am in an active PSSession and false if I run locally?

+6
source share
1 answer

Check if $PSSenderInfo variable $PSSenderInfo . From about_Automatic_Variables:

$PSSenderInfo

Contains information about the user who started the PSSession, including the user ID and time zone of the computer. This variable is available only in PSSessions.

The $PSSenderInfo includes a user-configurable property, ApplicationArguments , which by default contains only $PSVersionTable from the original session. To add data to ApplicationArguments , use the ApplicationArguments parameter of the New-PSSessionOption .

+7
source

All Articles