I am new to PowerShell scripting. One of the first things I learned is that I (normally !) cannot execute a Powershell script by double clicking on it (similar to *.bat Batch Scripts). Instead I always have to type at command prompt:
In addition to the suggestions above, if you right-click the file there should be a"Run with PowerShell" option. This keeps the safety check previously mentioned in place so you don’t accidentally execute a script.
I have always felt that the PowerShell team was right when they associated .PS1 with notepad. These days, I’ve re-associated it with the ISE - but it’s safer. I’ve doubleclicked a .vbs file all too easily and often.
Your Registry association hack is the kind of solution I am looking for.
Unfortunately there is something still odd.
After entering it exactly as you suggested I rebooted and double clicked of the file mytest.ps1 with the following most simple content:
$psversiontable
A dialog pops up telling me:
Format of the file <mytest.ps1> could not be determined.
For displaying of less common formats, the following option is available:
Option>General>Display all image file types
Hmm, whats does that mean?
When I think about your association then I remark that it is linked to the file type “Microsoft.PowerShellScript.1” rather tah to the extension “.ps1”
Maybe one have to repeat the association for that file extension as well?
Or to define *.ps1 as an extension of “Microsoft.PowerShellScript.1”? How do I have to do this in detail?
Not wanting to teach you to suck eggs but Jeffry Snover the architect for Powershell said that PS1 file deliberately open with notepad. This is part of the “Security by design” of Powershell. i.e. you don’t accidentally run a script by clicking on it, or worse, if a user is sent a dodgy file, they can’t execute the script accidentally.
The Powershell team advise against changing ps1 files to run on double click.
Given the theoretical ability of a PowerShell script to make massive changes to an environment, I’m with tfl on this one. I open my scripts in the ISE before running them to verify I have the right one. (Properly-commented scripts, FTMFW!) Running a script on a double-click is asking for disaster, where opening in the ISE is simply an extra click.