Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Toggle "Run as Administrator" compatibility option from context menu?

30 Jul 2015   #1
arrestme

Windows 7 ultimate x64
 
 
Toggle "Run as Administrator" compatibility option from context menu?

I've definitely looked my way around the net for this one, but alas, she's a hard one for those search engines & me to make any sense of for what I'm specifically trying to find a way to do.

I've made a few of my own context menu shortcuts (copy file path / create dir filelist /reg backup/create sys restore / toggle firewall block status / toggle hide protected system files / file+folder unblock) cuz I do a lot of things repetitively & they really make things much quicker for day2day tasking.

So basically I have 2 questions for the smart ones out there who could teach a noob like me a few things. If not teach, then just share? <3

1.) Like title says BUT without opening it; I want to skip having to go to properties of the file -> click compatibility tab -> check "Run this program as an administrator" under Privilege Level -> then click "Change settings for all users" -> to then click the box again (I know i can just click the 2nd box and skip checking the 1st, but ya kno wut i'm saying; making sure both get applied). ->clicking OK -> then clicking OK = just to make sure that particular file will always run as admin whether thru a shortcut or not.
Can I make a .bat file or must I do a wscript for that? (I can then take that & add into context menu to my preference, but not important) or maybe even a less-intrusive method? You will save my neck some sweet sweet second fractions guys!

also, if u have the time to spare;
2.) How would I have been able to find these options out on my own? If this is the harder question of the 2, feel free to skip.

Here's a quick example if I'm talking too much to be clear:


Thanks a ton everyone. LONGGGGG time viewer, first time poster. Hope ya'll are good.


My System SpecsSystem Spec
.
30 Jul 2015   #2
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Hi Arrestme, and thank you for joining Seven Forums,

And nice image there.

Those compatibility configurations are saved to the registry. Specifically to the key of
  • HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
or instead
  • HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
if the settings should apply for all users. Note that that second location, in HKLM, is a virtual key (a.k.a. "hidden registry key") so don't expect to be able to search it through RegEdit.

When a compatibility setting is configured on an application, the path of that executable is written to the according registry key, as a value. And the value's data contains the compatibility configuration information for that executable, in the form of a space separated list of strings, each string called a 'flag'.

The flag for configuring an application to run with administrative rights is 'RUNASADMIN'.


E.g. Instead of using the gui to set the "Run this program as an administrator" compatibility setting for all users, one could alternatively choose to run the command
Code:
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\path\to\FL.exe" /t "REG_SZ" /d "RUNASADMIN"
to succeed the same effect. (N.B., generally, writing to the HKLM hive requires local administrative rights, so an elevated command prompt would be needed.)


Quote   Quote: Originally Posted by arrestme View Post
Can I make a .bat file or must I do a wscript for that? (I can then take that & add into context menu to my preference, but not important)
Yes. If you can do the adding-to-the-context-menu part, I may be able to create some VBScript that aptly writes to the registry for you.

However, unfortunately, you'll have to abandon the idea of being able to configure the "Run this program as an administrator" compatibility setting for all users, because you'll have a hard time getting the script to run as administrator, launching it from the context menu (as 'HKLM\Software\...\Layers' requires administrative permissions to modify).


Toggle Run as Admin.vbs
Code:
' ToggleRunAsAdmin.vbs ExeFile
' Toggle the "Run this program as an administrator" compatibility setting on 'ExeFile'.
 
Function PromptForElevation
    If Not CreateObject("WScript.Shell").Run("Net Sess", 0, True) = 0 Then
        CreateObject("Shell.Application").ShellExecute "WScript", _
                """" & WScript.ScriptFullName & """" & " " & """" & WScript.Arguments.Item(0) & """", "","RunAs",1
        WScript.Quit
    End If
End Function
 
If WScript.Arguments.Unnamed.Count = 0 Then WScript.Quit(1)
If WScript.Arguments.Item(0) = "" Then WScript.Quit(1)
 
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
 
'PromptForElevation()
 
Set reLetters = New RegExp
reLetters.Pattern = "[a-z]"
reLetters.IgnoreCase = True
reLetters.Global = True
 
Set reRunAsAdmin = New RegExp
reRunAsAdmin.Pattern = " *RUNASADMIN *"
reRunAsAdmin.IgnoreCase = True
reRunAsAdmin.Global = True
 
hDefKey   = HKCU
strSubKey = "Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
strValue  = WScript.Arguments.Item(0)
strData   = "RUNASADMIN"
 
Set objReg = GetObject("WinMgmts:\\.\root\default:StdRegProv")
 
objReg.GetStringValue hDefKey, strSubKey, strValue, ExistingData
 
If IsNull(ExistingData) Then
    objReg.SetStringValue hDefKey, strSubKey, strValue, strData
Else
    If reLetters.Test( Replace(ExistingData, strData, "") ) Then
        If reRunAsAdmin.Test( ExistingData ) Then
            objReg.SetStringValue hDefKey, strSubKey, strValue, reRunAsAdmin.Replace(ExistingData, "")
        Else
            objReg.SetStringValue hDefKey, strSubKey, strValue, ExistingData & " " & strData
        End If
    Else
        objReg.DeleteValue hDefKey, strSubKey, strValue
    End If
End If
My System SpecsSystem Spec
31 Jul 2015   #3
arrestme

Windows 7 ultimate x64
 
 

Hello there & thankyou for the welcome. I'd like to quickly do a preface response with a quick HELLZ yea for this Gentleman & Scholar; Pyprohly. I once was lost but now I am found m'i'right people? Didn't feel right sinking into such a fruitful share without first saying hello, so if you don't mind, I'm like a kid in a candy store waiting to bite into this beautiful knowledgecopia. I'll be back for afterthoughts bro.
My System SpecsSystem Spec
.

31 Jul 2015   #4
arrestme

Windows 7 ultimate x64
 
 

I knew being uber polite might come in handy bro, cuz i can't figure it out man I äm stuck.
This was my last step I took for context menuing for exes. Wrong spot maybe or something?

Everything seems to be how u said it should. It goes up onto the context menu, but when i click it no script loads, the options dont change and i even chacked in registry for some reason it didnt right it there either (prob pointless to check there after, but had to make sure hehe)
Code:
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin]
"Icon"="\"C:\\Windows\\System32\\rekeywiz.exe\""
@="Run always as Admin"
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin\command]
@="WScript C:\\Windows\\ToggleRunasAdmin.vbs"
Code:
' Toggle_Run_as_Admin.vbs ExePath
' Toggle the "Run this program as an adminstrator" compatibility setting on 'ExePath' for the current user.
 
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
 
If WScript.Arguments.Unnamed.Count = 0 Then WScript.Quit(1)
If WScript.Arguments.Item(0) = "" Then WScript.Quit(1)
 
strSubKey = "Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
strValue  = WScript.Arguments.Item(0)
strData   = "RUNASADMIN"
 
Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
 
objReg.GetStringValue HKCU, strSubKey, strValue, Exists
 
MsgBox(IsNull(Exists))
 
If IsNull(Exists) Then
    objReg.SetStringValue HKCU, strSubKey, strValue, strData
Else
    objReg.DeleteValue HKCU, strSubKey, strValue
End If
Am I doing it wrong bruh? I added contexts in HKCR\exefile\shell\Set Run as Admin\command & it points to the vbs toggle script...
I've officially tried everything except the bat file so far, because I wanted to ask what you thought about a possible bypass to not abandon the all users setting, aka getting the script to run as admin from context to write to HKLM/.../layers. Could adding the location of the vscript manually to HKLM/.../layers with the proper path & RUNASADMIN value? So then not all vscripts will b able to run as admin for safety reasons (otherwise we could've just added wscript.exe to layers, but possibly unsafe in case?). Then it launches the script with admin every time? I kno u can launch .vbs as admin with special right click trick & others, so it might work no? Otherwise, what if I just take permissions & ownership of folder? Could the script write to it under different username besides admin if changed rights? Was I on to something with the first guess & Just embarassing myself more with these last ones? Doh! Once again thanks bro.
My System SpecsSystem Spec
31 Jul 2015   #5
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by arrestme View Post
Code:
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin]
"Icon"="\"C:\\Windows\\System32\\rekeywiz.exe\""
@="Run always as Admin"
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin\command]
@="WScript C:\\Windows\\ToggleRunasAdmin.vbs"
Am I doing it wrong bruh? I added contexts in HKCR\exefile\shell\Set Run as Admin\command & it points to the vbs toggle script...
Very close. You forgot to pass the file as a parameter to the script.
E.g.
Code:
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin]
@="Run always as Admin"
"Icon"="\"C:\\Windows\\System32\\rekeywiz.exe\""
 
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin\command]
@="WScript \"C:\\Windows\\ToggleRunasAdmin.vbs\" %1"

Now on to the topic of elevating the script itself...

Quote   Quote: Originally Posted by arrestme View Post
I've officially tried everything except the bat file so far, because I wanted to ask what you thought about a possible bypass to not abandon the all users setting, aka getting the script to run as admin from context to write to HKLM/.../layers.
Except the bat file? What bat file? Anyway, so I've added a function to the VBScript that will prompt you for elevation, which will run the script as administrator should you accept it's UAC dialogue. Uncomment line 18 to enable this behaviour. After doing so, you will be able to write to 'HKLM\...\Layers' (upon accepting the UAC).

In the script, line 30, edit "hDefKey = HKCU" to "hDefKey = HKLM" to write to 'HKLM\...\Layers' instead, i.e., to save the RUNASADMIN configuration setting for all users.

If you happen to find the UAC dialogue excessive, you can choose to go about changing the permissions of the 'HKLM/.../layers' key, and make it have similar access rules to 'HKCU/.../layers' so that no administrative rights would be required to edit the key. But because 'HKLM/.../layers' exists as a virtural key it is going to be a little tricky to make possible those changes.

Quote   Quote: Originally Posted by arrestme View Post
Could adding the location of the vscript manually to HKLM/.../layers with the proper path & RUNASADMIN value? So then not all vscripts will b able to run as admin for safety reasons (otherwise we could've just added wscript.exe to layers, but possibly unsafe in case?).
You may not add anything but exe files to 'HKLM\...\Layers' (well you can but it won't do anything) as only programs can have compatibility configurations. You could add the WScript engine to the list. That will work, however, any script that tries to run using that engine, you'll have to always explicitly allow it to do so by first accepting a UAC dialogue. It won't be unsafe or pose any security risks because of this, but it will become annoying.


Quote   Quote: Originally Posted by arrestme View Post
Was I on to something with the first guess & Just embarassing myself more with these last ones? Doh! Once again thanks bro.
You, sir, are very computer literate.
My System SpecsSystem Spec
31 Jul 2015   #6
arrestme

Windows 7 ultimate x64
 
 

Wehehehelll.. (straightens cufflinks) I do dabble. lol just kidding more like involuntary lifestyle choice. xD

Oh sweet! This just in, LIGHTENING HAS STRUCK, I REPEAT THE EAGLE HAS LANDED! It works! Tweaked that quick parameter for the script pass, elevating the ball FAR with help of quick UAC by uncommenting line 18... and decides to put HKCU on the bench to make way for.... HKLM? Now this is unprecedented....ladies and gentleman we - 1second..whats this? HKLM is closing in fast towards the endzone at line 30?? WHAAAT!? OMG HKLM catches the script parameter just in the nick of time! YES!! GO!! YEEEEES Team Bruh WINS the championship!!!! GO TEAM BRUH!!! #crowd_cheers #coach_Pyprohly_gets_gatoraided #freeze_frame_showing_arrestme_carried_off_with_trophy_and_sees_Pyprohly_looking_on_like_Mr_Miyagi_a t_end_of_Karate_Kid #superimposed_text_fades_in_-_Arrestme_goes_on_to_become_president_of_the_universe_Pyprohly_goes_beyond_to_other_galaxies_spreadi ng_adventure_and_learning_still_to_this_day. #long_ass_hashtags lmao

I may have already changed the permissions in registry or made myself owner before because no UAC.
My System SpecsSystem Spec
31 Jul 2015   #7
arrestme

Windows 7 ultimate x64
 
 

hmm now wait a minute...I forgot I was testing on an exe file I had isolated in a folder I had taken as owner and given my only full privilege. It changed that with no UAC. But going to...lets say my batman.exe...the same thing happens as before, no change with no UAC even. theyre gonna take away my universe presidency A SEQUEL?
My System SpecsSystem Spec
31 Jul 2015   #8
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Haha. Creative.

Now lets find out what broke here... did some debugging and found a issue to do with paths and spaces in them. To fix this, we must make a small change to the reg file, specifically how it passes that parameter, again: needs quotes 'round it is all.
Code:
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin]
"Icon"="\"C:\\Windows\\System32\\rekeywiz.exe\""
@="Run always as Admin"
[HKEY_CLASSES_ROOT\exefile\shell\Set Run as Admin\command]
@="WScript \"C:\\Windows\\ToggleRunAsAdmin.vbs\" \"%1\""

Quote   Quote: Originally Posted by arrestme View Post
I forgot I was testing on an exe file I had isolated in a folder I had taken as owner and given my only full privilege. It changed that with no UAC. But going to...lets say my batman.exe...the same thing happens as before, no change with no UAC even.
The script should always work, no matter how badly denied you are to the file being acted on. It's the registry keys permissions that really count here.

If line 18 is uncommented, you should always receive a UAC prompt.


Quote   Quote: Originally Posted by arrestme View Post
A SEQUEL?
Preparing my popcorn...
My System SpecsSystem Spec
04 Aug 2015   #9
arrestme

Windows 7 ultimate x64
 
 

Baboom! haha yes you scored a hat trick helping me out thrice! Aw, we ran out of gatoraid.
Yea I was wondering wut was going on cuz instead of just the file, it ended up pertaining to that whole partition letting me write to their exes, & not the main one

....so I went on a little "permission impossible" I guess on my own yesterday. lol
Of course it was my own doing who was the culprit XD but that's why these context things get me learning the most interesting things. Thanks once again man. Hope you getting paid for this cuz u should!

I might make another thread asking about this chain of Python scripts that piggyback on one another, & I been trying to figure out a way to use these Python2Exe programs, which I haven't gotten to work ever. I think it's because of the piggybacking modules. This also piggybacks on my complete lack of Python knowledge at all...I was ridiculed to a crisp in their support IRC. Not sure if I should go find the section that post could go in, to specifically ask for somebody to get the files from me, see how they work, then use their knowledge to use the Python2Exe program for me, since i guess at a lot of the settings.

A lot of people would benefit, if u kno anyone who might help me with that. It's a Python/PGU/Pygame OSD of live virtualized feed of a plugged in USB Gamecube Controller. It's used for streaming & what-not, but NOBODY uses it, because it requires all these plugins & basic knowledge of installing things for windows/python, but MOSTLY it's the 2nd reason; to stream it live in your "Scene", it can't have anything over it's window, & b completely visible at all times, which is counterproductive sometimes. If it were an EXE, you not only wouldn't need any of the python scripts/modules/programs anymore to run it, but you could then layer it on your streaming screen "scene" as a "game", or even other media, thereby not needing it to be visible or unobstructed to live feed through it's scene layer.

The above is all complicated, but rly the program isn't supposed to be, I'm just overheated having basically quit streaming for weeks cuz of it. All of the modules/scripts/etc are all verifiable as well & safe. Thanks again for everthing hombre. You're the "El Chapo" of tech.
My System SpecsSystem Spec
05 Aug 2015   #10
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

I'm glad you have found the assistance resourceful.


Sadly, I cannot help you with your Python problem, nor likely can this forum at this time.

Where ever you create your said thread, I do not recommend you create such a thread here, in assertion that you will not get an answer, or even a reply, because Python remains out of the scope of knowledge which this forum is able to provide, and the stated problem is better suited for its respective forum. Unfortunately, I don't know of one, or if one exists at this stage.

If you really do get desperate though, my average enough experience in Python got me successfully compiling Python 3 scripts once, but that was some while ago—never really had the need to compile them—I prefer them in script form, readable, all the time. While I may be able to help you compile that Python 2 script of yours, that's as far as I will go on that one. I've never touched a Python script that has a gui before.

If you want to continue discussing this Python problem here, please PM me instead. But I encourage you look elsewhere for help first.

Good luck, Arrestme.
My System SpecsSystem Spec
Reply

 Toggle "Run as Administrator" compatibility option from context menu?




Thread Tools Search this Thread
Search this Thread:

Advanced Search



Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 18:38.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App