Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a PowerShell script from Windows Explorer when Notepad is not the default application?

It seems that the only time Windows shows "Run with PowerShell" in the content menu of a ps1 file is when the default application is Notepad. If I change the default application to the PowerShell ISE (or any other editor, for that matter), the "Run with PowerShell" context item doesn't appear.

Windows 7 Professional SP1, WMF 4.0

like image 566
elmonty Avatar asked Sep 23 '16 17:09

elmonty


People also ask

How do I run a PowerShell script from File Explorer?

In File Explorer (or Windows Explorer), right-click the script file name and then select "Run with PowerShell". The "Run with PowerShell" feature starts a PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

Why is PowerShell opening in notepad?

Windows sets the default action for . PS1 files to open them in Notepad, instead of sending them to the PowerShell command interpreter. This is to directly prevent accidental execution of malicious scripts when they're simply double-clicked.

Do Windows PowerShell scripts work by default?

The default execution policy, Restricted , prevents all scripts from running, including scripts that you write on the local computer. For more information, see about_Execution_Policies. The execution policy is saved in the registry, so you need to change it only once on each computer.

Why are PowerShell scripts disabled by default?

While running PowerShell script, if you get running scripts is disabled on this system, it is because the PowerShell execution policy is set up by default as Restricted and doesn't allow to run script. PowerShell has built-in security features implemented.


2 Answers

In Windows 10 press Start then type "default app settings", click on "Default app settings". A new windows comes up "Choose default apps by file type". Scroll to ".ps1". Click on the current app icon, then select Notepad.

As elmonty stated, this will restore the "Run with PowerShell" context menu entry when right clicking on a ps1 file.

You can copy the below into a text file and import into your registry. This will open the PowerShell console and execute the script when you double click on a ps1 file.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""

or open regedit, navigate to:

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]

doulbe click on the (Default) string entry and enter the following in the "Value data:":

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
like image 80
Leon van Wyk Avatar answered Oct 18 '22 03:10

Leon van Wyk


I found out the answer. You must set the default application back to Notepad. This will restore the "Run with PowerShell" context item. Next, you have to edit the "Open with" application directly in the registry, here:

\HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command

Change the value to:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe" "%1"

Or you can change it to the editor of your choice.

like image 21
elmonty Avatar answered Oct 18 '22 01:10

elmonty