How to run an application as "run as administrator" from the command prompt?

PowershellCmdCommand Prompt

Powershell Problem Overview


I have a batch file called test.bat. I am calling the below instructions in the test.bat file:

start /min powershell.exe %sysdrive%\testScripts\testscript1.ps1

When I run this through the command prompt, my testscript is running successfully. I want to run it as administrator (as if I have created a desktop shortcut and run as administrator. It shouldn't prompt for any username or password).

I have tried adding /elevate and /NOUAC parameters in the above test.bat, but no luck. How do I fix this issue?

I know how to do it manually, but I want this to be executed from the command prompt.

(By Marnix Klooster): ...without using any additional tools, like those suggested in an answer to Super User question How to run program from command line with elevated rights.)

Powershell Solutions


Solution 1 - Powershell

Try this:

runas.exe /savecred /user:administrator "%sysdrive%\testScripts\testscript1.ps1" 

It saves the password the first time and never asks again. Maybe when you change the administrator password you will be prompted again.

Solution 2 - Powershell

See this TechNet article: Runas command documentation

From a command prompt:

C:\> runas /user:<localmachinename>\administrator cmd

Or, if you're connected to a domain:

C:\> runas /user:<DomainName>\<AdministratorAccountName> cmd

Solution 3 - Powershell

It looks like psexec -h is the way to do this:

 -h         If the target system is Windows Vista or higher, has the process
            run with the account's elevated token, if available.

Which... doesn't seem to be listed in the online documentation in Sysinternals - PsExec.

But it works on my machine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPraveen JakkarajuView Question on Stackoverflow
Solution 1 - PowershellDhanaView Answer on Stackoverflow
Solution 2 - PowershellJohn RuizView Answer on Stackoverflow
Solution 3 - PowershellBen CurthoysView Answer on Stackoverflow