Python: How to start a process with Administrator Permissions?

pat picture pat · Jan 10, 2011 · Viewed 27.1k times · Source

I am starting the following script from a Windows 7 command line with administrator permissions:

import win32com.client
import time
import SendKeys
import os
from ctypes import *

shell = win32com.client.Dispatch("WScript.Shell")

os.startfile("C:\...exe")

I have also assigned the feature 'Run this programme as an administrator' to python.exe under Properties > Compatibility > Privilege Level. This did not change anything.

The programme still behaves differently when opened this way to how it behaves when I just open it via a double click on screen. Am I missing some important bit here? Will the process invoked in this way not be run as if started with administrator privileges?

Thanks for your help in advance!

Cheers -

Pat

Answer

Chinmay Kanchi picture Chinmay Kanchi · Jan 10, 2011

I don't have access to Vista or Windows 7, but you should be able to use the runas command.

import subprocess
subprocess.call(['runas', '/user:Administrator', 'C:/my_program.exe'])