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.

5
  • 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 Commented Mar 20, 2022 at 19:49
  • 1
    Does this answer your question? Powershell Script doesn't work when starting it by double-clicking Commented Mar 20, 2022 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"? Commented Mar 20, 2022 at 22:12
  • 1
    this description will be informative and help you: stackoverflow.com/questions/58243310/… Commented Mar 20, 2022 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 :(. Commented Mar 21, 2022 at 13:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.