New
#50
Hi. This doesn't work on folders that has single quote character ' in their names, e.g.It looks like a PowerShell window is trying to run, but it'll disappear eventually. FYI, I'm on LTSC 2019.Code:Oma'sNest
Hi. This doesn't work on folders that has single quote character ' in their names, e.g.It looks like a PowerShell window is trying to run, but it'll disappear eventually. FYI, I'm on LTSC 2019.Code:Oma'sNest
I have been pulling my hair out trying to find a solution for this. Powershell 7 doesn't have this problem:
C:\Program Files\PowerShell\7\pwsh.exe -WindowStyle Hidden -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%V!" -Command "Start-Process 'C:\Program Files\PowerShell\7\pwsh.exe' -Verb runAS;Exit"
But I can't for the life of me figure out how to get Powershell 5 to open as Admin to the current folder if the folder has an apostrophe present. Does anyone have a working command for this?
- - - Updated - - -
So someone on the PowerShell Discord came up with a pretty crazy method that works. Figured I would share it here. But I'm warning you... it's not pretty.
Here's the registry value:
It was created by the following:Code:powershell -EncodedCommand JABjAG8AbQBtAGEAbgBkACAAPQAgAFsAYwBvAG4AdgBlAHIAdABdADoAOgBUAG8AQgBhAHMAZQA2ADQAUwB0AHIAaQBuAGcAKABbAFMAeQBzAHQAZQBtAC4AVABlAHgAdAAuAEUAbgBjAG8AZABpAG4AZwBdADoAOgBVAG4AaQBjAG8AZABlAC4ARwBlAHQAQgB5AHQAZQBzACgAIgBTAGUAdAAtAEwAbwBjAGEAdABpAG8AbgAgAGAAIgAkACgARwBlAHQALQBMAG8AYwBhAHQAaQBvAG4AKQBgACIAIgApACkACgBTAHQAYQByAHQALQBQAHIAbwBjAGUAcwBzACAAUABvAHcAZQByAHMAaABlAGwAbAAgAC0AQQByAGcAdQBtAGUAbgB0AEwAaQBzAHQAIAAiAC0ARQBuAGMAbwBkAGUAZABDAG8AbQBtAGEAbgBkACAAJABjAG8AbQBtAGEAbgBkACAALQBOAG8ARQB4AGkAdAAiACAALQBWAGUAcgBiACAAcgB1AG4AYQBzAA==
Not the most elegant solution in the world, but it works!Code:$command = @' $command = [convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("Set-Location `"$(Get-Location)`"")) Start-Process Powershell -ArgumentList "-EncodedCommand $command -NoExit" -Verb runas '@ [convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($command))
I got one better for you guys. A StackOverflow user who has a ton of experience guided me to a much more robust solution:
Run Windows PowerShell (5.1) as admin (Even in folders with an apostrophe or special character):
Run CMD (Command Prompt) as admin (Even in folders with an apostrophe or special character):Code:powershell.exe -WindowStyle Hidden -NoProfile -Command "Start-Process -Verb RunAs powershell.exe -ArgumentList \"-NoExit -Command Push-Location \\\"\"%V/\\\"\"\"
All credit goes to mklement0 here: windows - What registry commands allow me to run PowerShell 5 / CMD as admin from the right click context menu from a folder that has an apostrophe in it? - Stack OverflowCode:powershell.exe -WindowStyle Hidden -NoProfile -Command "Start-Process -Verb RunAs cmd.exe -ArgumentList \"/k pushd \"\"%V/\"\"\"
It turns out that these commands (including the encoded one) have issues with folders containing`
or$
characters.
`
may not be popular, but$
is quite common (My$Money, Finance$Test).
The CMD tweak from this tutorial works perfectly, I have not found a situation where it doesn't work. Its command contains special parameters, don't know if it would help suggest some ideas for you:
Code:@="cmd /c echo|set/p=\"%V\"|powershell -NoP -W 1 -NonI -NoL \"SaPs 'cmd' -Args '/c \"\"\"cd /d',$([char]34+$Input+[char]34),'^&^& start /b cmd.exe\"\"\"' -Verb RunAs\""