Perl and Windows UAC

While doing some Registry-related things with Perl a couple of weeks ago, I ran into something that took me nearly a day to figure out: Win32::TieRegistry asks for write access to the Registry by default, and under Win7 that requires Administrator privileges. (I kid you not, it took me over a day to figure that out.)

Well, getting what turns out to be called "elevated privileges" from Perl turns out to have been pretty hard - a lot harder than it should have been, for reasons I went into in some detail in a little article about it, but, to cut straight to the chase, I came up with a way to get Perl to restart a script after invoking UAC to gain elevated privileges. It's got some weaknesses that are inherent to the way Windows manages the console, but, well, I present to you Win32::RunAsAdmin. You can use it like this:

   use Win32::RunAsAdmin qw(force);

That checks to see whether you're running with elevated privileges already, and if not, it calls the shell to run the same script again with the same arguments, and exits. Obviously, the (few) components of the module are also available separately; check the POD for details. But in all it's about ten lines.

Problem solved. Hope this helps somebody.


Excellent work. I was looking for such a module.

Is it 100% safe (i.e. check function)?

Shell.Application was added for WScript and CScript scripts, so the actual functionality should be accessible directly with Win32::API. I'm not sure at the moment what it is calling under the hood.

Hi! What about Win32::IsAdminUser() to check for elevated privileges? The docs say:

Returns non zero if the account in whose security context the current process/thread is running belongs to the local group of Administrators in the built-in system domain; returns 0 if not. On Windows Vista it will only return non-zero if the process is actually running with elevated privileges. Returns undef and prints a warning if an error occurred. This function always returns 1 on Win9X.


Leave a comment

About Michael Roberts

user-pic I blog about things - things that help me go. Perl helps me go.