我想在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`')";
没有任何成功。
如果我理解正确,这应该工作:
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。