In my directory I have a file called "t1.ps1" and a second file called "t1 - something.ps1".
Both of them run fine when run fine, when executed via right click run with PowerShell or when executed directly in PowerShell, but when executed via double click or cmd it seems to always execute the first script.
For me it seems like the dash is the problem, but I was kinda surprised that windows did not just throw an error or just did nothing, but instead executed a different script.
Maybe this is a well known phenomena or i just have some jank settings, that made this happen, but I was kinda surprised about it.
1 Answer
By default,
.ps1files are NOT executed when you double-click them in File Explorer / on the Desktop - instead, they're opened for editing.The fact that they are executed for you implies that you modified the default configuration:
However, the fact that double-clicking
t1 - something.ps1executest1.ps1instead implies that your configuration is flawed:Specifically, it suggests that the file-name argument in the underlying
powershell.execommand used for invocation lacks (effective) enclosing"..."quoting - as would happen if you had used File Explorer to interactively associate.ps1files withpowershell.exe.See this answer for more information and a fix.
2 Comments
powershell.exe as the default program for .ps1 files, which resultet in theese kind of things.pwsh.exe, the CLI of the PowerShell (Core) 7+ edition, the problem would no longer arise, because it defaults to the -File parameter (whereas default is -Command with powershell.exe, which ultimately renders the "..." quoting around the script file path ineffective).
powershell.exe -file pathhereto have an executable shortcut of your scriptpowershell.exe -File ".\t1 - something.ps1"?