Solved Adding & Running .BAT File - As Administrator - from Start Menu


IanMosley

Windows Insider
Power User
VIP
Local time
11:23 PM
Posts
760
Location
UK
OS
Windows 11 Pro v 23H2 (Build 22631.3520)
I wish to run the enclosed .bat file - as administrator - from Start Menu.

Is this possible? And if so, how do I do it?

File contents

@ECHO ON
rundll32.exe pnpclean.dll,RunDLL_PnpClean /drivers/maxclean
dism /Online /Cleanup-Image /CheckHealth
dism /Online /Cleanup-Image /ScanHealth
sfc /scannow
dism /Online /Cleanup-Image /RestoreHealth
PAUSE

EXIT
 
Last edited:

My Computer

System One

  • OS
    Windows 11 Pro v 23H2 (Build 22631.3520)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Precision 3660 Tower Workstation
    CPU
    12th Gen Intel(R) Core(TM) i9-12900 5.10 GHz
    Motherboard
    64-bit operating system, x64-based processor
    Memory
    32.00 GB
    Graphics Card(s)
    Intel UHD Graphics 770
    Sound Card
    Realtek Audio
    Monitor(s) Displays
    Dell P2714H Monitor
    Screen Resolution
    1920 x 1080
    Hard Drives
    1 x 512GB NVME PC801 NVMe SK hynix Boot
    1 x 1TB Seagate ST1000LM049-2GH172 Internal HDD
    1 x 1TB Seagate STGX4000400 External HDD
    1 x 2TB Seagate STGX4000400 External HDD
    1 x 4TB Seagate STGX4000400 External HDD
    PSU
    300 Watts
    Cooling
    Air
    Keyboard
    Microsoft Wired Keyboard 600
    Mouse
    Microsoft USB Basic Optical Mouse v2.0
    Browser
    Firefox
    Antivirus
    Windows Defender + Malwarebytes Premium
    Other Info
    BaseBoard Manufacturer Dell Inc.
    BaseBoard Product 0J1CP3
    BaseBoard Version A01
You can use the following to elevate a command prompt to run those commands... it WILL invoke a UAC auth screen which can be annoying, but I don't know a way to bypass that off the top of my head.

Batch:
@ECHO OFF

:init
setlocal DisableDelayedExpansion
set cmdInvoke=1
set winSysFolder=System32
set "batchPath=%~dpnx0"
rem this works also from cmd shell, other than %~0
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)

ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"

if '%cmdInvoke%'=='1' goto InvokeCmd

ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
goto ExecElevation

:InvokeCmd
ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"

:ExecElevation
"%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

:gotPrivileges
setlocal & cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
rundll32.exe pnpclean.dll,RunDLL_PnpClean /drivers/maxclean
dism /Online /Cleanup-Image /CheckHealth
dism /Online /Cleanup-Image /ScanHealth
sfc /scannow
dism /Online /Cleanup-Image /RestoreHealth
PAUSE
EXIT /B
 

Attachments

  • SystemCheck.bat
    1.6 KB · Views: 15

My Computers

System One System Two

  • OS
    Windows 11 Pro β
    Computer type
    Laptop
    Manufacturer/Model
    HP Pavilion Laptop 15-eg0070wm
    CPU
    Intel® Core™ i7-1165G7
    Memory
    32 GB DDR4-3200 SDRAM
    Graphics Card(s)
    Intel® Iris® Xᵉ Graphics
    Screen Resolution
    1920x1080
    Hard Drives
    2TB PCIe® NVMe™ M.2 SSD
  • Operating System
    macOS Ventura
    Computer type
    PC/Desktop
    Manufacturer/Model
    Apple iMac 27" 5K (2017)
    CPU
    3.4 GHz Quad-Core Intel Core i5
    Memory
    40 GB 2400 MHz DDR4
    Graphics card(s)
    Radeon Pro 570 4 GB
    Monitor(s) Displays
    27" 5K, 34" UW
    Screen Resolution
    Mon 1: 5120 × 2880 Mon 2: 2560 x 1080
    Hard Drives
    32GB NVME, 1TB SSD
Maybe a bit simpler for you.....
Put your batch file in a folder on the C: drive.
Then make a shortcut to that batch file and give it "Run as Administrator" privileges.
And then put the shortcut in the Startup folder, not the batch file itself.

In order to run a batch file with Admin privileges, I've had to run it from a Shortcut, where I select "Run as Administrator".
It's a very old trick, but seems to still work.
Good Luck,
TM :cool:
PS: If UAC bugs you, shut it off! I do!
 

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.
You can use the following to elevate a command prompt to run those commands... it WILL invoke a UAC auth screen which can be annoying, but I don't know a way to bypass that off the top of my head.
This is way beyond my technical skill.

However, do I just create a .bat with these contents? I've tried this but I cannot pin it to the Start Menu.
 

My Computer

System One

  • OS
    Windows 11 Pro v 23H2 (Build 22631.3520)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Precision 3660 Tower Workstation
    CPU
    12th Gen Intel(R) Core(TM) i9-12900 5.10 GHz
    Motherboard
    64-bit operating system, x64-based processor
    Memory
    32.00 GB
    Graphics Card(s)
    Intel UHD Graphics 770
    Sound Card
    Realtek Audio
    Monitor(s) Displays
    Dell P2714H Monitor
    Screen Resolution
    1920 x 1080
    Hard Drives
    1 x 512GB NVME PC801 NVMe SK hynix Boot
    1 x 1TB Seagate ST1000LM049-2GH172 Internal HDD
    1 x 1TB Seagate STGX4000400 External HDD
    1 x 2TB Seagate STGX4000400 External HDD
    1 x 4TB Seagate STGX4000400 External HDD
    PSU
    300 Watts
    Cooling
    Air
    Keyboard
    Microsoft Wired Keyboard 600
    Mouse
    Microsoft USB Basic Optical Mouse v2.0
    Browser
    Firefox
    Antivirus
    Windows Defender + Malwarebytes Premium
    Other Info
    BaseBoard Manufacturer Dell Inc.
    BaseBoard Product 0J1CP3
    BaseBoard Version A01
I attached an identical batch file to the post, so just download that and stick it in Startup
 

My Computers

System One System Two

  • OS
    Windows 11 Pro β
    Computer type
    Laptop
    Manufacturer/Model
    HP Pavilion Laptop 15-eg0070wm
    CPU
    Intel® Core™ i7-1165G7
    Memory
    32 GB DDR4-3200 SDRAM
    Graphics Card(s)
    Intel® Iris® Xᵉ Graphics
    Screen Resolution
    1920x1080
    Hard Drives
    2TB PCIe® NVMe™ M.2 SSD
  • Operating System
    macOS Ventura
    Computer type
    PC/Desktop
    Manufacturer/Model
    Apple iMac 27" 5K (2017)
    CPU
    3.4 GHz Quad-Core Intel Core i5
    Memory
    40 GB 2400 MHz DDR4
    Graphics card(s)
    Radeon Pro 570 4 GB
    Monitor(s) Displays
    27" 5K, 34" UW
    Screen Resolution
    Mon 1: 5120 × 2880 Mon 2: 2560 x 1080
    Hard Drives
    32GB NVME, 1TB SSD
My mistake, I thought you wanted it in Startup, not Start Menu... You should be able to drag/drop the file on the start menu to solve that, if not, there's a tutorial on here for adding batch files/folders/etc to the Start Menu but I don't have a link right now.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro β
    Computer type
    Laptop
    Manufacturer/Model
    HP Pavilion Laptop 15-eg0070wm
    CPU
    Intel® Core™ i7-1165G7
    Memory
    32 GB DDR4-3200 SDRAM
    Graphics Card(s)
    Intel® Iris® Xᵉ Graphics
    Screen Resolution
    1920x1080
    Hard Drives
    2TB PCIe® NVMe™ M.2 SSD
  • Operating System
    macOS Ventura
    Computer type
    PC/Desktop
    Manufacturer/Model
    Apple iMac 27" 5K (2017)
    CPU
    3.4 GHz Quad-Core Intel Core i5
    Memory
    40 GB 2400 MHz DDR4
    Graphics card(s)
    Radeon Pro 570 4 GB
    Monitor(s) Displays
    27" 5K, 34" UW
    Screen Resolution
    Mon 1: 5120 × 2880 Mon 2: 2560 x 1080
    Hard Drives
    32GB NVME, 1TB SSD
Create a shortcut of your batch file anywhere
Move it here C:\ProgramData\Microsoft\Windows\Start Menu\Programs
You now have it in your All Apps menu
You can then Pin it to Start Menu/Task bar & Run as Admin
 

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
If you use the batch I provided, after you add it to the Start Menu, you do NOT have to run as Admin as it will do it for you.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro β
    Computer type
    Laptop
    Manufacturer/Model
    HP Pavilion Laptop 15-eg0070wm
    CPU
    Intel® Core™ i7-1165G7
    Memory
    32 GB DDR4-3200 SDRAM
    Graphics Card(s)
    Intel® Iris® Xᵉ Graphics
    Screen Resolution
    1920x1080
    Hard Drives
    2TB PCIe® NVMe™ M.2 SSD
  • Operating System
    macOS Ventura
    Computer type
    PC/Desktop
    Manufacturer/Model
    Apple iMac 27" 5K (2017)
    CPU
    3.4 GHz Quad-Core Intel Core i5
    Memory
    40 GB 2400 MHz DDR4
    Graphics card(s)
    Radeon Pro 570 4 GB
    Monitor(s) Displays
    27" 5K, 34" UW
    Screen Resolution
    Mon 1: 5120 × 2880 Mon 2: 2560 x 1080
    Hard Drives
    32GB NVME, 1TB SSD
Create a shortcut of your batch file anywhere
Move it here C:\ProgramData\Microsoft\Windows\Start Menu\Programs
You now have it in your All Apps menu
You can then Pin it to Start Menu/Task bar & Run as Admin
I've successfully created a tile - System Diagnostics - on my Start Menu using this method.
However, when I click on it -
1. No option to Run as Administrator; and,
2. All I get is a 'pop-up window' as indicated below -

1.png
 

My Computer

System One

  • OS
    Windows 11 Pro v 23H2 (Build 22631.3520)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Precision 3660 Tower Workstation
    CPU
    12th Gen Intel(R) Core(TM) i9-12900 5.10 GHz
    Motherboard
    64-bit operating system, x64-based processor
    Memory
    32.00 GB
    Graphics Card(s)
    Intel UHD Graphics 770
    Sound Card
    Realtek Audio
    Monitor(s) Displays
    Dell P2714H Monitor
    Screen Resolution
    1920 x 1080
    Hard Drives
    1 x 512GB NVME PC801 NVMe SK hynix Boot
    1 x 1TB Seagate ST1000LM049-2GH172 Internal HDD
    1 x 1TB Seagate STGX4000400 External HDD
    1 x 2TB Seagate STGX4000400 External HDD
    1 x 4TB Seagate STGX4000400 External HDD
    PSU
    300 Watts
    Cooling
    Air
    Keyboard
    Microsoft Wired Keyboard 600
    Mouse
    Microsoft USB Basic Optical Mouse v2.0
    Browser
    Firefox
    Antivirus
    Windows Defender + Malwarebytes Premium
    Other Info
    BaseBoard Manufacturer Dell Inc.
    BaseBoard Product 0J1CP3
    BaseBoard Version A01
Ok, I solved it...

Remove the batch file from C:\ProgramData\Microsoft\Windows\Start Menu\Programs and place it somewhere else on your system.
Right-click the batch file and go to "Send To" -> "Desktop (create shortcut)"
Take this new shortcut and put that in C:\ProgramData\Microsoft\Windows\Start Menu\Programs and Pin it to Start
You do NOT have to run it as Admin as it will do it itself and it should run fine from the new shortcut
 

My Computers

System One System Two

  • OS
    Windows 11 Pro β
    Computer type
    Laptop
    Manufacturer/Model
    HP Pavilion Laptop 15-eg0070wm
    CPU
    Intel® Core™ i7-1165G7
    Memory
    32 GB DDR4-3200 SDRAM
    Graphics Card(s)
    Intel® Iris® Xᵉ Graphics
    Screen Resolution
    1920x1080
    Hard Drives
    2TB PCIe® NVMe™ M.2 SSD
  • Operating System
    macOS Ventura
    Computer type
    PC/Desktop
    Manufacturer/Model
    Apple iMac 27" 5K (2017)
    CPU
    3.4 GHz Quad-Core Intel Core i5
    Memory
    40 GB 2400 MHz DDR4
    Graphics card(s)
    Radeon Pro 570 4 GB
    Monitor(s) Displays
    27" 5K, 34" UW
    Screen Resolution
    Mon 1: 5120 × 2880 Mon 2: 2560 x 1080
    Hard Drives
    32GB NVME, 1TB SSD
The problems I've been having to get this to work have been down to the text editor I use.

All up and working now. Thank you. :-)
 

My Computer

System One

  • OS
    Windows 11 Pro v 23H2 (Build 22631.3520)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Precision 3660 Tower Workstation
    CPU
    12th Gen Intel(R) Core(TM) i9-12900 5.10 GHz
    Motherboard
    64-bit operating system, x64-based processor
    Memory
    32.00 GB
    Graphics Card(s)
    Intel UHD Graphics 770
    Sound Card
    Realtek Audio
    Monitor(s) Displays
    Dell P2714H Monitor
    Screen Resolution
    1920 x 1080
    Hard Drives
    1 x 512GB NVME PC801 NVMe SK hynix Boot
    1 x 1TB Seagate ST1000LM049-2GH172 Internal HDD
    1 x 1TB Seagate STGX4000400 External HDD
    1 x 2TB Seagate STGX4000400 External HDD
    1 x 4TB Seagate STGX4000400 External HDD
    PSU
    300 Watts
    Cooling
    Air
    Keyboard
    Microsoft Wired Keyboard 600
    Mouse
    Microsoft USB Basic Optical Mouse v2.0
    Browser
    Firefox
    Antivirus
    Windows Defender + Malwarebytes Premium
    Other Info
    BaseBoard Manufacturer Dell Inc.
    BaseBoard Product 0J1CP3
    BaseBoard Version A01
Back
Top Bottom