tbriggs707
tbriggs707

Reputation: 99

Powershell Script Window Disappears

My end goal is to create a keyboard shortcut that runs a powershell script. I followed what this guy is doing here. Right now, when I run the script from a PowerShell terminal, everything works just fine. But when I run it from the shortcut by double-clicking in the File Explorer (or keyboard shortcut), a new window appears with some kind of text in red but the window disappears before I have time to read anything. I'm sure I could fix the issue if I could read the message, but the window disappears too quickly. My .ps1 script and shortcut are saved to the Desktop.

I found this article that suggests adding the -NoExitswitch, but this does not fix the issue for me.

I have tried changing the Execution Policy to Bypass and Unrestricted and neither made a difference.

I tried modifying my script to pause on the first line of code, but it doesn't get that far so I assume the issue is not with my script.

The shortcut's properties:

Target: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Bypass -File C:\Users\%UserName%\Desktop\<script>.ps1

Start in: C:\Users\%UserName%\Desktop\

The script's properties window.

I need to know why my script isn't running.

Please let me know what other information I left out and thanks in advance.

Upvotes: 2

Views: 1477

Answers (3)

mklement0
mklement0

Reputation: 437090

Your symptom implies that C:\Users\%UserName%\Desktop\<script>.ps1 refers to a nonexistent script file.

<script>.ps1 is just a placeholder for the real filename.

Note: The use of environment-variable reference %UserName% per se is not a problem; any environment variable can be referenced this way, both in the Target: field (command line) and the Start in: field (working directory).
As an aside: Consider using %USERPROFILE%, or, more robustly, %HOMEDRIVE%%HOMEPATH% to refer to the current user's home directory[1].

Make sure that the path is correct, and try again.

If you want to troubleshoot an environment-variable-based path, temporarily modify your command line as follows:

powershell.exe -noexit -ExecutionPolicy Bypass -c 'C:\Users\%UserName%\Desktop\<script>.ps1'

-File was replaced with -c (-Command), and single quotes are used around the path, which makes PowerShell treat the content as a literal string to print.

The above will merely print the expanded script path.


As for the root cause:

Debatably, PowerShell always exits the new process if the script file path passed to the -File argument cannot be found - even if -NoExit is also present.

The same problem exists - but only in Windows PowerShell, not PowerShell Core - if the file exists but doesn't have extension .ps1.

I've reported this behavior in GitHub issue #10471.


[1]%USERPROFILE% and %HOMEDRIVE%%HOMEPATH% by default point to the same directory (C:\Users\%USERNAME%), but can be configured point to point to different locations; %USERPROFILE% then points to a directory with OS and application configuration data only, whereas %HOMEDRIVE%%HOMEPATH% points to the directory where the user's desktop and documents are stored.

Upvotes: 2

Theo
Theo

Reputation: 61028

You could use the New-Shortcut function I've posted here to create your shortcut:

$myDesktop     = [Environment]::GetFolderPath("Desktop")
$myScriptPath  = Join-Path -Path $myDesktop -ChildPath 'YOUR PS1 SCRIPT FILE'

$shortcutProps = @{
    'ShortcutPath'     = Join-Path -Path $myDesktop -ChildPath 'Run PowerShell File.lnk'
    'TargetPath'       = '%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe'
    'Arguments'        = '-ExecutionPolicy Bypass', '-NoExit', '-NoLogo', ('-File "{0}"' -f $myScriptPath)
    'Description'      = "Run $myScriptPath"
    'WorkingDirectory' = $myDesktop
    'HotKey'           = 'Ctrl','Shift','F'
    'WindowStyle'      = 'Default'
}

New-Shortcut @shortcutProps

Besides the -NoExit switch, there are more options to leave the window open after the code has run.
For that have a look at deadlydog's answer and the blog he wrote about that.

Upvotes: 0

Jesse
Jesse

Reputation: 1424

Try changing C:\Users\%UserName%\ to %userprofile%\. I don't believe that %username% works in shortcuts the way you are trying to use it.

Upvotes: 0

Related Questions