Toggle Administrator (elevated) mode

Discuss and share scripts and script files...
Post Reply
noembryo
Posts: 165
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: 165
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..

Post Reply