【问题标题】:Open Powershell in a specific directory from shortcut从快捷方式打开特定目录中的 Powershell
【发布时间】:2012-12-23 09:47:49
【问题描述】:

这听起来应该很简单……我一定是个笨蛋。

我想要的只是制作一个 Windows 快捷方式,将 Powershell 打开到特定目录:

我正在使用目标:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
    -noexit -command {cd c:/path/to/open}

把它作为文本吐出命令。

【问题讨论】:

    标签: powershell windows-7 shortcuts


    【解决方案1】:

    如果您使用Powershell 7 (pwsh),只需像这样使用-WorkingDirectory 标志:

    pwsh -WorkingDirectory "C:\path\to\your\directory"
    

    【讨论】:

      【解决方案2】:

      我只是想添加我的 Developer Powershell 链接...作为记录。

      C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell d998f19b; cd c:\dev\}"
      

      这将在c:\dev\ 中启动 Developer Powershell (VS 2019)。

      【讨论】:

      • 在 Total Commander 中打开 this 目录:使用此命令添加新按钮 C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe 和这些参数:-noe -c "&{Import-Module """C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell YOUR-VS-HEX-CODE; Set-Location '%P'}"
      【解决方案3】:

      使用这个命令。

      powershell.exe -noexit -command "cd c:\temp"
      

      -NoExit:运行启动命令后不要退出。

      【讨论】:

      • 如果路径中有空格,您将需要包含's:powershell.exe -noexit -command "cd 'c:\a path with spaces\readme.txt'"
      • 正是我想要的。额外的引号是必需的,包括在使用插值路径时。
      • 如果我们谈论的是 any 类型的快捷方式(如提示和技巧),就是这样:如果您在 Windows 资源管理器中的所需文件夹中,您可以在地址栏中键入 powershell,它将在该位置打开 PowerShell。这也适用于cmd 和 Windows 可以通过 PATH 环境变量找到的任何其他应用程序。
      • 知道为什么这些解决方案在黑色 CMD 样式窗口而不是标准蓝色 PS 窗口中打开 PS 吗?
      • 谢谢伙计,我用它来避免在 Android Studio 中使用由于错误而裁剪命令的终端。
      【解决方案4】:

      为Powershell定义一个快捷方式,并打开它的属性,最后在“开始”中输入触发Powershell快捷方式时要打开的文件夹目标

      【讨论】:

      • 在程序中转到已安装的 .exe 文件,然后右键单击属性。然后,添加快捷方式
      • 您也可以在任何文件资源管理器窗口中右键单击并“创建快捷方式”,然后键入“powershell.exe”作为目标。
      【解决方案5】:

      如果需要资源管理器右键单击选项,请运行此脚本:

      New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
      if(-not (Test-Path -Path "HKCR:\Directory\shell\$KeyName"))
      {
          Try
          {
              New-Item -itemType String "HKCR:\Directory\shell\$KeyName" -value "Open PowerShell in this Folder" -ErrorAction Stop
              New-Item -itemType String "HKCR:\Directory\shell\$KeyName\command" -value "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command Set-Location '%V'" -ErrorAction Stop
              Write-Host "Successfully!"
           }
           Catch
           {
               Write-Error $_.Exception.Message
           }
      }
      else
      {
          Write-Warning "The specified key name already exists. Type another name and try again."
      }
      

      这是现在显示的内容:


      请注意,您可以从how to start PowerShell from Windows Explorer下载详细脚本。

      【讨论】:

        【解决方案6】:

        如果您希望 powershell 以管理员身份启动并在特定目录中运行,即使在不同的驱动器上,最好使用 Set-Location 命令。请按照以下步骤操作

        1. 创建一个 ShortCutLink,目标是 powershellcommand exe。
        2. Start in: 留空。 (通常在空白时从当前工作目录开始;但我们不在乎。
        3. Target 更改为您的 powershell 和位置目标:

          C:\Windows\...\v1.0\powershell.exe -noexit -command "Set-Location D:\_DCode\Main"

        4. 单击Advanced... 并选择Run as administrator
        5. 点击OKs 退出。

        不要忘记从Colors 选项卡更改快捷方式颜色的便捷技巧。这样,如果您有两个或多个打开 powershell 窗口的链接,看到不同的颜色可以直观地让您知道哪个 shell 正在工作。

        【讨论】:

        • 这是唯一对我有用的——谢谢。对于(我的)未来参考,powershell.exe 的完整路径是C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
        【解决方案7】:

        将此代码复制到记事本并使用 reg 扩展名保存。 双击生成的文件。如果您收到有关导入注册表的消息,请单击是,然后确定。 导航到资源管理器中的任何文件夹并调出上下文菜单。这通常通过单击鼠标右键来完成。


        Windows Registry Editor Version 5.00
        
        [HKEY_CLASSES_ROOT\Directory\Background\shell\PShell]
        "MUIVerb"="Open in Powershell Window"
        
        [HKEY_CLASSES_ROOT\Directory\Background\shell\PShell\command]
        @="c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
        

        【讨论】:

          【解决方案8】:

          您还可以将“开始于”快捷方式字段设置为您想要的位置。

          【讨论】:

          • 迄今为止最简单的解决方案,特别是如果您尝试使用带有空格或奇数字符的路径(例如 Program Files (x86) 下的任何内容)
          • “开始于”对我不起作用,仍然在 c:\windows\system32 开始。
          • 是的,如果快捷方式选中了“以管理员身份运行”,这将不起作用。
          • Start in 字段留空会更酷,因为它会在当前位置打开。
          • 这是最适合我的解决方案。我制作了现有快捷方式的副本,将其命名为“- Vagrant”,然后将“Start in”路径设置为我的 VagrantFile 所在的位置。现在我只需点击快捷方式并“流浪”就可以开始工作了。
          【解决方案9】:

          尝试:

          %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
          -noexit -command "cd c:/path/to/open"
          

          【讨论】:

            【解决方案10】:

            好的 - 您需要使用 & 参数来指定它是一个 powershell 命令并且语法略有不同:

            %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
            -noexit -command "& {cd c:\path\to\open}"
            

            【讨论】:

            • 这个和the solution by Kayasak有区别吗?
            • 我在Hyper.hyper.js 中使用PowerShell,我使用了这个解决方案,例如:shellArgs: ['-noexit', '& {cd "$HOME\\my old documents"}']。我无法让['-noexit', '-command', '"cd "$HOME\\my old documents"']['-noexit', '-command "cd "$HOME\\my old documents"'] 工作。
            • 旧帖子,但只是帮助了我 :) 我厌倦了以管理员身份卡在 system32 目录中。谢谢
            猜你喜欢
            • 1970-01-01
            • 2015-10-06
            • 2014-10-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-12
            • 2021-10-04
            相关资源
            最近更新 更多