Run powershell script from cmd with admin privileges - powershell

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?

Related

Admin Powershell closing instantly

I'm using a simple script to run another script as admin that will need to change the location of files in areas that are otherwise restricted. I've tried literally every method I could find on stackoverflow for running a script as administrator to no avail.
My 'admin' version of powershell closes instantly when I call it from another script. All I want is to be able to run a script that has adminitrative rights without the thing closing on me instantly when I gave it a script to execute.
Start-Process powershell -Verb runAs
start-process powershell -argument "C:\Scripts\Backup.ps1 TestBackup" -Verb runAs
and the next 6 answers on this page:
PowerShell: Running a command as Administrator
I expected to be able to run the passed script as admin, but instead admin powershell pops up and instantly closes.
I've had issues with relaunching a script as admin, due to execution policy, so I ended up encoding the command and running it that way:
$Code = ". 'C:\Scripts\Backup.ps1' TestBackup"
$Encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))
# Indicate that the process should be elevated
Start-Process PowerShell.exe -Verb RunAs -ArgumentList "-EncodedCommand",$Encoded

Run a Powershell script from Batch file with elevated privileges?

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

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'"

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?

Powershell with elevated permissions through RunOnce

I am trying to run a powershell script using the RunOnce registry key. I need it to run as administrator and I can get the script to run but I can't get it to run with elevated permissions. Here is what I have tried but it doesn't do anything.
My code:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -verb runas -File c:\script.ps1
Any Ideas?
Try powershell.exe -executionpolicy unrestricted -file ..... and remove the set-excecution unrestricted line in your script as it won't have any effect. If powershell is able to read that command in your script file, then it means the script is allowed => execution-policy doesn't need to be changed.
Set-ExecutionPolicy is an interactive command, it does not belong in a script(unless running on a remote computer maybe).
Also, if you're trying to execute it with RunOnce, then make sure it's run using the HKLM RunOnce key to make sure it runs in the SYSTEM context(so it has the proper permissions).