How do I launch Powershell with admin rights, and then start a .bat/.cmd that needs admin rights?

johnch99 1 Reputation point
2022-07-13T16:51:38.093+00:00

I made a autoexec.bat and saved the file in %USERPROFILE%\Appdata\Roaming\Microsoft\Start Menu\Programs\Startup, inside the autoexec.bat, I used the command:
Powershell Start-Process -verb runas -File C:\Temp\Test.bat , When I launch the autowexec.bat it launches a CMD and starts test.bat with no admin rights, is there a way to launch Powershell with admin rights and start C:\Temp\Test.bat? If not, launching CMD with admin rights and and start C:\Temp\Test.bat maybe okay.

I also tried Powershell.exe -ExecutionPolicy Unrestricted Start-Process -Filepath C:\Temp\Test.bat, When I run this command, it launches a CMD and starts C:\Temp\Test.bat with no admin rights

Thanks for the your help

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,424 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,266 Reputation points
    2022-07-13T18:59:06.937+00:00

    Why run Powershell from a .BAT file just to run another .BAT file? Try this in the autoexec.bat file: how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator

    If insist on using Powershell to do this, then start CMD.EXE (with the necessary command line options) by using the Start-Process with the -VerbRunAs switch.

    Keep in mind that UAC may be involved in the privilege elevation.

    0 comments No comments

  2. johnch99 1 Reputation point
    2022-07-13T20:58:41.127+00:00

    Thanks, somebody from https://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator, suggested using:

    Powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList '/c "c:\bin\myScript.cmd"'"

    Its works great.

    0 comments No comments