"Run as TrustedInstaller" from Context menu

Discussion in 'Scripting' started by freddie-o, Dec 3, 2019.

  1. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,152
    1,538
    60
    #1 freddie-o, Dec 3, 2019
    Last edited: Jun 23, 2022
    Right click any Windows Process and run them as TrustedInstaller / NT Authority\System.
    All we need is PowerRun to elevate a Windows process to TrustedInstaller / NT Authority\System privileges.

    Run as TrustedInstaller from Context menu.png


    About
    This automated batch script downloads the latest PowerRun from (sordum.org) website and adds (installs) "Run as TrustedInstaller" to the Context Menu


    Credits

    @wtarkan for PowerRun
    @inTerActionVRI and @BAU for helping with the script



    Batch script
    Code:
    @echo OFF
    
    @reg query HKU\S-1-5-19>nul 2>nul||(set 0="%~f0" %*&powershell -nop -c start cmd -args '/x/d/q/rcall',$env:0 -v runas&exit)
    
    :menu
    cd /d "%~dp0"
    
    if /i "%PROCESSOR_ARCHITECTURE%" equ "amd64" (set "arch=x64") else (set "arch=%PROCESSOR_ARCHITECTURE%")
    if "%PROCESSOR_ARCHITEW6432%" neq "" if /i "%arch%" neq "x86" if /i "%arch%" neq "x64" start %SystemRoot%\Sysnative\cmd.exe /C "%~dpnx0" %* & exit
    
    set "ROOT=%~dp0"
    set "DownloadFileName=PowerRun.zip"
    set "DownloadURI=https://www.sordum.org/files/download/power-run/PowerRun.zip"
    
    SetLocal EnableExtensions EnableDelayedExpansion
    
    echo.
    echo ____________________________________________________________
    echo.
    echo       "Run as TrustedInstaller" from the Context Menu
    echo ____________________________________________________________
    echo.
    echo.
    echo  [1]  Add "Run as TrustedInstaller" to the Context menu
    echo.
    echo  [2]  Remove "Run as TrustedInstaller" from the Context menu
    echo.
    echo.
    echo. [Q]  Quit
    echo.
    echo.
    echo ____________________________________________________________
    echo.
    echo.
    choice /C:12Q /N /M "Enter your choice :"
    if %errorlevel% equ 3 exit
    if %errorlevel% equ 2 (
       if exist "%SystemROOT%\System32\PowerRun.exe" (
           del /q %SystemRoot%\system32\PowerRun.exe >NUL
           if not %arch%==x86 if not %arch%==arm del /q %SystemRoot%\SysWOW64\PowerRun.exe >NUL
           reg delete "HKCR\*\shell\Run as TrustedInstaller" /f >nul 2>&1
           echo.
           echo.Successfully uninstalled PowerRun and removed "Run as TrustedInstaller" from the Context menu.
       ) else (
           echo.
           echo.PowerRun has already been removed.
       )
    )
    if %errorlevel% equ 1 (
       if not exist "%SystemROOT%\System32\PowerRun.exe" (
           @start "Downloading !DownloadFileName! file..." /wait /B powershell -c "(New-Object System.Net.WebClient).DownloadFile('!DownloadURI!', '%ROOT%\!DownloadFileName!')"
           if exist "%ROOT%\!DownloadFileName!" @start "Expanding !DownloadFileName! file..." /wait /B powershell -c "Expand-Archive -Force '%ROOT%\!DownloadFileName!' '%ROOT%'"
            if exist "PowerRun\PowerRun.exe" (
               if %arch%==x64 (
                   copy PowerRun\PowerRun_x64.exe %SystemRoot%\system32\PowerRun.exe /y >NUL
                   copy PowerRun\PowerRun.exe %SystemRoot%\SysWOW64\PowerRun.exe /y >NUL
               ) else (
                   copy PowerRun\PowerRun.exe %SystemRoot%\system32\PowerRun.exe /y >NUL
               )
               reg add "HKCR\*\shell\Run as TrustedInstaller" /v "Icon" /t REG_SZ /d "imageres.dll,1" /f >NUL
               reg add "HKCR\*\shell\Run as TrustedInstaller\command" /v "" /t REG_EXPAND_SZ /d "cmd.exe /X /D /U /R for %%%%# in (\"%%1\") do PowerRun.exe /WD:\"%%%%~dp#\" \"%%1\"" /f >nul 2>&1
               echo.
               echo.Successfully installed PowerRun and added "Run as TrustedInstaller" to the Context menu.
           )
    rmdir /q /s "PowerRun" >nul
    del /q /f "!DownloadFileName!" >nul
       ) else (
           echo.
           echo.PowerRun is already installed.
       )
    )
    echo.
    echo Press any key to make another choice...
    pause >nul
    cls
    goto menu
    





    REG file
    Alternatively you can just use a REG file.
    * Do not move or delete the PowerRun folder
    * Modify the path of your PowerRun folder.


    x64
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\*\shell\Run as TrustedInstaller]
    "Icon"="imageres.dll,1"
    
    [HKEY_CLASSES_ROOT\*\shell\Run as TrustedInstaller\command]
    @="X:\\Path of your PowerRun Folder\\PowerRun_x64.exe \"%1\""
    


    x86
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\*\shell\Run as TrustedInstaller]
    "Icon"="imageres.dll,1"
    
    [HKEY_CLASSES_ROOT\*\shell\Run as TrustedInstaller\command]
    @="X:\\Path of your PowerRun Folder\\PowerRun.exe \"%1\""
    


    + + +​




    Bonus
    Fix "Run as administrator"


     
  2. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,152
    1,538
    60
  3. Mouri_Naruto

    Mouri_Naruto MDL Developer

    Jul 10, 2014
    495
    1,659
    10
    I need some "programs and scripts would not run or would hang using NSudo" for testing and try to solve the issue.
     
  4. boyonthebus

    boyonthebus MDL Expert

    Sep 16, 2018
    1,104
    699
    60
    I have not experienced this. All my programs (the ones I have tried) run fine with Nsudo.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,152
    1,538
    60
    #5 freddie-o, Dec 4, 2019
    Last edited: Dec 4, 2019
    (OP)
    EDIT:

    Hi. This is the script I used.

    Code:
    @echo off
    if defined PROCESSOR_ARCHITEW6432 start %SystemRoot%\Sysnative\cmd.exe /c "%~dpnx0" %* & exit
    cls
    >NUL 2>&1 net.exe session ||(
      echo.Run as Admin !!!
        >nul pause
    exit
    )
    cd /d "%~dp0"
    set "arch=x64"
    if /i %PROCESSOR_ARCHITECTURE%==x86 if not defined PROCESSOR_ARCHITEW6432 set "arch=x86"
    if %arch%==x64 (
      copy NSudoG.exe %SystemRoot%\system32\NSudoG.exe /y
      copy NSudoG.exe %SystemRoot%\SysWOW64\NSudoG.exe /y
    ) else (
      copy NSudoG.exe %SystemRoot%\system32\NSudoG.exe /y
    )
    Reg.exe add "HKCR\*\shell\runastrustedinstaller" /v "" /t REG_SZ /d "Run as TrustedInstaller" /f
    Reg.exe add "HKCR\*\shell\runastrustedinstaller" /v "Description" /t REG_SZ /d "Run as TrustedInstaller" /f
    Reg.exe add "HKCR\*\shell\runastrustedinstaller" /v "Icon" /t REG_SZ /d "imageres.dll,73" /f
    Reg.exe add "HKCR\*\shell\runastrustedinstaller" /v "MUIVerb" /t REG_SZ /d "Run as TrustedInstaller" /f
    Reg.exe add "HKCR\*\shell\runastrustedinstaller\command" /v "" /t REG_SZ /d "NSudoG.exe -U:T -P:E \"%%1\"" /f
    echo.Install completed.
    >nul pause
    
    

    I tried these other parameters too...

    Code:
    NSudoG.exe -U:T -P:E -M:S
    
    NSudoG.exe -U:T -ShowWindowMode:Hide
    
    NSudoG.exe -U:T -P:E -ShowWindowMode:Hide
    
    NSudoG.exe -U:T -P:E -M:S -ShowWindowMode:Hide
    
    
    Using NSudo, NSudoC and NSudoG

    Which caused some programs to hang, error installing my .BAT script and .REG files not able to merge.

    After testing some more, I tried this

    Code:
    NSudoG.exe -U:T
    
    
    This time it worked. The programs no longer hang. :)


    EDIT:

    The problem now is... one of my .BAT script that merges all my .REG files still has an error and .REG files still don't merge





     
  6. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,152
    1,538
    60
    There is no problem with NSudo.exe GUI. Only with this script.
     
  7. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    361
    610
    10
    #7 Thomas Dubreuil, Dec 4, 2019
    Last edited: Dec 4, 2019
    Use NSudo.ContextMenu.Launcher maybe ?

    Not sure such effort is needed for NSudo, since you can easyinstall NSudo Context menu with NSudo Installer, which link is also on official NSudo Download page.
    One of the advantages of the installer is
    -You can have NSudo in any location (+ it adds NSudo path to environment variables path),
    -The context menu options between:
    All files (HKEY_CLASSES_ROOT*\shell\NSudo)
    exe files and batch scripts: .exe .bat .cmd .inf .ps1 .py .reg .vbs
    exe files only
    -Few different options like start menu shortcut, tile icons, custom json (more programs), uninstall support via programs and features etc.
    -It is pure batch

    Having a look at the script, you can see how I implement NSudo context menu, for example
    Code:
    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\NSudo.RunAs.TrustedInstaller.EnableAllPrivileges\command" /ve /t REG_SZ /d "\"%NSudoFolder%\NSudo.exe\" -U:T -P:E -ShowWindowMode=Hide cmd /c start \"NSudo.ContextMenu.Launcher\" \"%%1\"" /f
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,152
    1,538
    60
    #8 freddie-o, Dec 5, 2019
    Last edited: Dec 5, 2019
    (OP)
    Works. Except for this one batch script of mine that merges all my reg files...

    When I Run As TrustedInstaller. Run As System and Run As System (Enable All Privileges), this shows...
    NSudo.jpg


    When I Run As TrustedInstaller (Enable All Privileges), nothing happens?

    But the batch script runs OK on my Run as TrustedInstaller script.

    EDIT:

    Also all I really need is to Run as TrustedInstaller.
     
  9. JeepWillys58

    JeepWillys58 MDL Senior Member

    Nov 6, 2010
    414
    183
    10
    #9 JeepWillys58, Nov 23, 2021
    Last edited: Nov 23, 2021
    Hi dear friend @freddie-o ,

    I would like to add a little addendum to your PowerRun install and uninstall scripts to make it easier to use...

    PowerRun Install by @freddie-o

    Code:
    @echo off
    
    REM change wording if needed..
    TITLE PowerRun Install by @freddie-o
    echo Checking for admin rights...
    Echo.
    Echo.
    Echo  ....Please click on "Yes"....
    
    reg.exe query HKU\S-1-5-19 1>nul 2>nul && goto :gotAdmin
    if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
    
    :gotAdmin
        pushd "%~dp0"
    cls
    if defined PROCESSOR_ARCHITEW6432 start %SystemRoot%\Sysnative\cmd.exe /c "%~dpnx0" %* & exit
    cls
    >NUL 2>&1 REG QUERY "HKU\S-1-5-19" ||(
      echo.Run as Admin !
       >nul pause
    exit
    )
    cd /d "%~dp0"
    set "arch=x64"
    if /i %PROCESSOR_ARCHITECTURE%==x86 if not defined PROCESSOR_ARCHITEW6432 set "arch=x86"
    if %arch%==x64 (
      copy PowerRun_x64.exe %SystemRoot%\system32\PowerRun.exe /y
      copy PowerRun.exe %SystemRoot%\SysWOW64\PowerRun.exe /y
    ) else (
      copy PowerRun.exe %SystemRoot%\system32\PowerRun.exe /y
    )
    Reg add "HKCR\*\shell\Run as TrustedInstaller"
    Reg add "HKCR\*\shell\Run as TrustedInstaller" /v "Icon" /t REG_SZ /d "imageres.dll,1" /f
    Reg add "HKCR\*\shell\Run as TrustedInstaller\command" /v "" /t REG_SZ /d "PowerRun.exe \"%%1\"" /f
    echo.Install completed.
    timeout /t 3 >nul
    exit
    
    PowerRun Uninstall by @freddie-o
    Code:
    @echo off
    
    REM change wording if needed..
    TITLE PowerRun Uninstall by @freddie-o
    echo Checking for admin rights...
    Echo.
    Echo.
    Echo  ....Please click on "Yes".....
    
    reg.exe query HKU\S-1-5-19 1>nul 2>nul && goto :gotAdmin
    if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
    
    :gotAdmin
        pushd "%~dp0"
    cls
    if defined PROCESSOR_ARCHITEW6432 start %SystemRoot%\Sysnative\cmd.exe /c "%~dpnx0" %* & exit
    cls
    >NUL 2>&1 REG QUERY "HKU\S-1-5-19" ||(
      echo.Run as Admin !!!
      >nul pause
      exit
    )
    cd /d "%~dp0"
    set "arch=x64"
    if /i %PROCESSOR_ARCHITECTURE%==x86 if not defined PROCESSOR_ARCHITEW6432 set "arch=x86"
    if %arch%==x64 (
      del /q %SystemRoot%\system32\PowerRun.exe
      del /q %SystemRoot%\SysWOW64\PowerRun.exe
    ) else (
      del /q %SystemRoot%\system32\PowerRun.exe
    )
    Reg delete "HKCR\*\shell\Run as TrustedInstaller" /f
    echo.Uninstall completed.
    timeout /t 3 >nul
    exit
    
    Happy computing!

    Regards.
    @JeepWillys58
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. JeepWillys58

    JeepWillys58 MDL Senior Member

    Nov 6, 2010
    414
    183
    10
    Hi dear friend @Mouri_Naruto

    I had a problem when I use it, but let's do like "Hannibal" and let's go by parts...lol...

    I install the Nsudo using the install script "NSudo_Launcher_Installer_AIO.bat"

    After setting up a WIM to do some customizations, I open a Command Prompt by selecting "Run as TrustedInstaller (Enable all privileges)" and type a command to copy a modified file, hosts and sethc.exe, the host file it copies with no problem, but in the sethc.exe file the prompt returns the message "You don't have admin rights to copy" or something like that.

    Has anyone had the same problem?

    Regards.
    @JeepWillys58
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. inTerActionVRI

    inTerActionVRI MDL Addicted

    Sep 23, 2009
    877
    1,361
    30
    @freddie-o

    I made a merge of the "install" and "uninstall" parts, putting them in a small menu.

    If you allow me I will post it here.

    Thnks!
     
  12. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,152
    1,538
    60
    #13 freddie-o, Apr 5, 2022
    Last edited: Apr 5, 2022
    (OP)
    Meanwhile I was able to compile the code to merge the scripts

    Code:
    @echo off
    
    if "%2"=="firstrun" exit
    cmd /c "%0" null firstrun
    
    if "%1"=="skipuac" goto skipuacstart
    
    :checkPrivileges
    NET FILE 1>NUL 2>NUL
    if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
    
    :getPrivileges
    if '%1'=='ELEV' (shift & goto gotPrivileges)
    
    setlocal DisableDelayedExpansion
    set "batchPath=%~0"
    setlocal EnableDelayedExpansion
    ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
    ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
    "%temp%\OEgetPrivileges.vbs"
    exit /B
    
    :gotPrivileges
    setlocal & pushd .
    cd /d %~dp0
    cmd /c "%0" skipuac firstrun
    cd /d %~dp0
    
    :skipuacstart
    if "%2"=="firstrun" exit
    
    :menu
    echo.
    echo ____________________________________________________________
    echo.
    echo       "Run as TrustedInstaller" from the Context Menu
    echo ____________________________________________________________
    
    echo.
    echo.
    echo.
    echo  [1]  Add "Run as TrustedInstaller" to the Context menu
    echo.
    echo  [2]  Remove "Run as TrustedInstaller" from the Context menu
    echo.
    echo.
    echo.
    set /p userinp= ^ Enter your choice [1 or 2]:
    if %userinp%==1 goto add
    if %userinp%==2 goto remove
    goto invalid
    
    :add
    cd /d "%~dp0"
    set "arch=x64"
    if /i %PROCESSOR_ARCHITECTURE%==x86 if not defined PROCESSOR_ARCHITEW6432 set "arch=x86"
    if %arch%==x64 (
      copy PowerRun_x64.exe %SystemRoot%\system32\PowerRun.exe /y >NUL
      copy PowerRun.exe %SystemRoot%\SysWOW64\PowerRun.exe /y >NUL
    ) else (
      copy PowerRun.exe %SystemRoot%\system32\PowerRun.exe /y >NUL
    )
    Reg add "HKCR\*\shell\Run as TrustedInstaller" >NUL
    Reg add "HKCR\*\shell\Run as TrustedInstaller" /v "Icon" /t REG_SZ /d "imageres.dll,1" /f >NUL
    Reg add "HKCR\*\shell\Run as TrustedInstaller\command" /v "" /t REG_SZ /d "PowerRun.exe \"%%1\"" /f >NUL
    echo.
    echo Successfully added "Run as TrustedInstaller" to the Context menu.
    echo.
    echo Press any key to exit...
    pause >nul
    exit /b
    
    :remove
    cd /d "%~dp0"
    set "arch=x64"
    if /i %PROCESSOR_ARCHITECTURE%==x86 if not defined PROCESSOR_ARCHITEW6432 set "arch=x86"
    if %arch%==x64 (
      del /q %SystemRoot%\system32\PowerRun.exe >NUL
      del /q %SystemRoot%\SysWOW64\PowerRun.exe >NUL
    ) else (
      del /q %SystemRoot%\system32\PowerRun.exe >NUL
    )
    Reg delete "HKCR\*\shell\Run as TrustedInstaller" /f >NUL
    echo.
    echo Successfully removed "Run as TrustedInstaller" from the Context menu.
    echo.
    echo Press any key to exit...
    pause >nul
    exit /b
    
    :invalid
    echo.
    echo Invalid user input
    echo.
    echo Press any key to try again...
    pause >nul
    cls
    goto menu
    
     
  13. inTerActionVRI

    inTerActionVRI MDL Addicted

    Sep 23, 2009
    877
    1,361
    30
    #14 inTerActionVRI, Apr 5, 2022
    Last edited: Apr 5, 2022
    Thnks man!
    Yes, cool!

    I added the menu title that I hadn't put in.

    Uninstall section has been optimized.
    Code:
      del /q %SystemRoot%\system32\PowerRun.exe >NUL
      if "%arch%" neq "x86" if "%arch%" neq "arm" del /F /Q %SystemRoot%\SysWOW64\PowerRun.exe >NUL
    
    Code:
    @echo off
    
    color 0E
    :options
    cls
    echo.===============================================================================
    echo.                "Run as TrustedInstaller" from the Context Menu
    echo.===============================================================================
    echo.
    rem >nul 2>&1 net.exe session
    rem >nul 2>&1 reg.exe query HKU\S-1-5-19
    >nul 2>&1 fltmc.exe
    if %errorlevel% equ 1 (
       echo. Note: You can start the script again as Administrator.
       echo.       Or use the option below.
       echo.
       echo. [A]  Get Administrator Rights
       echo.
    ) else (
       cd /d "%~dp0"
    
       if /i "%PROCESSOR_ARCHITECTURE%" equ "amd64" (set "arch=x64") else (set "arch=%PROCESSOR_ARCHITECTURE%")
       if "%PROCESSOR_ARCHITEW6432%" neq "" if /i "%arch%" neq "x86" if /i "%arch%" neq "x64" start %SystemRoot%\Sysnative\cmd.exe /C "%~dpnx0" %* & exit
    
       SetLocal EnableExtensions EnableDelayedExpansion
    
       echo. [1]  Install PowerRun and
       echo.      add "Run as TrustedInstaller" in Context Menu
       echo.
       echo. [2]  Uninstall PowerRun and
       echo.      remove "Run as TrustedInstaller" from Context Menu
       echo.
    )
    echo.
    echo. [Q] Quit
    echo.
    echo.===============================================================================
    choice /C:A12Q /N /M "Enter Your Choice :"
    if %errorlevel% equ 4 exit
    if %errorlevel% equ 3 (
       if exist "%SystemROOT%\System32\PowerRun.exe" (
           del /q %SystemROOT%\System32\PowerRun.exe
           if "%arch%" neq "x86" if "%arch%" neq "arm" del /F /Q %SystemROOT%\SysWOW64\PowerRun.exe
           reg.exe delete "HKCR\*\shell\Run as TrustedInstaller" /f >nul 2>&1
           echo.
           echo.Successfully uninstalled PowerRun and removed
           echo."Run as TrustedInstaller" from the Context menu.
       ) else (
           echo.
           echo.PowerRun has already been removed.
       )
    )
    if %errorlevel% equ 2 (
       if not exist "%SystemROOT%\System32\PowerRun.exe" (
           if exist "PowerRun.exe" (
               if "%arch%" equ "x64" (
                   copy PowerRun_x64.exe %SystemROOT%\System32\PowerRun.exe /y
                   copy PowerRun.exe %SystemROOT%\SysWOW64\PowerRun.exe /y
               ) else (
                   copy PowerRun.exe %SystemROOT%\System32\PowerRun.exe /y
               )
               reg.exe add "HKCR\*\shell\Run as TrustedInstaller" /v "Icon" /t REG_SZ /d "imageres.dll,1" /f >nul 2>&1
               reg.exe add "HKCR\*\shell\Run as TrustedInstaller\command" /v "" /t REG_SZ /d "PowerRun.exe \"%%1\"" /f >nul 2>&1
               echo.
               echo.Successfully installed PowerRun and added
               echo."Run as TrustedInstaller" from the Context menu.
           )
       ) else (
           echo.
           echo.PowerRun is already installed.
       )
    )
    if %errorlevel% equ 1 goto :AdminRights
    
    echo.
    echo.Done
    echo.
    pause
    goto :options
    
    ::==========================================
    :: Getting Admin Rights
    :AdminRights
    
    rem Set Flags/Args
    set "FA=/X /D /U"
    
    rem >nul 2>&1 net.exe session || (
    rem >nul 2>&1 reg.exe query HKU\S-1-5-19 || (
    >nul 2>&1 fltmc.exe || (
       (
           :::: 1. Run as Admin with PowerShell, any path, params, loop guard, minimal i/o, tips by AveYo
           set "_=%FA% /C call "%~f0" %*"
           >nul 2>&1 PowerShell.exe -NoP -C "start cmd.exe $env:_ -Verb RunAs"
       ) || (
           :::: 2. Run as Admin with native shell, any path, params, loop guard, minimal i/o, tips by AveYo
           >nul 2>&1 reg.exe add "HKCU\Software\Classes\.Admin\Shell\runas\command" /ve /t REG_SZ /d "cmd.exe %FA% /R set \"f0=%%2\" & call \"%%2\" %%3" /f
           if "%f0%" neq "%~f0" (
               set _=%*
               >"%TMP%\RunAs.Admin" cd
               >nul 2>&1 start "%~nx0" /high "%TMP%\RunAs.Admin" "%~f0" "%_:"=""%"
           )
       ) || (
           :::: 3. Run as Admin with VBS CScript, as last chance, hehehehe
           >"%TMP%\GetAdmin.vbs" cmd.exe %FA% /R echo.CreateObject^("Shell.Application"^).ShellExecute "cmd.exe", " %FA% /C cd ""%~dp0"" && ""%~f0"" ""%*"" ", "", "RunAs", 1
           >nul 2>&1 cmd.exe %FA% /R %SystemROOT%\System32\CScript.exe //U //NoLogo "%TMP%\GetAdmin.vbs"
       )
       >nul 2>&1 reg.exe delete "HKCU\Software\Classes\.Admin" /f
       if exist "%TMP%\RunAs.Admin" del /F /Q "%TMP%\RunAs.Admin"
       if exist "%TMP%\GetAdmin.vbs" del /F /Q "%TMP%\GetAdmin.vbs"
       exit
    )
    ::==========================================
    
     
  14. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    1,467
    1,580
    60
    I'm impressed :eek_yello:
    3 || in a row interesting.
    Code:
    rem >nul 2>&1 net.exe session || (
    rem >nul 2>&1 reg.exe query HKU\S-1-5-19 || (
    >nul 2>&1 fltmc.exe || (
       (
           :::: 1. Run as Admin with PowerShell, any path, params, loop guard, minimal i/o, tips by AveYo
           set "_=%FA% /C call "%~f0" %*"
           >nul 2>&1 PowerShell.exe -NoP -C "start cmd.exe $env:_ -Verb RunAs"
       ) || (
           :::: 2. Run as Admin with native shell, any path, params, loop guard, minimal i/o, tips by AveYo
           >nul 2>&1 reg.exe add "HKCU\Software\Classes\.Admin\Shell\runas\command" /ve /t REG_SZ /d "cmd.exe %FA% /R set \"f0=%%2\" & call \"%%2\" %%3" /f
           if "%f0%" neq "%~f0" (
               set _=%*
               >"%TMP%\RunAs.Admin" cd
               >nul 2>&1 start "%~nx0" /high "%TMP%\RunAs.Admin" "%~f0" "%_:"=""%"
           )
       ) || (
           :::: 3. Run as Admin with VBS CScript, as last chance, hehehehe
           >"%TMP%\GetAdmin.vbs" cmd.exe %FA% /R echo.CreateObject^("Shell.Application"^).ShellExecute "cmd.exe", " %FA% /C cd ""%~dp0"" && ""%~f0"" ""%*"" ", "", "RunAs", 1
           >nul 2>&1 cmd.exe %FA% /R %SystemROOT%\System32\CScript.exe //U //NoLogo "%TMP%\GetAdmin.vbs"
       )
       >nul 2>&1 reg.exe delete "HKCU\Software\Classes\.Admin" /f
       if exist "%TMP%\RunAs.Admin" del /F /Q "%TMP%\RunAs.Admin"
       if exist "%TMP%\GetAdmin.vbs" del /f /q "%TMP%\GetAdmin.vbs"
       exit
    )
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. inTerActionVRI

    inTerActionVRI MDL Addicted

    Sep 23, 2009
    877
    1,361
    30
    #16 inTerActionVRI, Apr 5, 2022
    Last edited: Apr 5, 2022
    kkkkkkk

    These are the Brazilian representation of "lots of laughs", heheheh
     
  16. inTerActionVRI

    inTerActionVRI MDL Addicted

    Sep 23, 2009
    877
    1,361
    30
    #17 inTerActionVRI, Apr 5, 2022
    Last edited: Apr 6, 2022
    This is based on the one you sent today, with choice and some other modifications.
    Similar to the ones I posted before.

    Code:
    @echo OFF
    
    if "%2"=="firstrun" exit
    cmd /c "%0" null firstrun
    
    if "%1"=="skipuac" goto skipuacstart
    
    :checkPrivileges
    rem >nul 2>&1 net.exe session
    rem >nul 2>&1 reg.exe query HKU\S-1-5-19
    >nul 2>&1 fltmc.exe
    if %errorlevel% equ 1 (
       if '%1'=='ELEV' (shift & goto gotPrivileges)
    
       setlocal DisableDelayedExpansion
       set "batchPath=%~f0"
       setlocal EnableDelayedExpansion
       :: Erase OEgetPrivileges.vbs file content
       ECHO OFF>"%TMP%\OEgetPrivileges.vbs"
       ECHO CreateObject^("Shell.Application"^).ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%TMP%\OEgetPrivileges.vbs"
       "%TMP%\OEgetPrivileges.vbs"
       if exist "%TMP%\OEgetPrivileges.vbs" del /F /Q "%TMP%\OEgetPrivileges.vbs"
       exit /B
    )
    
    :gotPrivileges
    setlocal & pushd .
    cd /d %~dp0
    cmd /c "%0" skipuac firstrun
    cd /d %~dp0
    
    :skipuacstart
    if "%2"=="firstrun" exit
    
    :menu
    cd /d "%~dp0"
    
    if /i "%PROCESSOR_ARCHITECTURE%" equ "amd64" (set "arch=x64") else (set "arch=%PROCESSOR_ARCHITECTURE%")
    if "%PROCESSOR_ARCHITEW6432%" neq "" if /i "%arch%" neq "x86" if /i "%arch%" neq "x64" start %SystemRoot%\Sysnative\cmd.exe /C "%~dpnx0" %* & exit
    
    SetLocal EnableExtensions EnableDelayedExpansion
    
    echo.
    echo ____________________________________________________________
    echo.
    echo       "Run as TrustedInstaller" from the Context Menu
    echo ____________________________________________________________
    echo.
    echo.
    echo  [1]  Add "Run as TrustedInstaller" to the Context menu
    echo.
    echo  [2]  Remove "Run as TrustedInstaller" from the Context menu
    echo.
    echo.
    echo. [Q] Quit
    echo.
    echo.
    echo ____________________________________________________________
    choice /C:12Q /N /M "Enter Your Choice :"
    if %errorlevel% equ 3 exit
    if %errorlevel% equ 2 (
       if exist "%SystemROOT%\System32\PowerRun.exe" (
           del /q %SystemRoot%\system32\PowerRun.exe >NUL
           if not %arch%==x86 if not %arch%==arm del /q %SystemRoot%\SysWOW64\PowerRun.exe >NUL
           reg.exe delete "HKCR\*\shell\Run as TrustedInstaller" /f >NUL
           echo.
           echo.Successfully uninstalled PowerRun and removed
           echo."Run as TrustedInstaller" from the Context menu.
       ) else (
           echo.
           echo.PowerRun has already been removed.
       )
    )
    if %errorlevel% equ 1 (
       if not exist "%SystemROOT%\System32\PowerRun.exe" (
           if exist "PowerRun.exe" (
               if %arch%==x64 (
                   copy PowerRun_x64.exe %SystemRoot%\system32\PowerRun.exe /y >NUL
                   copy PowerRun.exe %SystemRoot%\SysWOW64\PowerRun.exe /y >NUL
               ) else (
                   copy PowerRun.exe %SystemRoot%\system32\PowerRun.exe /y >NUL
               )
               reg.exe add "HKCR\*\shell\Run as TrustedInstaller" >NUL
               reg.exe add "HKCR\*\shell\Run as TrustedInstaller" /v "Icon" /t REG_SZ /d "imageres.dll,1" /f >NUL
               reg.exe add "HKCR\*\shell\Run as TrustedInstaller\command" /v "" /t REG_SZ /d "PowerRun.exe \"%%1\"" /f >NUL
               echo.
               echo.Successfully installed PowerRun and added
               echo."Run as TrustedInstaller" from the Context menu.
           )
       ) else (
           echo.
           echo.PowerRun is already installed.
       )
    )
    echo.
    echo Press any key to exit...
    pause >nul
    cls
    goto menu
    
     
  17. inTerActionVRI

    inTerActionVRI MDL Addicted

    Sep 23, 2009
    877
    1,361
    30
    Don't get me wrong, I laughed because I thought about what you said when I was doing these redundancies.
    It was made so that when one doesn't work, the other one does, and so on.

    But a curious thing: if you leave the PowerShell option in the last position, if an error occurs in one of the other two when one of them is positioned in the middle, the PowerShell option doesn't work, it suffers a bypass.
     
  18. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,720
    5,014
    60
    Damn, @inTerActionVRI
    Like I told @Dark Monkey, the order is bananas! should be native cmd → cscript vbs → powershell.
    Also did say NEVER do these kinds of "fallbacks" when it comes to elevation. If ANY of the methods fail, the system is non-standard, so cautiously STOP IT and direct user to run it as admin!
    Functionally goes down the drain, script clarity goes down the drain, trust in a script doing this kind of thing goes down the drain, looks goes down the drain.
    I would prefer if people would skip the credits if they don't value the slickness of the original snippet.
    Script makes use of powershell anywhere? Just go with powershell one-liner alone:
    Code:
    fltmc >nul || (set Admin=/x /d /c call "%~f0" %* & powershell -nop -c start cmd $env:Admin -verb runas; & exit /b)
    Script does not use powershell? Go with the new native cmd two-liner (or vbs if you're fond of it):
    Code:
    >nul reg add hkcu\software\classes\.Admin\shell\runas\command /f /ve /d "cmd /x /d /r set \"f0=%%2\" &call \"%%2\" %%3" &set _= %*
    >nul fltmc || if "%f0%" neq "%~f0" ( cd.>"%tmp%\runas.Admin" &start "%~n0" /high "%tmp%\runas.Admin" "%~f0" "%_:"=""%" &exit /b )
    and btw, there's no need for "cleanup", nothing wrong with having the key there, or the 0-byte temp file, those don't work by themselves and are just placeholders, won't cause conflicts or vulnerabilities (like a schtasks would)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,152
    1,538
    60
    Strange! When I use the registry key to add "Run as TrustedInstaller" to the Context menu
    registry.png



    No errors running a script (to import reg files) as TrustedInstaller

    script.png



    But when I use the script to add "Run as TrustedInstaller" to the Context menu
    registry.png


    I get errors

    script.png