PowerShell command with admin privileges in .Bat batch script


goroc52121

New member
Local time
2:35 PM
Posts
8
OS
Windows 11
Good morning,
I'm asking how to run a PowerShell command with admin privileges in .Bat batch script.
The command that interests me is the one related to removing applications in Windows 11.

Example: Winget uninstall Microsoft.OutlookForWindows_8wekyb3d8bbwe
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Cu Guloso
Good morning,
I'm asking how to run a PowerShell command with admin privileges in .Bat batch script.
The command that interests me is the one related to removing applications in Windows 11.

Example: Winget uninstall Microsoft.OutlookForWindows_8wekyb3d8bbwe
There seems to be some confusion on the syntax as well as the difference between a bat,cmd script and a powershell script.


Command prompt:

winget uninstall "<PackageName>" ie:
Code:
winget uninstall "Microsoft.OutlookForWindows"

In Powershell:

Uninstall-Package -Name <PackageName>
To get the package list in Powershell:

Code:
Get-AppxProvisionedPackage -Online | Format-Table DisplayName, PackageName
 

My Computers

System One System Two

  • OS
    All Branches but Release
    Computer type
    Laptop
    Manufacturer/Model
    Acer Nitro ANV15-51
    CPU
    AMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 2
    Motherboard
    Sportage_RBH
    Memory
    32 GB DDR5
    Graphics Card(s)
    Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB GDDR6
    Sound Card
    AMD/Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (15.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9 144Hz
    Hard Drives
    KINGSTON OM8SEP4512Q-AA 1TB
    Western Digital 256GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & 1GB Orange-fr
    Browser
    Edge Canary- Firefox Nightly-Chrome Dev-Chrome Dev
    Antivirus
    Windows Defender
  • Operating System
    Windows 11 Beta
    Computer type
    Laptop
    Manufacturer/Model
    Asus X751BP
    CPU
    AMD A9-9420
    Memory
    8 GB of DDR4
    Graphics card(s)
    AMD Radeon R5
    Screen Resolution
    1600x900
    Hard Drives
    Seagate 1 TB
There seems to be some confusion on the syntax as well as the difference between a bat,cmd script and a powershell script.


Command prompt:

winget uninstall "<PackageName>" ie:
Code:
winget uninstall "Microsoft.OutlookForWindows"

In Powershell:

Uninstall-Package -Name <PackageName>
To get the package list in Powershell:

Code:
Get-AppxProvisionedPackage -Online | Format-Table DisplayName, PackageName
I want to run the .bat command to open Powershell as an administrator and automatically delete apps.

Sorry if I'm not being clear, as I'm just starting.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Cu Guloso
Below command when place beginning of the batch script will elevate batch script and run it as admin.

Code:
(Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
Below command when place beginning of the batch script will elevate batch script and run it as admin.

Code:
(Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)
Thanks for the quick response, but it's not working.

.Bat does not open
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Cu Guloso
All that aside.... You can definitely run a Powershell command in a Batch File (.bat) (not called a script)

And if you want the batch file to "Run As Administrator", then run it from a shortcut, and in the Properties of the shortcut, specify that you want it to "Run As Administrator". Simple.

I use a lot of batch files, and I run them all as "Administrator", even the ones that really don't require it.

I have several Powershell commands in my "Cleanup.bat" batch file program, as well as standard DOS Batch Commands, and I run it "As Administrator", from a desktop shortcut, as I said above.

If you want to just run a batch file without a shortcut, then Right Click on the batch file and from the context menu, click on, "Run As Administrator". Again, very Simple.

Good Luck,
TM :cool:
 

My Computer

Works on all my scripts, here is one i have attached.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
I was able to open Powershell as administrator in .bat, but it doesn't execute the command.

Example:
powershell start-process powershell -verb runas

Winget uninstall Microsoft.OutlookForWindows_8wekyb3d8bbwe;
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Cu Guloso
Winget is not only PowerShell command you can executed without invoking PowerShell.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
I was able to open Powershell as administrator in .bat, but it doesn't execute the command.

Example:
powershell start-process powershell -verb runas

Winget uninstall Microsoft.OutlookForWindows_8wekyb3d8bbwe;
winget uninstall "Microsoft.OutlookForWindows"
The command is valid but this will return package unfound.


Screenshot 2024-04-22 161932.jpeg
 

My Computers

System One System Two

  • OS
    All Branches but Release
    Computer type
    Laptop
    Manufacturer/Model
    Acer Nitro ANV15-51
    CPU
    AMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 2
    Motherboard
    Sportage_RBH
    Memory
    32 GB DDR5
    Graphics Card(s)
    Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB GDDR6
    Sound Card
    AMD/Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (15.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9 144Hz
    Hard Drives
    KINGSTON OM8SEP4512Q-AA 1TB
    Western Digital 256GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & 1GB Orange-fr
    Browser
    Edge Canary- Firefox Nightly-Chrome Dev-Chrome Dev
    Antivirus
    Windows Defender
  • Operating System
    Windows 11 Beta
    Computer type
    Laptop
    Manufacturer/Model
    Asus X751BP
    CPU
    AMD A9-9420
    Memory
    8 GB of DDR4
    Graphics card(s)
    AMD Radeon R5
    Screen Resolution
    1600x900
    Hard Drives
    Seagate 1 TB
winget uninstall "Microsoft.OutlookForWindows"
The command is valid but this will return package unfound.
Thanks.
But it just won't uninstall by running the .bat
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Cu Guloso
This guide will teach you the steps to uninstall applications using the Windows Package Manager command-line tool on Windows 10 and Windows 11.

 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
This guide will teach you the steps to uninstall applications using the Windows Package Manager command-line tool on Windows 10 and Windows 11.

But I want to use it via CMD.
Is there no way to do this?
.bat
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    Cu Guloso
You mean use it from .bat script?
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
Take a look at the following batch file. It self-elevates (i.e. UAC prompt) and then runs the PowerShell code.


Just replace the PowerShell code with the code you want to run.
 

My Computer

System One

  • OS
    Windows 10/11
    Computer type
    Laptop
    Manufacturer/Model
    Acer
Right click the .bat file and select "run as administrator".


Or right-click on the .bat file, and select "create shortcut".

Then you right-click on the new shortcut, and go to "properties-> shortcut-> advanced", and there select "run as administrator".

This way, the shortcut will always run with Administrator privileges.

You can rename the shortcut as you wish.
 

My Computer

System One

  • OS
    macOS
    Computer type
    PC/Desktop
    Manufacturer/Model
    Mac mini M4
    CPU
    Apple silicon M4
    Memory
    32 GB
    Graphics Card(s)
    Apple silicon M4
    Monitor(s) Displays
    LG 27GN650-B IPS HDR Gaming Monitor 27" FHD
    Screen Resolution
    1080p
    Hard Drives
    Apple SSD AP1024Z
    Internet Speed
    1 Gbps / 1 Gbps symmetrical FTTH (GPON)
    Browser
    Microsoft Edge
This is my .bat that removes some Apps during Windows install.
It is called from setupcomplete.cmd


"Win10-removedApps.bat"
---------------------------------------------------------------

cd /d %~dp0
set CurrentPath=%~dp0

powershell "Remove-AppxPackage -AllUsers -Package Microsoft.549981C3F5F10_1.1911.21713.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.BingWeather_4.25.20211.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.GetHelp_10.1706.13331.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.Getstarted_8.2.22942.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.Microsoft3DViewer_6.1908.2042.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.MicrosoftOfficeHub_18.1903.1152.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.MicrosoftSolitaireCollection_4.4.8204.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.MicrosoftStickyNotes_3.6.73.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.MixedReality.Portal_2000.19081.1301.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.Office.OneNote_16001.12026.20112.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.People_2019.305.632.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.SkypeApp_14.53.77.0_neutral_~_kzf8qxf38zg5c
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.Wallet_2.4.18324.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package microsoft.windowscommunicationsapps_16005.11629.20316.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.WindowsFeedbackHub_2019.1111.2029.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.WindowsMaps_2019.716.2316.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.Xbox.TCUI_1.23.28002.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.XboxApp_48.49.31001.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.XboxGameOverlay_1.46.11001.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.XboxGamingOverlay_2.34.28001.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.XboxIdentityProvider_12.50.6001.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.XboxSpeechToTextOverlay_1.17.29001.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.YourPhone_2019.430.2026.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.ZuneMusic_2019.19071.19011.0_neutral_~_8wekyb3d8bbwe
powershell "Remove-AppxPackage -AllUsers -Package Microsoft.ZuneVideo_2019.19071.19011.0_neutral_~_8wekyb3d8bbwe


perhaps it helps
 

My Computer

System One

  • OS
    Windows 10
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom
    CPU
    Ryzen 7 5700X
    Motherboard
    Gigabyte B550 Aorus Elite V2
    Memory
    2 x 16 GB DDR 4 - 3200 - G.Skill RipJaws V
    Graphics Card(s)
    ASUS TUF Gaming GeForce RTX 4070
    Sound Card
    OnBoard Realtek
    Monitor(s) Displays
    2 x Dell S2721DGF
    Screen Resolution
    2560 x 1440 @ 165 MHZ
    Hard Drives
    1 TB Kingston KC3000 M.2PCIe 4.0
    PSU
    be quiet! Pure Power 12 M Modular 80+ Gold
    Case
    Phanteks Eclipse P600s
    Cooling
    be quit! Dark Rock Pro 4
    Keyboard
    Logitech G213 Prodigy
    Mouse
    Logitech G502 Hero
    Internet Speed
    1000 MBit Cable DSL
    Browser
    Firefox
    Antivirus
    Microsoft Defender
Back
Top Bottom