Page 1 of 1

Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 00:15
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..

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 01:49
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;
    }

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 06:38
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:

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 12:00
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.. :(

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 13:15
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..

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 13:24
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
    }

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 14:33
by admin
1) $xyPath = """<xy>"""; --- I find this clearer ---> $xyPath = quote(<xy>);

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

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 17:00
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

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 17:10
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?

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 17:13
by admin
Check out SC ctbicon... (no time to go into it now)

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 17:29
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.

Re: Toggle Administrator (elevated) mode

Posted: 20 Nov 2025 17:43
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..