2

I am trying to launch a PowerShell script using keyboard shortcuts. My current script (super-simple, just prompts a yes/no popup) looks like this:

$a = new-object -comobject wscript.shell 
$a.popup("Here's a test script", 0,"Title goes here", 4)

To make sure that it wasn't any lookup issues, I put both the script and the shortcut to the script on the desktop, namely:

  • C:\users\me\desktop\ShortcutTest.ps1
  • C:\users\me\desktop\ShortcutTest - shortcut.ps1

The Shortcut has the following settings in the properties:

Target:        C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File 'C:\Users\me\Desktop\ShortcutTest.ps1'
Start in:      C:\Users\me\Desktop
Shortcut key:  Ctrl + Alt + Q

the problem is when I press the shortcut it successfully starts a PowerShell window, but it immediately closes the instance.

Troubleshooting so far

  • Running the shortcut as an administrator, same result
  • Changing the location of the shortcut, same result
  • Changing the location of the actual script, same result (it initially resided on a server that prevents scripts)
  • Switching the 'Start in' and 'Target' sections of the shortcut, same result
  • Removing the -File parameter from the 'Target' in the shortcut makes the PowerShell instance remain opened, so I'm guessing that I'm missing some form of parameter settings in the script, or when calling it from the shortcut.
  • Changing the script contents to a simple 'Write-Host "Great success"' still closes the PowerShell instance immediately, further making me suspect that it is the parameters used in the 'Target' (-File and -ExecutionPolicy) that is missing something.

If anyone has any tips on how I could solve this, or at least troubleshoot further, it would be greatly appreciated!

5
  • "When I press the shortcut it successfully starts a Powershell window, but it immediately closes the instance." What were you expecting to happen? It runs the script and the shell terminates. Are you saying you want the script to run but the console window to remain after the script finishes? Commented Apr 20, 2017 at 13:03
  • Ah, sorry for not being clear :) the script is supposed to wait for the user to click on one of the boxes in the prompt window. When running the script manually it prompts the user with a yes/no box, but when I run it with the shortcut the prompt is not displayed. Commented Apr 20, 2017 at 13:05
  • 1
    Try this, it works for me on another file I have: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -ExecutionPolicy ByPass -File 'C:\Users\me\Desktop\ShortcutTest.ps1' Commented Apr 20, 2017 at 13:05
  • 2
    I think you need to use double quotes. e.g. -File "C:\Users\me\Desktop\ShortcutTest.ps1" Commented Apr 20, 2017 at 13:25
  • You were both right :) It required both double-quotes and using the -WindowStyle Hidden, thank you so much! Commented Apr 20, 2017 at 13:41

1 Answer 1

2

Try this:

 "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -
     WindowStyle Hidden -ExecutionPolicy ByPass -File 
    'C:\Users\me\Desktop\ShortcutTest.ps1'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.