Search code examples
powershellpowershell-remoting

How can I tell if I'm in a remote PowerShell session?


I would like to execute code that is specific to remote PSSessions. That is, the code doesn't apply locally, but applies to all remote sessions.

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


Solution

  • Check if the $PSSenderInfo variable exists. From about_Automatic_Variables:

    $PSSenderInfo

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

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