1

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.

peter
  • 13
  • 3
  • 1
    Ps1 files are not executables by default. You can create a shortcut of your Ps1 by prependig `powershell.exe -file pathhere` to have an executable shortcut of your script – Santiago Squarzon Mar 20 '22 at 19:49
  • 1
    Does this answer your question? [Powershell Script doesn't work when starting it by double-clicking](https://stackoverflow.com/questions/13762002/powershell-script-doesnt-work-when-starting-it-by-double-clicking) – DocZerø Mar 20 '22 at 20:00
  • 1
    What command are you using to start it? More specifically, do you have quotes around your filename (or filepath)? i.e., something like `powershell.exe -File ".\t1 - something.ps1"`? – DarkMoon Mar 20 '22 at 22:12
  • 1
    this description will be informative and help you: https://stackoverflow.com/questions/58243310/any-way-to-double-click-on-ps1-file-open-them-in-powershell – An-dir Mar 20 '22 at 22:29
  • Thank you for your anwers and helping me figure out what the problem was, I would like to upvote your comments or flag them as helpfull, but i currently do not have enough reputation to do that :(. – peter Mar 21 '22 at 13:43

1 Answers1

1
  • By default, .ps1 files 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.ps1 executes t1.ps1 instead implies that your configuration is flawed:

      • Specifically, it suggests that the file-name argument in the underlying powershell.exe command used for invocation lacks (effective) enclosing "..." quoting - as would happen if you had used File Explorer to interactively associate .ps1 files with powershell.exe.

      • See this answer for more information and a fix.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thank you for your answer! Like you suggested, my configuration was the problem, I had set `powershell.exe` as the default program for `.ps1` files, which resultet in theese kind of things. – peter Mar 21 '22 at 13:38
  • Makes sense, @peter; glad to hear the answer helped. Note that with `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). – mklement0 Mar 21 '22 at 13:41