Solved Powershell command in batch script


DMD

Well-known member
Member
Local time
8:41 PM
Posts
130
OS
W11
Good morning
I am asking how to run a powershell command with administrator privileges in batch script.
The command I am interested in is the one related to App restore.
Code:
Get-AppxPackage -AllUsers| Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"

I have tried to run it in Batch by entering the powershell command but I have no idea how to enter elevated admnistrate privileges.
Any ideas?
Thank you very much.
Code:
powershell "Get-AppxPackage -AllUsers| Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}"
 

My Computer

System One

  • OS
    W11
    Computer type
    PC/Desktop
    CPU
    CPU AMD Ryzen 9 7950X
    Motherboard
    ASUS ROG Strix X670E-F Gaming WiFi
    Memory
    G.Skill Flare X5 32GB Kit DDR5-6000 CL32 (F5-6000J3238F16GX2-FX5)
    Monitor(s) Displays
    LG E2381VR
    Screen Resolution
    1920x1080
    Hard Drives
    1x Samsung Memorie MZ-V8P1T0B 980 PRO SSD 1TB
    2x Western Digital WD Red Nas 4TB
    PSU
    SEASONIC FOCUS PX-850 Plus Platinum
    Case
    Corsair 4000D Airflow
    Cooling
    ARCTIC Liquid Freezer II 280 A-RGB
    Keyboard
    Logitech G510
    Mouse
    Logitech G502 HERO
    Browser
    Firefox
    Antivirus
    Avg free
    Other Info
    Download: 90 Mbps
    Upload: 20Mbps
2 ways.

1. Run the batch as admin - it should pass admin privileges to PowerShell.


2. The other way is to re-script that PowerShell command as noted in the following link to have PowerShell start itself with elevated privileges using -Verb runAs.


The first answer is what I'm referring to.

HTH
 

My Computers

System One System Two

  • OS
    Windows 11 23H2 Current build
    Computer type
    PC/Desktop
    Manufacturer/Model
    HomeBrew
    CPU
    AMD Ryzen 9 3950X
    Motherboard
    MSI MEG X570 GODLIKE
    Memory
    4 * 32 GB - Corsair Vengeance 3600 MHz
    Graphics Card(s)
    EVGA GeForce RTX 3080 Ti XC3 ULTRA GAMING (12G-P5-3955-KR)
    Sound Card
    Realtek® ALC1220 Codec
    Monitor(s) Displays
    2x Eve Spectrum ES07D03 4K Gaming Monitor (Matte) | Eve Spectrum ES07DC9 4K Gaming Monitor (Glossy)
    Screen Resolution
    3x 3840 x 2160
    Hard Drives
    3x Samsung 980 Pro NVMe PCIe 4 M.2 2 TB SSD (MZ-V8P2T0B/AM) } 3x Sabrent Rocket NVMe 4.0 1 TB SSD (USB)
    PSU
    PC Power & Cooling’s Silencer Series 1050 Watt, 80 Plus Platinum
    Case
    Fractal Design Define 7 XL Dark ATX Full Tower Case
    Cooling
    NZXT KRAKEN Z73 73.11 CFM Liquid CPU Cooler (3x 120 mm push top) + Air 3x 140mm case fans (pull front) + 1x 120 mm (push back) and 1 x 120 mm (pull bottom)
    Keyboard
    SteelSeries Apex Pro Wired Gaming Keyboard
    Mouse
    Logitech MX Master 3S | MX Master 3 for Business
    Internet Speed
    AT&T LightSpeed Gigabit Duplex Ftth
    Browser
    Nightly (default) + Firefox (stable), Chrome, Edge
    Antivirus
    Defender + MB 5 Beta
  • Operating System
    ChromeOS Flex Dev Channel (current)
    Computer type
    Laptop
    Manufacturer/Model
    Dell Latitude E5470
    CPU
    Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz, 2501 Mhz, 2 Core(s), 4 Logical Processor(s)
    Motherboard
    Dell
    Memory
    16 GB
    Graphics card(s)
    Intel(R) HD Graphics 520
    Sound Card
    Intel(R) HD Graphics 520 + RealTek Audio
    Monitor(s) Displays
    Dell laptop display 15"
    Screen Resolution
    1920 * 1080
    Hard Drives
    Toshiba 128GB M.2 22300 drive
    INTEL Cherryville 520 Series SSDSC2CW180A 180 GB SATA III SSD
    PSU
    Dell
    Case
    Dell
    Cooling
    Dell
    Mouse
    Logitech MX Master 3S (shared w. Sys 1) | Dell TouchPad
    Keyboard
    Dell
    Internet Speed
    AT&T LightSpeed Gigabit Duplex Ftth
I thank you for the valuable information, but I don't quite understand the correct syntax of the command, I wrote it this way, but I think it is wrong.
Code:
powershell start-process powershell -verb runas "Get-AppxPackage -AllUsers| Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}"
 

My Computer

System One

  • OS
    W11
    Computer type
    PC/Desktop
    CPU
    CPU AMD Ryzen 9 7950X
    Motherboard
    ASUS ROG Strix X670E-F Gaming WiFi
    Memory
    G.Skill Flare X5 32GB Kit DDR5-6000 CL32 (F5-6000J3238F16GX2-FX5)
    Monitor(s) Displays
    LG E2381VR
    Screen Resolution
    1920x1080
    Hard Drives
    1x Samsung Memorie MZ-V8P1T0B 980 PRO SSD 1TB
    2x Western Digital WD Red Nas 4TB
    PSU
    SEASONIC FOCUS PX-850 Plus Platinum
    Case
    Corsair 4000D Airflow
    Cooling
    ARCTIC Liquid Freezer II 280 A-RGB
    Keyboard
    Logitech G510
    Mouse
    Logitech G502 HERO
    Browser
    Firefox
    Antivirus
    Avg free
    Other Info
    Download: 90 Mbps
    Upload: 20Mbps
You missed the -Command right after the initial PowerShell.

Let me fire up a VM and test this after making sure my checkpoints are up to date.

Edit - there is more. Firing up the VM to test the final working version.
 

My Computers

System One System Two

  • OS
    Windows 11 23H2 Current build
    Computer type
    PC/Desktop
    Manufacturer/Model
    HomeBrew
    CPU
    AMD Ryzen 9 3950X
    Motherboard
    MSI MEG X570 GODLIKE
    Memory
    4 * 32 GB - Corsair Vengeance 3600 MHz
    Graphics Card(s)
    EVGA GeForce RTX 3080 Ti XC3 ULTRA GAMING (12G-P5-3955-KR)
    Sound Card
    Realtek® ALC1220 Codec
    Monitor(s) Displays
    2x Eve Spectrum ES07D03 4K Gaming Monitor (Matte) | Eve Spectrum ES07DC9 4K Gaming Monitor (Glossy)
    Screen Resolution
    3x 3840 x 2160
    Hard Drives
    3x Samsung 980 Pro NVMe PCIe 4 M.2 2 TB SSD (MZ-V8P2T0B/AM) } 3x Sabrent Rocket NVMe 4.0 1 TB SSD (USB)
    PSU
    PC Power & Cooling’s Silencer Series 1050 Watt, 80 Plus Platinum
    Case
    Fractal Design Define 7 XL Dark ATX Full Tower Case
    Cooling
    NZXT KRAKEN Z73 73.11 CFM Liquid CPU Cooler (3x 120 mm push top) + Air 3x 140mm case fans (pull front) + 1x 120 mm (push back) and 1 x 120 mm (pull bottom)
    Keyboard
    SteelSeries Apex Pro Wired Gaming Keyboard
    Mouse
    Logitech MX Master 3S | MX Master 3 for Business
    Internet Speed
    AT&T LightSpeed Gigabit Duplex Ftth
    Browser
    Nightly (default) + Firefox (stable), Chrome, Edge
    Antivirus
    Defender + MB 5 Beta
  • Operating System
    ChromeOS Flex Dev Channel (current)
    Computer type
    Laptop
    Manufacturer/Model
    Dell Latitude E5470
    CPU
    Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz, 2501 Mhz, 2 Core(s), 4 Logical Processor(s)
    Motherboard
    Dell
    Memory
    16 GB
    Graphics card(s)
    Intel(R) HD Graphics 520
    Sound Card
    Intel(R) HD Graphics 520 + RealTek Audio
    Monitor(s) Displays
    Dell laptop display 15"
    Screen Resolution
    1920 * 1080
    Hard Drives
    Toshiba 128GB M.2 22300 drive
    INTEL Cherryville 520 Series SSDSC2CW180A 180 GB SATA III SSD
    PSU
    Dell
    Case
    Dell
    Cooling
    Dell
    Mouse
    Logitech MX Master 3S (shared w. Sys 1) | Dell TouchPad
    Keyboard
    Dell
    Internet Speed
    AT&T LightSpeed Gigabit Duplex Ftth
OK, I can get the elevation part fine, but the full command in PowerShell fails. Same with PowerShell 7.

The first part of the line should be

Batch:
powershell -Command "start-process -verb runas powershell

But it fails on parsing the actual ps command.

I think using the actual example they have in that link, though, might be a trick - instead of passing the entire command, pass a ps file that has the command in it.

Meanwhile - are you trying to run this manually every time, or through a scheduled task, or ... ?

Any PS gurus out there that can chime in?
 

My Computers

System One System Two

  • OS
    Windows 11 23H2 Current build
    Computer type
    PC/Desktop
    Manufacturer/Model
    HomeBrew
    CPU
    AMD Ryzen 9 3950X
    Motherboard
    MSI MEG X570 GODLIKE
    Memory
    4 * 32 GB - Corsair Vengeance 3600 MHz
    Graphics Card(s)
    EVGA GeForce RTX 3080 Ti XC3 ULTRA GAMING (12G-P5-3955-KR)
    Sound Card
    Realtek® ALC1220 Codec
    Monitor(s) Displays
    2x Eve Spectrum ES07D03 4K Gaming Monitor (Matte) | Eve Spectrum ES07DC9 4K Gaming Monitor (Glossy)
    Screen Resolution
    3x 3840 x 2160
    Hard Drives
    3x Samsung 980 Pro NVMe PCIe 4 M.2 2 TB SSD (MZ-V8P2T0B/AM) } 3x Sabrent Rocket NVMe 4.0 1 TB SSD (USB)
    PSU
    PC Power & Cooling’s Silencer Series 1050 Watt, 80 Plus Platinum
    Case
    Fractal Design Define 7 XL Dark ATX Full Tower Case
    Cooling
    NZXT KRAKEN Z73 73.11 CFM Liquid CPU Cooler (3x 120 mm push top) + Air 3x 140mm case fans (pull front) + 1x 120 mm (push back) and 1 x 120 mm (pull bottom)
    Keyboard
    SteelSeries Apex Pro Wired Gaming Keyboard
    Mouse
    Logitech MX Master 3S | MX Master 3 for Business
    Internet Speed
    AT&T LightSpeed Gigabit Duplex Ftth
    Browser
    Nightly (default) + Firefox (stable), Chrome, Edge
    Antivirus
    Defender + MB 5 Beta
  • Operating System
    ChromeOS Flex Dev Channel (current)
    Computer type
    Laptop
    Manufacturer/Model
    Dell Latitude E5470
    CPU
    Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz, 2501 Mhz, 2 Core(s), 4 Logical Processor(s)
    Motherboard
    Dell
    Memory
    16 GB
    Graphics card(s)
    Intel(R) HD Graphics 520
    Sound Card
    Intel(R) HD Graphics 520 + RealTek Audio
    Monitor(s) Displays
    Dell laptop display 15"
    Screen Resolution
    1920 * 1080
    Hard Drives
    Toshiba 128GB M.2 22300 drive
    INTEL Cherryville 520 Series SSDSC2CW180A 180 GB SATA III SSD
    PSU
    Dell
    Case
    Dell
    Cooling
    Dell
    Mouse
    Logitech MX Master 3S (shared w. Sys 1) | Dell TouchPad
    Keyboard
    Dell
    Internet Speed
    AT&T LightSpeed Gigabit Duplex Ftth
What you're seeing is the headache of invoking a PS expression, inside a PS command line, inside of a batch file. Each level of CMD or PowerShell wants to eat your special characters. Now you could spent all day learning to add escape characters to this Rube Goldberg mess, or just use a pre-made CMD wrapper.

Fortunately, I have one that will self-elevate when needed. Add your PS expression or code after the "#>"

Code:
<# : batch script
@echo off
powershell -nop ^
"if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) { ^
    Start-Process -Verb RunAs 'cmd.exe' -ArgumentList '/c %~dpnx0 %*' ^
} ^
else { ^
    Invoke-Expression ('$args = @(''%*'' -split '' '')' + [System.IO.File]::ReadAllText('%~f0')) ^
}"
goto :eof
#>

Get-AppxPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}
 
Last edited:

My Computer

System One

  • OS
    Windows 7
Thank you very much, you were kind and helped me to solve this problem.
Also, I added the command to run with administrative privileges.
Just launch the file in normal mode, execution is done in administrative mode by running the powershell command.
Code:
<# : batch script
@echo off

@chcp 28591 > nul
::@mode con cols=101 lines=20
@Title Running in ADMINISTRATOR mode
:: We run the commands with administrator permissions
::------------------------------------------
:: --> Verification of authorizations
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
:: --> Error you do not have administrator rights
if '%errorlevel%' NEQ '0' (
echo     Requesting administrator privileges
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
@echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "%~s0", "%params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"

powershell -nop ^
"if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) { ^
    Start-Process -Verb RunAs 'cmd.exe' -ArgumentList '/c %~dpnx0 %*' ^
} ^
else { ^
    Invoke-Expression ('$args = @(''%*'' -split '' '')' + [System.IO.File]::ReadAllText('%~f0')) ^
}"
goto :eof
#>

Get-AppxPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

exit
 
Last edited:

My Computer

System One

  • OS
    W11
    Computer type
    PC/Desktop
    CPU
    CPU AMD Ryzen 9 7950X
    Motherboard
    ASUS ROG Strix X670E-F Gaming WiFi
    Memory
    G.Skill Flare X5 32GB Kit DDR5-6000 CL32 (F5-6000J3238F16GX2-FX5)
    Monitor(s) Displays
    LG E2381VR
    Screen Resolution
    1920x1080
    Hard Drives
    1x Samsung Memorie MZ-V8P1T0B 980 PRO SSD 1TB
    2x Western Digital WD Red Nas 4TB
    PSU
    SEASONIC FOCUS PX-850 Plus Platinum
    Case
    Corsair 4000D Airflow
    Cooling
    ARCTIC Liquid Freezer II 280 A-RGB
    Keyboard
    Logitech G510
    Mouse
    Logitech G502 HERO
    Browser
    Firefox
    Antivirus
    Avg free
    Other Info
    Download: 90 Mbps
    Upload: 20Mbps
Back
Top Bottom