I want to run a windows command with Python 3. Like this os.system("echo hi"). However, How about running a command that requires admin access? How do you do this? Thanks.
1 Answer
You can do this with the ShellExecuteEx Win32 API wrapper included in the Pywin32 extensions. If you are using something like ActivePython you may already have the extensions.
To use ShellExecuteEx :
import win32com.shell.shell as shell
commands = 'echo hi'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)
8 Comments
Luke Dinkler
Excellent! Can we do this without the pop-up window?
snowcrash09
Do you mean the User Account Control pop-up? You're not really supposed to disable that. There are ways around it listed in this question but they all are a bit awkward.
Luke Dinkler
What if we had the user's password?
Deepak Yadav
@LukeDinkler One way for stopping popup : Run your python as admin then execute the scripts. Then automatically all the scripts would have the admin access, and they will not show popups.
Bernd Wechner
This appears to be deprecated in Python 3 and I'm having trouble finding the analogous Python 3 approach.
|
runas. See, e.g., this and, if entering a password on the shell is not an option, this