Windows Explorer (desktop) has the ability to run a PowerShell script by right-clicking and selecting "Run with PowerShell". However, as the window is closed when the script has finished, any messages are lost. So one could stick a "Press any key" at the end. However, that would be very annoying when run from the PowerShell command prompt.

Is there a way for PowerShell to determine where it was run from? Specifically, the desktop?

closed as off-topic by peterh, kasperd, Jenny D, sebix, mdpc Aug 18 '17 at 23:10

  • This question does not appear to be about server, networking, or related infrastructure administration within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    PowerShell is nothing to do with server management?? sigh – Rob Nicholson Feb 7 at 23:03

Run with PowerShells $myinvocation.line is taken from corresponding registry key. For instance, on (my) Windows 8.1 with PS version 5.1:

PS D:\PShell> $auxRegKey='\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\0\Command'
PS D:\PShell> (get-itemproperty -literalpath HKLM:$auxRegKey).'(default)'
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
PS D:\PShell>

The following code snippet could help:

'script ouput here'
$auxRegVal=(get-itemproperty -literalpath HKLM:$auxRegKey).'(default)'
$auxRegCmd=$auxRegVal.Split(' ',3)[2].Replace('%1', $MyInvocation.MyCommand.Definition)
if ("`"$($myinvocation.Line)`"" -eq $auxRegCmd) {
    $MyInvocation.MyCommand.Definition + ': supposedly run via explorer right click'
    $x = Read-Host
} else {
    $MyInvocation.MyCommand.Definition + ': run from CLI'    # optional

The script says supposedly because we could imagine following (improbable) command from an open cmd window (or even its equivalent from PowerShell prompt):

==> "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'D:\PShell\SF\q866281.ps1'"
script ouput here
D:\PShell\SF\q866281.ps1: supposedly run via explorer right click

  • Thanks for this - so you're suggesting looking in the registry to find the full command path that the desktop uses to build the invocation and testing against that. Yes, I can see that working. Bit fiddly but what the heck – Rob Nicholson Aug 3 '17 at 16:36
  • Okay, really smelly code but marked as the answer :-) Let's hope Microsoft doesn't change the registry key entry drastically - but as long as they leave that %1 in there, it'll hang together – Rob Nicholson Aug 3 '17 at 16:48

There is a variable named $myinvocation will return a InvocationInfo Class object. It contains various information about how the script was invoked. https://msdn.microsoft.com/en-us/library/system.management.automation.invocationinfo(v=vs.85).aspx

You could do something like this to reach your goal.

"hello world"
if ($myinvocation.line) {
    "run from cli"
} else {
    "run via explorer right click"
    $x = read-host
  • This doesn't work for me. I get run from cli with right click as well. (Using PS version 5.1) – Gerald Schneider Aug 1 '17 at 13:41
  • What version of Powershell are you running? I am using Version 3. – Clayton Aug 1 '17 at 13:43
  • When run from the desktop, I get Line: if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\HeliosComms(RobNicho\OneDrive - Helios Medical Communications\My Scripts\Run-Test.ps1' – Rob Nicholson Aug 1 '17 at 15:26
  • I'm guessing your running a script named run-test.ps1... If that line code does not appear in the run-test.ps1 script, it may be coming from the User or Machine Powershell profile. Is there some sort of error it is throwing? Is it actually running the run-test.ps1 script? – Clayton Aug 1 '17 at 15:51
  • Yes, it's running the script fine. That invocation line must be what Windows 10 tacks on the front when running from the desktop. So this is PowerShell v5 – Rob Nicholson Aug 3 '17 at 16:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.