Toggle Administrator (elevated) mode

Discuss and share scripts and script files...
Post Reply
noembryo
Posts: 171
Joined: 13 Apr 2022 21:40
Location: Windows 10 @100%
Contact:

Toggle Administrator (elevated) mode

Post by noembryo »

I think I had seen a topic about it but searching now didn't help me.
The script I had downloaded then didn't work anyway, so I'm creating this new topic to check my luck.
If it looks like spamming the forum, please delete it..

So, it all come down to my laziness.
What I want is a script that I will then assign to a custom button.
That script should check if XYplorer has currently administration privileges, and if not, close XYplorer and re-open it as Administrator.
If it does have administrator privileges, it should close it and open it normally.

The way I do it now is just closing the app, finding a shortcut of XYplorer, right click on it and select "Run as Administrator".
I know it's not much work, but this is why I start learning to program.. :biggrin:
Asking our wise AI overlords, didn't produce any working code, not even some code that I could change.
Any help will be appreciated..
Check my free programs here..

noembryo
Posts: 171
Joined: 13 Apr 2022 21:40
Location: Windows 10 @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by noembryo »

Well, to answer my own wish, after some fiddling with some Claude code (the only one that made some sense), I made it with this:

Code: Select all

// XYplorer Admin Toggle Script
    // Check if running as Administrator using PowerShell
    $psCmd = "powershell -NoProfile -NonInteractive -Command ""([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)""";
    $adminResult = runret($psCmd);
    
    // Clean up the result - remove all whitespace and newlines
    $adminResult = replace($adminResult, <crlf>, "");
    $adminResult = replace($adminResult, " ", "");
    $adminResult = trim($adminResult);
    
    $isAdmin = ($adminResult == "True");
    
    // Get XYplorer executable path
    $xyPath = """<xypath>\<xyexe>""";
    
    // Get current tab/location to restore after restart
    $currentPath = get("Path");
    
    // Use Windows temp directory for helper files
    $tempDir = "%TEMP%";
    
    if ($isAdmin == 1) {
        // Currently running as Admin - restart as normal user
        // Use explorer.exe to launch XYplorer without admin rights
        // explorer.exe runs as the normal user even when called from admin process
        run "explorer.exe $xyPath", """$currentPath""";
        exit;
        
    } else {
        // Currently running as normal user - restart as Administrator
        // Create a VBScript to run XYplorer as admin
        $vbsFile = "$tempDir\restart_xy_admin.vbs";
        
        // Build VBScript with proper escaping - replace quotes in paths
        $xyPathClean = replace($xyPath, '"', '');
        $vbsContent = 'Set objShell = CreateObject("Shell.Application")' . <crlf> . 
                      'objShell.ShellExecute "' . $xyPathClean . '", "' . $currentPath . '", "", "runas", 1' . <crlf> .
                      'WScript.Sleep 2000' . <crlf> .
                      'Set fso = CreateObject("Scripting.FileSystemObject")' . <crlf> .
                      'fso.DeleteFile WScript.ScriptFullName';
        
        writefile($vbsFile, $vbsContent, "o");
        
        // Run the VBScript and close XYplorer
        run """$vbsFile""", , 0;
        exit;
    }
Check my free programs here..

admin
Site Admin
Posts: 65012
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by admin »

Instead of <xypath>\<xyexe> you can use <xy>.

Btw, you can use this to switch from user to admin mode:

Code: Select all

if (<get userrole> != "Admin") {
  exit "sre"; //restart elevated with saving
}
I don't see an easy way to go back from admin to user though. :eh:

noembryo
Posts: 171
Joined: 13 Apr 2022 21:40
Location: Windows 10 @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by noembryo »

admin wrote: 20 Nov 2025 06:38 Instead of <xypath>\<xyexe> you can use <xy>.
This worked fine.
Btw, you can use this to switch from user to admin mode:

Code: Select all

if (<get userrole> != "Admin") {
  exit "sre"; //restart elevated with saving
}
This didn't work.
It always returns true here..
It doesn't matter though, since I found a working way to check.

What I really wanted, but couldn't make, was a way to change the toolbar icon before exiting, to show what the next click will do.
E.g. toggling between a shield icon and a normal PC..
I lost a lot of time trying, but gave up.. :(
Check my free programs here..

noembryo
Posts: 171
Joined: 13 Apr 2022 21:40
Location: Windows 10 @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by noembryo »

admin wrote: 20 Nov 2025 06:38

Code: Select all

if (<get userrole> != "Admin") {
  exit "sre"; //restart elevated with saving
}
Update: My bad, it works fine..
Check my free programs here..

noembryo
Posts: 171
Joined: 13 Apr 2022 21:40
Location: Windows 10 @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by noembryo »

A more simplified version:

Code: Select all

// XYplorer Admin Toggle Script
    // // Check if running as Administrator
    $isAdmin = <get userrole> == "Admin";
    
    // Clean up the result - remove all whitespace and newlines
    $isAdmin = replace($isAdmin, <crlf>, "");
    $isAdmin = replace($isAdmin, " ", "");
    $isAdmin = trim($isAdmin);

    // Get XYplorer executable path
    $xyPath = """<xy>""";
    
    // Get current tab/location to restore after restart
    $currentPath = get("Path");
    
    if ($isAdmin == 1) {
        // Currently running as Admin - restart as normal user
        // Use explorer.exe to launch XYplorer without admin rights (explorer.exe runs as the normal user even when called from admin process)
        run "explorer.exe $xyPath", """$currentPath""";
        exit;        
    } else {
        // Currently running as normal user - restart as Administrator
        exit "sre"; //restart elevated with saving
    }
Check my free programs here..

admin
Site Admin
Posts: 65012
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by admin »

1) $xyPath = """<xy>"""; --- I find this clearer ---> $xyPath = quote(<xy>);

2) Oha, that explorer.exe trick works? Cool!

admin
Site Admin
Posts: 65012
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by admin »

Yep, seems to work. Next beta:

Code: Select all

    + SC exit enhanced: Now you can use it restart an elevated XYplorer as unelevated 
      (without administrator rights).
      Name:   Exit
      Action: Exit XYplorer, optionally with restart.
      Syntax: exit [mode]
        mode: 
          sru = restart unelevated with saving
          nru = restart unelevated without saving

noembryo
Posts: 171
Joined: 13 Apr 2022 21:40
Location: Windows 10 @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by noembryo »

admin wrote: 20 Nov 2025 17:00 Yep, seems to work. Next beta:
:tup: :tup:
What I really wanted, but couldn't make, was a way to change the toolbar icon before exiting, to show what the next click will do.
E.g. toggling between a shield icon and a normal PC..
About the icon change now..
..are there any options?
Check my free programs here..

admin
Site Admin
Posts: 65012
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by admin »

Check out SC ctbicon... (no time to go into it now)

Horst
Posts: 1340
Joined: 24 Jan 2021 12:27
Location: Germany

Re: Toggle Administrator (elevated) mode

Post by Horst »

I know that Explorer trick because we use it
for a Total Commander admin mode toggle.

Strange, but the script from above doesn't work for me.
The elevation runs fine but going back to unelevated only stops the elevated XY
but doesn't start a normal new one.
Windows 11 Home, Version 25H2 (OS Build 26200.7171)
Portable x64 XYplorer (Actual version, including betas)
Display settings 1920 x 1080 Scale 100%
Everything 1.5.0.1402a (x64), Everything Toolbar 2.1.0, Listary Pro 6.3.6.99

noembryo
Posts: 171
Joined: 13 Apr 2022 21:40
Location: Windows 10 @100%
Contact:

Re: Toggle Administrator (elevated) mode

Post by noembryo »

admin wrote: 20 Nov 2025 17:13 Check out SC ctbicon... (no time to go into it now)
Thank you I'll check it out..
UPDATE: Found it! Thank you..
Horst wrote: 20 Nov 2025 17:29 Strange, but the script from above doesn't work for me.
The elevation runs fine but going back to unelevated only stops the elevated XY
but doesn't start a normal new one.
Before pressing the button, stop getting folder sizes (with Esc).
This was preventing me some times..
I wonder if there is a way to stop that before exiting..
Check my free programs here..

Post Reply