Add Open PowerShell window here as administrator in Windows 10  

Page 9 of 10 FirstFirst ... 78910 LastLast

  1. Posts : 828
    Windows 10/11
       #80

    @garlin I get this error:
    Add Open PowerShell window here as administrator in Windows 10-1.png

    Works fine with this non-admin script
    cmd /c set "_dir=%v" & start powershell.exe -noE -c cd -literalP $env:_dir
    Add Open PowerShell window here as administrator in Windows 10-2.png
    Last edited by LesFerch; 19 Dec 2023 at 23:30.
      My Computer


  2. Posts : 828
    Windows 10/11
       #81

    @Brink and @garlin Here are the new scripts that work with all valid folder names and support Unicode.

    PowerShell Here uses a set command to pass the path.

    PowerShell Here As Admin uses an HKCU registry value to pass the path.

    The values are regular REG_SZ.

    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShell]
    @="@shell32.dll,-8508"
    "Icon"="powershell.exe"
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShell\command]
    @="cmd /c set \"_dir=%v\" & start powershell.exe -noE -c cd -literalP $env:_dir"
    
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin]
    @="@shell32.dll,-37448"
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin\command]
    @="cmd /c reg add hkcu\\software\\_dir /d \"%v\" /f & start powershell -WindowS H -noP -c Start-Process -v RunAs powershell.exe '-noL -noE -c cd -literalP (gp hkcu:\\software\\_dir).\\\"\\\"\\\"(default)\\\"\\\"\\\"'"
      My Computer


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

    LesFerch said:
    @Brink and @garlin Here are the new scripts that work with all valid folder names and support Unicode.

    PowerShell Here uses a set command to pass the path.

    PowerShell Here As Admin uses an HKCU registry value to pass the path.

    The values are regular REG_SZ.

    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShell]
    @="@shell32.dll,-8508"
    "Icon"="powershell.exe"
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShell\command]
    @="cmd /c set \"_dir=%v\" & start powershell.exe -noE -c cd -literalP $env:_dir"
    
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin]
    @="@shell32.dll,-37448"
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin\command]
    @="cmd /c reg add hkcu\\software\\_dir /d \"%v\" /f & start powershell -WindowS H -noP -c Start-Process -v RunAs powershell.exe '-noL -noE -c cd -literalP (gp hkcu:\\software\\_dir).\\\"\\\"\\\"(default)\\\"\\\"\\\"'"
    Thank you. Now updated.

    Any way to have it delete the HKEY_CURRENT_USER\Software\_dir key it creates afterwards?
      My Computers


  4. Posts : 828
    Windows 10/11
       #83

    Brink said:
    Thank you. Now updated.

    Any way to have it delete the HKEY_CURRENT_USER\Software\_dir key it creates afterwards?
    That should be possible, but I didn't consider it because I didn't see any harm in leaving it there.
      My Computer


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

    LesFerch said:
    That should be possible, but I didn't consider it because I didn't see any harm in leaving it there.
    No harm indeed, but some folks would prefer no traces left. Too bad it doesn't auto clean up like using the Temp folder.
      My Computers


  6. Posts : 828
    Windows 10/11
       #85

    Brink said:
    No harm indeed, but some folks would prefer no traces left. Too bad it doesn't auto clean up like using the Temp folder.
    No problem. Here you go:

    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin]
    @="@shell32.dll,-37448"
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin\command]
    @="cmd /c reg add hkcu\\software\\_dir /d \"%v\" /f & start powershell -WindowS H -noP -c Start-Process -v RunAs powershell.exe '-noL -noE -c cd -literalP (gp hkcu:\\software\\_dir).\\\"\\\"\\\"(default)\\\"\\\"\\\"; ri hkcu:\\software\\_dir'"
      My Computer


  7. Posts : 72,105
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #86

    LesFerch said:
    No problem. Here you go:

    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin]
    @="@shell32.dll,-37448"
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\PowerShellAsAdmin\command]
    @="cmd /c reg add hkcu\\software\\_dir /d \"%v\" /f & start powershell -WindowS H -noP -c Start-Process -v RunAs powershell.exe '-noL -noE -c cd -literalP (gp hkcu:\\software\\_dir).\\\"\\\"\\\"(default)\\\"\\\"\\\"; ri hkcu:\\software\\_dir'"
    Thank you again. Now updated.
      My Computers


  8. Posts : 1,208
    11 Home
       #87

    LesFerch said:
    I did examine your solution.
    So then, hadn't you noticed the fact that it saves the pathstring to a separate textfile to avoid any issues with special characters? To clarify, here's what you wrote in your post I quoted:
    So, after almost giving up, I realized the only way to solve it (short of going with an external script or exe) is to pass the folder path via a temp file.
    Emphasis in bold is mine.
    It's similar to one I've implemented myself and works very well, as long as Windows Script Host has not been disabled and VBScript has not been uninstalled. The version I wrote uses JScript, so it won't be affected in the future by VBScript becoming an optional install or being removed altogether. However, it's still not going to work on machines where WSH has been disabled.
    The VBScript's only purpose is to hide the command window that otherwise would briefly appear. As you know, this VBScript can very easily be eliminated (i.e. at the sacrifice of letting the command window briefly appear), but anyway... that was not the point I was trying to make in my reply, and in fact I see no JScript anywhere in your post I quoted so, I am afraid you lost me here.
    The goal here was to provide a replacement one-liner for the registry that only uses Cmd and PowerShell and will work with any valid Windows folder name.
    I know, and, that also is why I wasn't asking you what was your goal.
      My Computers


  9. Posts : 828
    Windows 10/11
       #88

    @hdmi The JScript version I referred to is just a solution I did just for myself that was similar to yours. I had initially dismissed the idea of using a file as not suitable for a one-liner, and got rather focused on trying to make a variable work before circling back and realizing a file could be used in a one-liner solution. And I did also previously (now edited) incorrectly say that's the only solution. I've now realized that a set variable is also a good solution for a non-admin launch and registry value works well for RunAs. I'm sure my wording could have been better. I apologize for the inconsistency.
      My Computer


  10. Posts : 1,208
    11 Home
       #89

    LesFerch said:
    @hdmi The JScript version I referred to is just a solution I did just for myself that was similar to yours. I had initially dismissed the idea of using a file as not suitable for a one-liner, and got rather focused on trying to make a variable work before circling back and realizing a file could be used in a one-liner solution. And I did also previously (now edited) incorrectly say that's the only solution. I've now realized that a set variable is also a good solution for a non-admin launch and registry value works well for RunAs. I'm sure my wording could have been better. I apologize for the inconsistency.
    Be aware that cmd.exe is limited to 8,191 characters. https://learn.microsoft.com/en-us/tr...ing-limitation
    Whereas the maximum path length on Windows 10 version 1607 and later can be up to (approximately) 32,767 characters. https://learn.microsoft.com/en-us/wi...ath-limitation
    That being said, the setx command with the /m parameter can be used to set a system environment variable, which survives the "RunAs" part, but the command is limited to 1024 characters. So, your approach that uses reg.exe certainly is better in that regard, but still doesn't solve the potential problem of cmd.exe being limited to 8,191 characters. That's the other reason why I had decided to use a VBScript. command line - Is there a limit in the text length that you can use as a parameter? If yes whats the limit? - Stack Overflow
      My Computers


 

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 05:33.
Find Us




Windows 10 Forums