How can I elevate Powershell while keeping the current working directory AND maintain all parameters passed to the script?

Note: On 15 Nov 2021 a bug was fixed in the code below in order to make it work properly with advanced scripts – see this answer for details. The closest you can get to a robust, cross-platform self-elevating script solution that supports: both positional (unnamed) and named arguments while preserving type fidelity within the … Read more

How can I detect if my process is running UAC-elevated or not?

For those of us working in C#, in the Windows SDK there is a “UACDemo” application as a part of the “Cross Technology Samples”. They find if the current user is an administrator using this method: private bool IsAdministrator { get { WindowsIdentity wi = WindowsIdentity.GetCurrent(); WindowsPrincipal wp = new WindowsPrincipal(wi); return wp.IsInRole(WindowsBuiltInRole.Administrator); } } … Read more

How to Start a Process Unelevated

I had best results by cloning Explorer’s token as follows: var shellWnd = WinAPI.GetShellWindow(); if (shellWnd == IntPtr.Zero) throw new Exception(“Could not find shell window”); uint shellProcessId; WinAPI.GetWindowThreadProcessId(shellWnd, out shellProcessId); var hShellProcess = WinAPI.OpenProcess(0x00000400 /* QueryInformation */, false, shellProcessId); var hShellToken = IntPtr.Zero; if (!WinAPI.OpenProcessToken(hShellProcess, 2 /* TOKEN_DUPLICATE */, out hShellToken)) throw new Win32Exception(); uint … Read more

Elevating a ProcessBuilder process via UAC?

This can’t be done with ProcessBuilder, you will need to call Windows API. I’ve used JNA to achieve this with code similar to the following: Shell32X.java: import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.Structure; import com.sun.jna.WString; import com.sun.jna.platform.win32.Shell32; import com.sun.jna.platform.win32.WinDef.HINSTANCE; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.platform.win32.WinNT.HANDLE; import com.sun.jna.platform.win32.WinReg.HKEY; import com.sun.jna.win32.W32APIOptions; public interface Shell32X extends Shell32 { Shell32X INSTANCE = … Read more

How to run NOT elevated in Vista (.NET)

The WinSafer API’s allow a process to be launched as a limited, normal, or elevated user. Sample Usage: CreateSaferProcess(@”calc.exe”, “”, SaferLevel.NormalUser); Source code: //http://odetocode.com/Blogs/scott/archive/2004/10/28/602.aspx public static void CreateSaferProcess(String fileName, String arguments, SaferLevel saferLevel) { IntPtr saferLevelHandle = IntPtr.Zero; //Create a SaferLevel handle to match what was requested if (!WinSafer.SaferCreateLevel( SaferLevelScope.User, saferLevel, SaferOpen.Open, out saferLevelHandle, IntPtr.Zero)) … Read more

Delphi: Prompt for UAC elevation when needed

i would relaunch yourself as elevated, passing command line parameters indicating what elevated thing you want to do. You can then jump right to the appropriate form, or just save your HKLM stuff. function RunAsAdmin(hWnd: HWND; filename: string; Parameters: string): Boolean; { See Step 3: Redesign for UAC Compatibility (UAC) http://msdn.microsoft.com/en-us/library/bb756922.aspx This code is released … Read more