Search code examples
powershellpowershell-remoting

How to check if a Powershell script is running remotely


I have a script that can be run either locally or remotely (via WinRM), however I would like it to behave slightly differently when run on a remote machine. I realise that I can pass in a switch to the script to identify whether it is running locally or remotely, but I want to know if it is possible for the script itself to detect whether it is running remotely?


Solution

  • Get-Host returns, amongst other information a Name:

    PS> (Get-Host).Name
    ConsoleHost
    PS> (Invoke-Command -ComputerName dev2 -Script {Get-Host}).Name
    ServerRemoteHost