PowerShell command with admin privileges in .Bat batch script


goroc52121

New member
Local time
2:48 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
    Win11 All /Debian/Arch
    Computer type
    Laptop
    Manufacturer/Model
    ASUSTeK COMPUTER INC. TUF Gaming FX705GM
    CPU
    2.20 gigahertz Intel i7-8750H Hyper-threaded 12 cores
    Motherboard
    ASUSTeK COMPUTER INC. FX705GM 1.0
    Memory
    24428 Megabytes
    Graphics Card(s)
    Intel(R) UHD Graphics 630 / NVIDIA GeForce GTX 1060
    Sound Card
    Intel(R) Display Audio / Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (17.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9
    Hard Drives
    2 SSD SATA/NVM Express 1.3
    WDS500G2B0A-00SM50 500.1 GB
    WDCSDAPNUW-1002 256 GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & ADSL Bouygues -fr
    Browser
    Edge Canary- Firefox Nightly
    Antivirus
    Windows Defender
    Other Info
    VMs of Windows 11 stable/Beta/Dev/Canary
    VM of XeroLinux- Arch based & Debian 12
  • Operating System
    Windows 11 Insider Canary
    Computer type
    Laptop
    Manufacturer/Model
    ASUS X751BP
    CPU
    AMD Dual Core A6-9220
    Motherboard
    ASUS
    Memory
    8 GB
    Graphics card(s)
    AMD Radeon R5 M420
    Sound Card
    Realtek
    Monitor(s) Displays
    17.3
    Screen Resolution
    1600X900 16:9
    Hard Drives
    1TB 5400RPM
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

System One

  • OS
    Win-11/Pro/64, Optimum 11 V5, 23H2 22631.3374
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Made w/Gigabyte mobo/DX-10
    CPU
    AMD FX 6350 Six Core
    Motherboard
    Gigabyte, DX-10, GA-78LMT-USB3
    Memory
    Crucial, 16 GB
    Graphics Card(s)
    NVIDEA GeForce 210, 1GB DDR3 Ram.
    Sound Card
    Onboard
    Monitor(s) Displays
    24" Acer
    Screen Resolution
    1280x800
    Hard Drives
    Crucial SSD 500GB, SanDisk 126GB SSD, Toshiba 1TB HD
    PSU
    EVGA 500 W.
    Case
    Pac Man, Mid Tower
    Cooling
    AMD/OEM
    Keyboard
    101 key, Backlit/ Mechanical Switches/
    Mouse
    Logitech USB Wireless M310
    Internet Speed
    Hughes Net speed varies with the weather
    Browser
    Firefox 64x
    Antivirus
    Windows Defender, Super Anti Spyware
    Other Info
    Given to me as DEAD, and irreparable.
    Rebuilt with Gigabyte mobo, AMD cpu, 16GB ram and 500GB Crucial SSD.
Works on all my scripts, here is one i have attached.
 

Attachments

  • Print_Directory.bat
    2 KB · Views: 1

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
    Win11 All /Debian/Arch
    Computer type
    Laptop
    Manufacturer/Model
    ASUSTeK COMPUTER INC. TUF Gaming FX705GM
    CPU
    2.20 gigahertz Intel i7-8750H Hyper-threaded 12 cores
    Motherboard
    ASUSTeK COMPUTER INC. FX705GM 1.0
    Memory
    24428 Megabytes
    Graphics Card(s)
    Intel(R) UHD Graphics 630 / NVIDIA GeForce GTX 1060
    Sound Card
    Intel(R) Display Audio / Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (17.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9
    Hard Drives
    2 SSD SATA/NVM Express 1.3
    WDS500G2B0A-00SM50 500.1 GB
    WDCSDAPNUW-1002 256 GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & ADSL Bouygues -fr
    Browser
    Edge Canary- Firefox Nightly
    Antivirus
    Windows Defender
    Other Info
    VMs of Windows 11 stable/Beta/Dev/Canary
    VM of XeroLinux- Arch based & Debian 12
  • Operating System
    Windows 11 Insider Canary
    Computer type
    Laptop
    Manufacturer/Model
    ASUS X751BP
    CPU
    AMD Dual Core A6-9220
    Motherboard
    ASUS
    Memory
    8 GB
    Graphics card(s)
    AMD Radeon R5 M420
    Sound Card
    Realtek
    Monitor(s) Displays
    17.3
    Screen Resolution
    1600X900 16:9
    Hard Drives
    1TB 5400RPM
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
    Windows 11 Pro with Debian Linux in WSL 2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Build to Order machine
    CPU
    Intel Core i7-4790 (Haswell Refresh)
    Motherboard
    MSI Z97 Gaming 7
    Memory
    32 GB
    Graphics Card(s)
    GIGABYTE NVIDIA GeForce RTX 2060 GAMING OC PRO 6G
    Monitor(s) Displays
    LG 27GN650-B IPS HDR Gaming Monitor 27" FHD
    Screen Resolution
    1080p
    Hard Drives
    2 x Samsung 860 EVO SATA SSD 1TB
    1 x Samsung 870 EVO SATA SSD 2TB
    PSU
    Corsair 1000 Watt
    Case
    Corsair Obsidian Series 750D full tower ATX case
    Cooling
    CORSAIR Hydro Series H80i v2
    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