[英]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.似乎 Windows 唯一一次在 ps1 文件的内容菜单中显示“使用 PowerShell 运行”是在默认应用程序是记事本时。 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.
如果我将默认应用程序更改为 PowerShell ISE(或任何其他编辑器,就此而言),则不会出现“使用 PowerShell 运行”上下文项。
Windows 7 Professional SP1, WMF 4.0 Windows 7 专业版 SP1,WMF 4.0
In Windows 10 press Start then type "default app settings", click on "Default app settings".在 Windows 10 中按“开始”,然后键入“默认应用设置”,单击“默认应用设置”。 A new windows comes up "Choose default apps by file type".
出现一个新窗口“按文件类型选择默认应用程序”。 Scroll to ".ps1".
滚动到“.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.正如 elmonty 所说,这将在右键单击 ps1 文件时恢复“使用 PowerShell 运行”上下文菜单条目。
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.
这将打开 PowerShell 控制台并在您双击 ps1 文件时执行脚本。
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:或打开 regedit,导航到:
[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.
这将恢复“使用 PowerShell 运行”上下文项。 Next, you have to edit the "Open with" application directly in the registry, here:
接下来,您必须直接在注册表中编辑“打开方式”应用程序,在这里:
\\HKEY_CLASSES_ROOT\\Microsoft.PowerShellScript.1\\Shell\\Open\\Command \\HKEY_CLASSES_ROOT\\Microsoft.PowerShellScript.1\\Shell\\Open\\Command
Change the value to:将值更改为:
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe" "%1" "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe" "%1"
Or you can change it to the editor of your choice.或者您可以将其更改为您选择的编辑器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.