我想在Windows资源管理器上下文菜单中添加一个项目,该菜单会以管理员身份打开PowerShell。
我发现这个命令启动了cmd中的powershell:
PowerShell -windowstyle hidden -Command `"Start-Process cmd -ArgumentList '/s,/k,pushd,%V && PowerShell' -Verb RunAs`"
这个问题是窗口是cmd窗口而不是powershell窗口。
所以我试着自己做一个命令然后想出来:
PowerShell.exe -WindowStyle Hidden -NoExit -Command "Start-Process PowerShell.exe -Verb RunAs -ArgumentList @(`'-NoExit`',`'-Command`',`'Set-Location -LiteralPath `"%V`"`')";
在cmd或powershell中执行时效果很好,但是当我将它放入注册表(HKEY_CLASSES_ROOT\Directory\shell\PowershellMenu (Administrator)\command\(Default)
)时,powershell窗口很快就会闪烁。
我试着......
...使用完整的PowerShell路径:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -NoExit -Command "Start-Process -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Verb RunAs -ArgumentList @(`'-NoExit`',`'-Command`',`'Set-Location -LiteralPath `"%V`"`')";
...添加一个睡眠以查看-NoExit
是否被忽略:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -NoExit -Command "Start-Process -FilePath 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Verb RunAs -ArgumentList @(`'-NoExit`',`'-Command`',`'Set-Location -LiteralPath `"%V`"; Sleep 10`')";
没有任何成功。
答案 0 :(得分:0)
如果我理解正确,那应该可以:
Windows Registry Editor Version 5.00
; powershell
[HKEY_CLASSES_ROOT\Directory\Background\shell\powershell-as-admin]
@="PowerShell"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe,0"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\Directory\Background\shell\powershell-as-admin\command]
@="powershell -WindowStyle Hidden -Command \"Start-Process cmd -WindowStyle Maximized -ArgumentList '/s,/k,pushd,%V && powershell' -Verb RunAs\""
[HKEY_CLASSES_ROOT\Directory\shell\powershell-as-admin]
@="PowerShell"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe,0"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\Directory\shell\powershell-as-admin\command]
@="powershell -WindowStyle Hidden -Command \"Start-Process cmd -WindowStyle Maximized -ArgumentList '/s,/k,pushd,%V && powershell' -Verb RunAs\""
[HKEY_CLASSES_ROOT\Drive\shell\powershell-as-admin]
@="PowerShell"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe,0"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\Drive\shell\powershell-as-admin\command]
@="powershell -WindowStyle Hidden -Command \"Start-Process cmd -WindowStyle Maximized -ArgumentList '/s,/k,pushd,%V && powershell' -Verb RunAs\""
[HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\powershell-as-admin]
@="PowerShell"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe,0"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\powershell-as-admin\command]
@="powershell -WindowStyle Hidden -Command \"Start-Process cmd -WindowStyle Maximized -ArgumentList '/s,/k,pushd,%V && powershell' -Verb RunAs\""
有一个缺点-Powershell窗口出现,从该窗口以管理员身份启动Powershell。
答案 1 :(得分:0)