Add Open PowerShell window here as administrator in Windows 10  

Page 8 of 10 FirstFirst ... 678910 LastLast

  1. Posts : 72,302
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #70

    LesFerch said:
    @Brink I saw that there were a number of iterations of this context menu item script. It looked like every topic had been covered, so I was a little surprised to find that there are several characters, that are valid for folder paths, that will result in an error. Those characters are $, [, and ` Here are some example folder paths that will error out:

    $a b
    [a b
    `a b

    So, I set about to create a one-liner script that could handle all valid Windows folder paths. It turns out to be an almost "you can't get there from here" problem.
    Thank you. I'll do some playing with it today.
      My Computers


  2. Posts : 828
    Windows 10/11
       #71

    Brink said:
    Thank you. I'll do some playing with it today.
    Great! I'll be around in case any questions come up.
      My Computer


  3. Posts : 72,302
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #72

    LesFerch said:
    Great! I'll be around in case any questions come up.
    Looking in the registry in W11, it used the command below for the default "Open PowerShell windows here" context menu for comparison.

    powershell.exe -noexit -command Set-Location -literalPath '%V'
      My Computers


  4. Posts : 828
    Windows 10/11
       #73

    Brink said:
    powershell.exe -noexit -command Set-Location -literalPath '%V'
    Microsoft can't even get it right. That command fails on any path that contains a single quote character, such as Tom's birthday:
    Add Open PowerShell window here as administrator in Windows 10-1.png
      My Computer


  5. Posts : 72,302
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #74

    LesFerch said:
    Microsoft can't even get it right. That command fails on any path that contains a single quote character, such as Tom's birthday:
    Add Open PowerShell window here as administrator in Windows 10-1.png
      My Computers


  6. Posts : 72,302
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #75

    Great work @LesFerch. Your command works much better. Especially with longer names. The tutorial has now been updated to use your command in the context menu instead.
      My Computers


  7. Posts : 977
    Windows 7
       #76

    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
    Add Open PowerShell window here as administrator in Windows 10-image.png

    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
      My Computer


  8. Posts : 828
    Windows 10/11
       #77

    garlin said:
    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 pass the echo directly into PowerShell as the $Input variable, and then escape all single quotes with a pair of single quotes.
    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.
      My Computer


  9. Posts : 828
    Windows 10/11
       #78

    garlin said:
    Code:
    cmd /c echo "%V%" | powershell $Path = $Input.Trim() -replace '''',''''''; Start-Process powershell -ArgumentList $('-NoExit -Command "Set-Location -LiteralPath ''' + $Path + '''"') -Verb RunAs
    Darn. Unicode characters break this solution.

    Also, the pipe is really slow, so I'll try to avoid that.
      My Computer


  10. Posts : 977
    Windows 7
       #79

    Why not? It doesn't correctly display the path until you fix the Code Page.

    Add Open PowerShell window here as administrator in Windows 10-image.png
      My Computer


 

Tutorial Categories

Add Open PowerShell window here as administrator in Windows 10 Tutorial Index Network & Sharing Instalation and Upgrade Browsers and Email General Tips Gaming Customization Apps and Features Virtualization BSOD System Security User Accounts Hardware and Drivers Updates and Activation Backup and Restore Performance and Maintenance Mixed Reality Phone


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 00:31.
Find Us




Windows 10 Forums