New
#70
Brink,
Last night I was mulling over @LesFerch's solution, but realized it's possible to take his "cmd /c echo" idea and end up with a more simplified answer. You pipe the echo directly into PowerShell as the $Input variable, and then escape all single quotes with a pair of single quotes.
This removes the need for any temp files, and fixes the "unparseable" characters.
Code:cmd /c echo "%V%" | powershell $Path = $Input.Trim() -replace '''',''''''; Start-Process powershell -ArgumentList $('-NoExit -Command "Set-Location -LiteralPath ''' + $Path + '''"') -Verb RunAs
Code:Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin] @="Open PowerShell window here as administrator" "Extended"=- "HasLUAShield"="" "Icon"="powershell.exe" [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command] @="cmd /c echo \"%V%\" | powershell $Path = $Input.Trim() -replace '''',''''''; Start-Process powershell -ArgumentList $('-NoExit -Command \"Set-Location -LiteralPath ''' + $Path + '''\"') -Verb RunAs" [HKEY_CLASSES_ROOT\Directory\shell\PowerShellAsAdmin] @="Open PowerShell window here as administrator" "Extended"=- "HasLUAShield"="" "Icon"="powershell.exe" [HKEY_CLASSES_ROOT\Directory\shell\PowerShellAsAdmin\command] @="cmd /c echo \"%V%\" | powershell $Path = $Input.Trim() -replace '''',''''''; Start-Process powershell -ArgumentList $('-NoExit -Command \"Set-Location -LiteralPath ''' + $Path + '''\"') -Verb RunAs" [HKEY_CLASSES_ROOT\Drive\shell\PowerShellAsAdmin] @="Open PowerShell window here as administrator" "Extended"=- "HasLUAShield"="" "Icon"="powershell.exe" [HKEY_CLASSES_ROOT\Drive\shell\PowerShellAsAdmin\command] @="cmd /c echo \"%V%\" | powershell $Path = $Input.Trim() -replace '''',''''''; Start-Process powershell -ArgumentList $('-NoExit -Command \"Set-Location -LiteralPath ''' + $Path + '''\"') -Verb RunAs" [-HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\PowerShellAsAdmin] ; To allow mapped drives to be available in elevated PowerShell [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System] "EnableLinkedConnections"=dword:00000001
Interesting! I'm actually still working on this myself because I just realized about an hour ago that my "solution" has a fatal flaw. It does not handle Unicode characters! For the non-admin PowerShell item, I realized that I could just set a variable, but that does not work with the RunAs, so I was exploring using an HKCU registry entry, but your solution may be better. I'll test it. Thanks!
@Brink Hang on. We're not done yet. Sorry.