如何在电源外壳中自动打开多个电源外壳? [英] How to open multiple power shell automatically in power shell?

查看:33
本文介绍了如何在电源外壳中自动打开多个电源外壳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 my_project.ps1 文件,我正在从中激活虚拟环境 &开始我的项目.

I have my_project.ps1 file from which I am activating virtual environment & starting my project.

目前我需要打开我的powershell,然后我需要转到保存我的.ps1文件的目录&只能从 powershell 打开它.

currently I need to open my powershell then after I need to go to directory where I have saved my .ps1 file & have to open it from powershell only.

有什么办法可以让我双击 .ps1 文件 &它会在power shell中自动打开吗?

Is there any way so that I can double click on .ps1 file & it will open automatically in power shell ?

推荐答案

按照设计,双击(打开)*.ps1 文件"https://en.wikipedia.org/wiki/Windows_shell" rel="nofollow noreferrer">Windows shell(桌面、文件资源管理器、任务栏、开始菜单)执行它们- 相反,它们在记事本或 PowerShell ISE 中打开用于编辑,具体取决于 Windows/PowerShell 版本

By design, double-clicking (opening) *.ps1 files from the Windows shell (Desktop, File Explorer, taskbar, Start Menu) does not execute them - instead they're opened for editing in Notepad or the PowerShell ISE, depending on the Windows / PowerShell version

但是,至少在 Windows 7 中,*.ps1 文件的快捷菜单包含一个 Run with PowerShell 命令,它确实调用手头的脚本;这可能足以满足您的目的,但此调用方法有局限性 - 请参阅底部部分了解详细信息.

However, since at least Windows 7, the shortcut menu for *.ps1 files contains a Run with PowerShell command, which does invoke the script at hand; this may be enough for your purposes, but this invocation method has limitations - see the bottom section for details.

使用文件资源管理器让PowerShell默认执行.ps1文件:

Use File Explorer to make PowerShell execute .ps1 files by default:

  • 右键单击 .ps1 文件并选择 Properties.
  • 点击 Opens with: 标签旁边的 Change....
  • 点击列表底部的More apps并向下滚动到在这台电脑上寻找另一个应用
  • 浏览或粘贴文件路径 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 并提交.
  • Right-click on a .ps1 file and select Properties.
  • Click on Change... next to the Opens with: label.
  • Click on More apps on the bottom of the list and scroll down to Look for another app on this PC
  • Browse to or paste file path C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and submit.

此方法让您无法控制 PowerShell 调用的细节;实际上,您最终会出现以下行为:

This method gives you no control over the specifics of the PowerShell invocation; in effect you'll end up with the following behavior:

  • 重新定义仅对当前用户有效 - 这可能是一件好事,因为其他用户可能不希望此更改,这可能会导致脚本意外执行.

  • The redefinition is only in effect for the current user - which is probably a good thing, as other users may not expect this change, which can result in unwanted execution of script.

任何有效的执行政策都将得到遵守;例如,如果 Restricted 有效,则调用将完全失败.

Whatever execution policy is in effect will be honored; e.g., if Restricted is in effect, invocation will fail altogether.

与默认的 Run in PowerShell 命令一样,运行脚本的窗口将自动关闭,除非脚本在退出前明确提示用户.

As with the default Run in PowerShell command, the window in which the script run will automatically close unless the script explicitly prompts the user before exiting.

要对 PowerShell 调用脚本的方式进行更多控制,请使用下面显示的编程方法.

To exercise more control over how PowerShell invokes the script, use the programmatic method shown next.

修改注册表,为 HKEY_CLASSES_ROOT\Microsoft.PowerShellScript 中的 *.ps1 文件重新定义 Open 快捷菜单命令.1\shell\Open\Command,如下图

Modify the registry to redefine the Open shortcut-menu command for *.ps1 files at HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell\Open\Command, as shown below.

重要:如果您之前使用过上述 GUI 方法,则必须手动删除底层注册表定义,否则程序化重新定义将不会生效:

Important: If you've previously used the GUI method above, you must manually remove the underlying registry definition, otherwise the programmatic redefinition won't take effect:

  • 打开regedit.exe(需要管理员权限)

导航到 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1删除那个键.

注意:至少在 Windows 10 上,尝试以编程方式删除此密钥 - 例如使用 reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice/f 由于权限问题而失败,即使以管理员身份运行也是如此.

Note: At least on Windows 10, trying to remove this key programmatically - e.g. with reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice /f fails due to permission issues, even when running as admin.

注意:

  • 代码仅针对当前用户重新定义命令,这可能是更好的方法,因为其他用户可能不希望这种更改,这会导致脚本意外执行.

  • The code redefines the command only for the current user, which is probably the better approach, because other users may not expect this change, which can result in unwanted execution of script.

  • 如果您仍然希望对 all 用户进行更改,请将 $forAllUsers 设置为 $true,如下,但请注意您然后必须运行代码提升(以管理员身份).
  • If you still want the make the change to all users, set $forAllUsers to $true, below, but note that you'll then have to run the code elevated (as admin).

与标准的Run in PowerShell 命令不同,下面的代码保持脚本执行的窗口打开和会话活动.

Unlike the standard Run in PowerShell command, the code below keeps the window in which the script executes open and the session alive.

任何有效的执行政策都将得到遵守;例如,如果 Restricted 生效,调用将完全失败.

Whatever execution policy is in effect will be honored; e.g., if Restricted is in effect, invocation will fail altogether.

  • 在下面的代码中,您可以在 -File 之前添加类似 -ExecutionPolicy RemoteSigned 的内容来覆盖有效策略,但我建议不要选择限制性较小的值出于安全原因.
  • In the code below, you can precede -File with something like -ExecutionPolicy RemoteSigned to override the effective policy, but I suggest not choosing a less restrictive value than that for security reasons.

与标准的Run in PowerShell 命令一样,脚本在它所在的目录中作为工作目录(当前位置)执行.

As with the standard Run in PowerShell command, the script executes in the directory in which it is located as the working directory (current location).

要自定义确切的调用命令,请修改下面的 $cmd = ... 行.

To customize the exact invocation command, modify the $cmd = ... line below.

  • 值得注意的是,要针对 PowerShell Core 而不是 Windows PowerShell,请将 powershell.exe 替换为 pwsh.exe.
  • Notably, to target PowerShell Core instead of Windows PowerShell, replace powershell.exe with pwsh.exe.
# Specify if the change should apply to the CURRENT USER only, or to ALL users.
# NOTE: If you set this to $true - which is NOT ADVISABLE - 
#       you'll need to run this code command ELEVATED (as administrator)
$forAllUsers = $false

# Determine the chosen scope's target registry key path.
$targetKey = "$(('HKCU', 'HKLM')[$forAllUsers]):\Software\Classes\Microsoft.PowerShellScript.1\shell\Open\Command"

# In the user-specific hive (HKCU: == HKEY_CURRENT_USER), the target key
# doesn't exist by default (whereas it does in the local-machine hive (HLKM: == HKEY_LOCAL_MACHINE)),
# so we need to make sure that it exists.
if (-not $forAllUsers) {
  $null = New-Item -Force $targetKey -ErrorAction Stop
}

# Specify the command to use when opening / double-clicking *.ps1 scripts:
# As written here:
#   * The profiles are loaded (add -NoProfile to change).
#   * The current execution policy is respected (add -ExecutionPolicy <policy>)
#   * The window stays open after the script exits (remove -NoExit to change)
#   * Windows PowerShell is targeted - to target PowerShell *Core* instead,
#     replace 'powershell.exe' with 'pwsh.exe'
# For help with all parameters, see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe
$cmd = "`"$((Get-Command powershell.exe)[0].Source)`" -noexit -file `"%1`""

#"# Write the command to the registry.
Set-ItemProperty -LiteralPath $targetKey -Name '(default)' -Value $cmd

<小时>

预定义的Run in PowerShell快捷菜单命令:

它在注册表项 HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell\0\Command 中定义(从 Windows 10 开始)如下:


The predefined Run in PowerShell shortcut-menu command:

It is defined in registry key HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell\0\Command (as of Windows 10) as follows:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"

  • 除非执行策略 AllSigned 生效 - 在这种情况下,只有 签名 脚本可以执行,但会被执行 不提示 - 该命令尝试将被调用进程的执行策略设置为Bypass,这意味着可以执行任何脚本,但只能执行在用户响应确认提示之前(无论脚本是否已签名,也无论是否从网络下载).

    • Unless execution policy AllSigned is in effect - in which case only signed scripts can be executed but are executed without prompting - the command attempts to set the execution policy for the invoked process to Bypass, which means that any script can be executed, but only after the user responds to a confirmation prompt beforehand (irrespective of whether the script is signed or not, and whether it was downloaded from the web or not).

      • 至少在早期的 Windows 7 版本/PowerShell 版本中,该命令被错误定义[1] 以一种有效地忽略了设置进程执行策略的尝试的方式,这意味着应用了持久配置的任何执行策略 - 并且没有显示确认提示.
      • At least in earlier Windows 7 releases / PowerShell versions, the command was misdefined[1] in a way that effectively ignored the attempt to set the process' execution policy, which meant that whatever execution policy was persistently configured applied - and no confirmation prompt was shown.

      除非目标脚本在退出前显式暂停以等待用户输入,否则脚本完成后脚本将自动关闭的窗口,因此您可能看不到它的输出.

      Unless the targeted script explicitly pauses to wait for user input before exiting, the window in which the script will close automatically when the script finishes, so you may not get to see its output.

      目标脚本在它所在的目录中执行作为工作目录(当前位置)

      The targeted script executes in the directory in which it is located as the working directory (current location)

      [1] 较早的、损坏的命令定义是 "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" "-Command" "if((Get-ExecutionPolicy ) -ne AllSigned) { Set-ExecutionPolicy -Scope Process Bypass }",这意味着 -file "%1" 之后的任何内容都作为 传递文件 "%1" 的参数,而不是 -Command 之后的命令的预期执行;另外 - 一个有争议的问题 - AllSigned 操作数需要被引用.

      [1] The earlier, broken command definition was "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" "-Command" "if((Get-ExecutionPolicy ) -ne AllSigned) { Set-ExecutionPolicy -Scope Process Bypass }", which meant what anything after -file "%1" was passed as arguments to file "%1" instead of the intended execution of the commands following -Command; additionally - a moot point - the AllSigned operand would have need to be quoted.

      这篇关于如何在电源外壳中自动打开多个电源外壳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆