简体   繁体   English

当记事本不是默认应用程序时,如何从 Windows 资源管理器运行 PowerShell 脚本?

[英]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.

相关问题 如何从 Windows Explorer 启动 PowerShell? - How do I start PowerShell from Windows Explorer? 如何从 python 运行 Windows powershell 脚本? - How to run Windows powershell script from python? 如何在Windows /在Powershell上运行此代码? - How do I run this code in Windows/on Powershell? 如何获取 Windows 资源管理器使用的默认字体 (TTF)? - How do I get the default font(TTF) Windows Explorer is using? 如何从基于同一台计算机的 Linux VM 在 Windows 计算机上运行 Internet Explorer? - How do I run Internet Explorer on a Windows computer from a Linux VM based on the same computer? 从命令提示符或Windows 7/8跳转列表菜单打开sln文件时,如何添加默认的“以管理员身份运行”? - How do I add a default Run As Administrator when I open a sln file from the command prompt or Windows 7/8 Jump List menu? 如何在已修改为不运行资源管理器的Windows系统上启动时运行应用程序 - How can I run an application on startup on a Windows system which has been modified to not run explorer 如何以管理员身份远程运行 Powershell 脚本? (PsExec) - How do I run a Powershell script remotely as admin? (PsExec) 如何防止 Windows 资源管理器干扰删除文件夹? - How do I keep Windows Explorer from interfering with deleting a folder? 如何以编程方式自动从Windows资源管理器中打开文件 - How do I automate the opening of a file from Windows Explorer programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM