简体   繁体   中英

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

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'"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM