我目前試圖創建一個開放從右鍵上下文菜單管理員PowerShell的命令。對於上下文,上下文菜單命令在CMD中運行。文字引號
我的問題是,我想cd
到哪裏右鍵出現的目錄。下面的命令對大多數目錄來說工作得很好,但是如果目錄路徑包含空格,那麼它只會嘗試移動到空格之前的路徑部分,並引發錯誤。我的理解是,當前目錄是通過%V
傳入但是當我運行使用相同的處理的命令echo %V
,其與空間分割路徑到2行,所以我假設被存儲在單獨的字符串的路徑的部分?
Powershell -noexit "Start-Process 'C:\Users\<me>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\Windows PowerShell.lnk' -ArgumentList '-noexit','Set-Location -literalPath `"%V`"' -Verb RunAs"
我已經更新了上面的命令下面搭配的建議,並在桌面上右鍵單擊(這在以前由於缺乏空間的工作),我現在得到以下錯誤:
Set-Location : Cannot find path 'C:\Users\<me>\Desktop`' because it does not exist.
At line:1 char:1
+ Set-Location -literalPath `C:\Users\<me>\Desktop`
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\<me>\Desktop`:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
請注意,在上述兩個代碼塊中,<me>
是我的實際用戶名。
我一直在撕裂我的頭髮試圖把路徑周圍的報價,但我似乎無法獲得PowerShell來把由於引號的事實,我已經使用了單引號和雙引號。
任何幫助將不勝感激,在此先感謝。
編輯:
對於那些仍然在尋找一個答案,我跑了以下PowerShell腳本功能性命令添加到我的上下文菜單:
$menu = 'Open Windows PowerShell Here as Administrator'
$command = "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'"""
'directory', 'directory\background', 'drive' | ForEach-Object {
New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell" -Name runas\command -Force |
Set-ItemProperty -Name '(default)' -Value $command -PassThru |
Set-ItemProperty -Path {$_.PSParentPath} -Name '(default)' -Value $menu -PassThru |
Set-ItemProperty -Name HasLUAShield -Value ''
}
答案是從How do I start PowerShell from Windows Explorer?
也許我是不是在這個問題清楚了,不好意思。我的問題是%V路徑中的空格,Powershell可執行文件很好。我嘗試在%V周圍放置'',但仍然收到錯誤。 – Spanner1122
確切的錯誤是什麼?你是否把''放在''的周圍? – Manu
我已經更新了上面的問題,以包含修改後使用的錯誤和完整命令。 – Spanner1122