Run a Powershell script from Batch file with elevated privileges? - powershell

I want to run example.ps1 from code.bat
I realize that the start command would run it but require that it be run as admin, how can I do this?

In your batch file:
powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs}"
...basically you're using Powershell to run Powershell. The second instance is elevated to Admin and is running your script with those permissions.
Full explanation:
Start-Process can be used to run a program, and also has the parameter -Verb RunAs which elevates the program to run as Admin.
We can't call Start-Process from a batch file as it's a PowerShell command.
But we can run powershell from a batch file, then using the -command parameter to run Start-Process.
We use Start-Process to run powershell (again) and run your script when it is elevated to Admin: -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs

Related

Powershell: Run script as administrator causes infinite execution

I am completely new to Powershell. Found a script online for updating Windows, and want to run it as administrator. Have a script that starts a new session and calls that script from a network share:
PowerShell.exe -noprofile -command "&{Start-Process PowerShell -ArgumentList
'-noprofile -file \\path\to\networkshare\00_WindowsUpdate.ps1' -Verb RunAs}"
The problem is that it keeps running the script and opening new windows infinitely. I have searched the Internet and could not find anything specific to my issue. How do I run my script to call the Windows Update script once and prevent it from executing infinitely?
Why are you using PowerShell, to call PowerShell, to run PowerShell?
In .cmd:
RUNAS /noprofile /user:domain\user "powershell -ExecutionPolicy Bypass -Command '& \\Path\update.ps1'"

Run powershell script from cmd with admin privileges

I am running a powershell script from cmd and I want to run it with admin rights.
powershell -command "&{. D:\Temp\Untitled1.ps1 ; Method1 'argument1'}"
Since I am not using start-process so I cannot use -verb Runas, what are the other work arounds for the same?

Running PowerShell script from batch file as SYSTEM

There are various posts I have seen showing how to run a PowerShell script from a batch file, however I am still not quite sure how to do this with admin rights or running under the system account. I have a batch file that needs to execute a PowerShell script and run the script as admin or the local system account WITHOUT prompting for credentials. This is running on end user machines that do not have the permissions. This is the best I have found for executing a powershell script from a batch file:
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
PAUSE
But UAC will prompt with the above. Any ideas?

How to call .cmd file as administrator?

Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.
Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd

Send a command to a powershell opened with "Start-Process powershell -Verb runas"

I want to open an admin powershell and send it a command (eventually a script). Right now, it doesn't matter what command, but I've tried things like:
Start-Process powershell -Verb runas < $something
or
$something | Start-process powershell -Verb runas
just to get some text to show up in the new admin powershell window. Any ideas?
That awkward moment when you do a little more research and find what you want: this link will help anyone: how to execute set of commands in elevated mode of powershell
Essentially, add the -argument argument to your command
Start-Process powershell -verb runas -argument dir