Run a powershell command as administrator in a bat file - powershell

I have this powershell command that must run as administartor in powershell. I want to run as a batch file as administator. Would you please help me?
Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage "$($_.InstallLocation)\appxmanifest.xml" -DisableDevelopmentMode -register }

To avoid the problem of double/double quote, i suggest you to put your command in a script.ps1 then do this command:
PowerShell -windowstyle hidden -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -windowstyle hidden -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""YourScript.ps1""' -Verb RunAs}"

Try this batch
<# : batch script
#echo off
cd /D "%~dp0"
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
setlocal
cd %~dp0
powershell -executionpolicy remotesigned -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
endlocal
goto:eof
#>
# here write your powershell commands...
Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage "$($_.InstallLocation)\appxmanifest.xml" -DisableDevelopmentMode -register }
save it as .bat or .cmd and run it.

You can run it like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command ""& {Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage ""$($_.InstallLocation)\appxmanifest.xml"" -DisableDevelopmentMode -register }"" -Verb RunAs}"
UPDATE
Fixed double quotes

Related

Executing PowerShell from a Window command batch script using the Start-Process command with named parameters

I am attempting to execute a Powershell Start-Process command with a named parameter list from a Windows command script. I tried including the named parameters;(-productname, -platform, -version, -exepath, -exe, -installMode) in the ArgumentList, however, they when the PowerShell process starts it appears to fail to see them. The command I am using is below:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""setadminstartandcreateshortcut.ps1"" -productname %PRODUCTNAME% -platform %PLATFORM% -version %VERSION% -exepath %TARGET% -exe %PRODUCTNAME%.exe -installMode %INSTALLMODE%' -Verb RunAs}"

How can i put my credentials in an batch file with powershell scripting in it

#ECHO OFF
echo start powershell
call powershell.exe -ExecutionPolicy ByPass -Command "$null=Install-Script -Name Get-WindowsAutoPilotInfo -force"
call powershell.exe -ExecutionPolicy ByPass -Command "& $env:ProgramFiles\WindowsPowerShell\Scripts\Get-WindowsAutoPilotInfo.ps1 -online
This is the .bat file. At the Get-Windowsautopilotinfo.ps1 -online I am asked to enter my credentials twice. Can I automate this process?

How do I run a PowerShell script as administrator using a shortcut?

I'm trying to run a PowerShell script as administrator using a shortcut. I have tried many ways, but it still does not work:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit -Verb RunAs Start-Process powershell.exe -ArgumentList '-file C:\project\test.ps1'
With this command, it will create two PowerShell windows and one window will close.
I also tried this one:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit Start-Process powershell.exe -Verb RunAs -File 'C:\project\test.ps1'
Can some one please help?
Tl;dr
This will do the trick:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"
Explanation
First, you have to call PowerShell to be able to execute Start-Process. You don't need any additional paramters at this point, because you just use this first PowerShell to launch another one. You do it like this:
powershell.exe -Command "& {...}"
Inside the curly braces you can insert any script block. First you will retrieve your current working directory (CWD) to set it in the new launched PowerShell. Then you call PowerShell with Start-Process and add the -Verb RunAs parameter to elevate it:
$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList ...
Then you need to add all desired PowerShell parameters to the ArgumentList. In your case, these will be:
-ExecutionPolicy ByPass -NoExit -Command ...
Finally, you pass the commands that you want to execute to the -Command parameter. Basically, you want to call your script file. But before doing so, you will set your CWD to the previously retrieved directory and THEN call your script:
Set-Location $wd; C:\project\test.ps1
In total:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"

Run powershell commands from cmd / batch

I would like to run the following as administrator:
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName $env:computername
-OutputFile .\computers.csv -append
I would like to simply double click on a .cmd or .bat file and have it invoke the Powershell script as administrator. Here's what I have:
PowerShell "SL -PSPath '%CD%'; $Path = (GL).Path; SL ~; Start
PowerShell -Verb RunAs -Args \"-ExecutionPolicy Unrestricted -Noexit"
SL -PSPath '"$Path"'; & '".\UninstallBloatware.ps1" "-ComputerName
$env:computername" "-OutputFile .\computers.csv" "-append"' "\""
I copied most of the code above from somewhere I can't remember. I don't know enough about quotes structure to know how to fix this. Any ideas what I'm doing wrong?

Trouble running script and passing the parameter. Powershell

I am having trouble with this method for launching my powershell script:
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""c:\Program Files\FileMaker\FileMaker Server\Data\Scripts\updateM2.ps1""' -Verb RunAs}";
The above works.. But I would like to pass the parameter to the script...:
UpdateSandP
So where do I put this.. ?