Abhijith
Abhijith

Reputation: 411

How do I elevate vbs to delete a Registry key

This is the faulty code

WshShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\cFos\test\tvalue"

This is my error
Error: Unable to remove registry key "HKLM\SOFTWARE\test".
Code: 800070005 Source: WshShell.RegDelete

I gather that I need to disable UAC in Win Vista/7/8 but I was wondering if there is a way to directly run the .vbs file as administrator. Because I need to distribute it and people might not like disabling their UAC and some might even think this is malicious if I give an explicit instruction to pull down their defense

Upvotes: 0

Views: 4704

Answers (2)

Nilpo
Nilpo

Reputation: 4816

Open the Start Menu and type cmd. When Command Prompt appears, right-click it and choose Run as Administrator. You can run your script from the elevated command prompt with either cscript.exe or wscript.exe.

To run the script from a shortcut, right-click the desktop and choose New > Shortcut. It will prompt you for the location of the program. Type the following:

runas /noprofile /savecred /user:localmachinename\administrator cscript.exe "C:\path\to\script.vbs"

Click Next. Enter whatever name you like for your shortcut and then click Finish.

Upvotes: 0

Jay
Jay

Reputation: 4686

Create a new shortcut file using below command line.

runas /profile /user:administrator cscript myscript.vbs

or...

runas /profile /user:administrator cscript "C:\Script Collection\My Script.vbs"

You can then simply double click the shortcut file to execute the script using Administrator account.

The shortcut will prompt for Administrator password each time. If you want it to fully automate it, use below command line instead.

runas /profile /savecred /user:administrator cscript myscript.vbs

It will prompt for the password, but only once. The next time you run it, it will not prompt it again.

Upvotes: 2

Related Questions