Discussion:
How to run scripts as administrator in Windows 7?
(too old to reply)
Dave "Crash" Dummy
2011-07-16 13:15:28 UTC
Permalink
I have some scripts that require administrator privileges to run in
Windows 7, and I can't run them directly because there is no "Run as
administrator" option in the context menu for .VBS files. I work around
this by creating a shortcut to wscript.exe, with my script as the
argument. The shortcut then has the "Run as administrator" option. This
works, but it is awkward and a pain in the posterior. Is there some way
to add the "Run as administrator" option to script files?
--
Crash

I always thought Mensa was a Japanese import.
PaulM
2011-07-16 16:14:02 UTC
Permalink
Put this code in the start of your script:



Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Else
--
Paul's XP and Vista Help
-----------------------------------------
www.paulsxp.com
---------------------------------------
Paul's Forum
--------------------------------------
www.paulsxp.com/forum
--------------------------------------
Post by Dave "Crash" Dummy
I have some scripts that require administrator privileges to run in
Windows 7, and I can't run them directly because there is no "Run as
administrator" option in the context menu for .VBS files. I work around
this by creating a shortcut to wscript.exe, with my script as the
argument. The shortcut then has the "Run as administrator" option. This
works, but it is awkward and a pain in the posterior. Is there some way
to add the "Run as administrator" option to script files?
--
Crash
I always thought Mensa was a Japanese import.
Dave "Crash" Dummy
2011-07-16 16:24:50 UTC
Permalink
Post by PaulM
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Else
Cool! That works. Thank you!
--
Crash

"It is not necessary to change. Survival is not mandatory."
~ W. Edwards Deming ~
PaulM
2011-07-17 01:54:51 UTC
Permalink
You are welcome.
--
Paul's XP and Vista Help
-----------------------------------------
www.paulsxp.com
---------------------------------------
Paul's Forum
--------------------------------------
www.paulsxp.com/forum
--------------------------------------
Post by Dave "Crash" Dummy
Post by PaulM
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Else
Cool! That works. Thank you!
--
Crash
"It is not necessary to change. Survival is not mandatory."
~ W. Edwards Deming ~
g***@gmail.com
2013-05-07 13:02:47 UTC
Permalink
Thank you dude =)
Post by PaulM
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Else
--
Paul's XP and Vista Help
-----------------------------------------
www.paulsxp.com
---------------------------------------
Paul's Forum
--------------------------------------
www.paulsxp.com/forum
--------------------------------------
Post by Dave "Crash" Dummy
I have some scripts that require administrator privileges to run in
Windows 7, and I can't run them directly because there is no "Run as
administrator" option in the context menu for .VBS files. I work around
this by creating a shortcut to wscript.exe, with my script as the
argument. The shortcut then has the "Run as administrator" option. This
works, but it is awkward and a pain in the posterior. Is there some way
to add the "Run as administrator" option to script files?
--
Crash
I always thought Mensa was a Japanese import.
k***@gmail.com
2013-06-26 15:26:37 UTC
Permalink
www.paulsxp.com---------------------------------------Paul's Forum Hi Paul, I think you were the guy I got that from about 6 years ago. That has worked for me for the past 6 years except for the company I'm back with. Also I noticed I did not see End If at the end but I may have not noticed it. Something funky on our computers. It works for Admins on the network locations I have my scripts but for end users it does not, until I made it Read\Write for all. Now they did a new security update and I'm getting "The script file "File Name" cannot be found.

I'm looking for a fix for the current problem. I think the reason my scripts are failing is I have them on a remote server, it first looks for the current folder, then a subfolder.

The one I've been using is the objShell instead of WshShell. I had a gap in my programming from 1983 to 2007. Paul, would you please tell me what the different is? I've researched this for years and get mixed answers and none of them are straight forward. I also wonder about what the difference is between ObjShell = CreateObject("Shell.Application") and ObjShell = WScript.CreateObject("Shell.Application")

I've tried variations but they all end with the same Result, it calls out the name of the script and says it can't find it. I'm stubborn and don't like asking for help or directions :).
k***@gmail.com
2013-10-02 11:01:10 UTC
Permalink
Watch this..

Vbscript to application as administrator.
tan2x
2013-10-02 18:34:59 UTC
Permalink
Dave Crash Dummy wrote on 07/16/2011 09:15 ET
Post by Dave "Crash" Dummy
I have some scripts that require administrator privileges to run i
Windows 7, and I can't run them directly because there is no "Run a
administrator" option in the context menu for .VBS files. I work aroun
this by creating a shortcut to wscript.exe, with my script as th
argument. The shortcut then has the "Run as administrator" option
Thi
works, but it is awkward and a pain in the posterior. Is there some wa
to add the "Run as administrator" option to script files
Cras
I always thought Mensa was a Japanese import
Automate everything including password input.
http://youtu.be/8RR4p2vCoBk
PaulM
2013-10-03 13:36:18 UTC
Permalink
Post by Dave "Crash" Dummy
I have some scripts that require administrator privileges to run in
Windows 7, and I can't run them directly because there is no "Run as
administrator" option in the context menu for .VBS files. I work around
this by creating a shortcut to wscript.exe, with my script as the
argument. The shortcut then has the "Run as administrator" option. This
works, but it is awkward and a pain in the posterior. Is there some way
to add the "Run as administrator" option to script files?
Crash
I always thought Mensa was a Japanese import.
Automate everything including password input..
http://youtu.be/8RR4p2vCoBk
This is what I used in my scripts:

If WScript.Arguments.length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Else


-----------------------------------------------------------
Paul's XP, Vista and Windows 7 Help
-----------------------------------------------------------
www.paulsxp.com
www.paulsxp.com/forum
---------------------------------------------------------
m***@gmail.com
2014-04-11 08:01:45 UTC
Permalink
Open a cmd as an administrator. Open you vb using the absolute path between quotes. "C:/...."
Loading...