{"id":3968,"date":"2016-09-02T09:51:14","date_gmt":"2016-09-02T04:21:14","guid":{"rendered":"http:\/\/198.58.113.91\/blog\/?p=3968"},"modified":"2024-07-26T16:23:55","modified_gmt":"2024-07-26T10:53:55","slug":"automatically-elevate-batch-file-run-administrator","status":"publish","type":"post","link":"https:\/\/www.winhelponline.com\/blog\/automatically-elevate-batch-file-run-administrator\/","title":{"rendered":"How to Automatically Elevate a Batch file to Run it as Administrator?"},"content":{"rendered":"<p>To elevate batch files manually, you would right-click on it and choose Run as Administrator. Here is a way to automatically elevate a batch file that requires elevated privileges to run correctly. This is equivalent to choosing &#8220;Run as Administrator&#8221; by right-clicking a batch file. In either case, the UAC prompt would still show up.<!--more--><\/p>\n<h2>Automatically Elevating a Batch File<\/h2>\n<pre><code>::::::::::::::::::::::::::::::::::::::::::::\n:: Elevate.cmd - Version 45\n:: Automatically check & get admin rights\n:: see \"https:\/\/stackoverflow.com\/a\/12264592\/1016343\" for description\n::::::::::::::::::::::::::::::::::::::::::::\n @echo off\n CLS\n ECHO.\n ECHO =============================\n ECHO Running Admin shell\n ECHO =============================\n\n:init\n setlocal DisableDelayedExpansion\n set cmdInvoke=1\n set winSysFolder=System32\n set \"batchPath=%~dpnx0\"\n rem this works also from cmd shell, other than %~0\n for %%k in (%0) do set batchName=%%~nk\n set \"vbsGetPrivileges=%temp%\\OEgetPriv_%batchName%.vbs\"\n setlocal EnableDelayedExpansion\n\n:checkPrivileges\n  NET FILE 1>NUL 2>NUL\n  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )\n\n:getPrivileges\n  if '%1'=='ELEV' (echo ELEV & shift \/1 & goto gotPrivileges)\n  ECHO.\n  ECHO **************************************\n  ECHO Invoking UAC for Privilege Escalation\n  ECHO **************************************\n\n  ECHO Set UAC = CreateObject^(\"Shell.Application\"^) > \"%vbsGetPrivileges%\"\n  ECHO args = \"ELEV \" >> \"%vbsGetPrivileges%\"\n  ECHO For Each strArg in WScript.Arguments >> \"%vbsGetPrivileges%\"\n  ECHO args = args ^& strArg ^& \" \"  >> \"%vbsGetPrivileges%\"\n  ECHO Next >> \"%vbsGetPrivileges%\"\n  \n  if '%cmdInvoke%'=='1' goto InvokeCmd \n\n  ECHO UAC.ShellExecute \"!batchPath!\", args, \"\", \"runas\", 1 >> \"%vbsGetPrivileges%\"\n  goto ExecElevation\n\n:InvokeCmd\n  ECHO args = \"\/c \"\"\" + \"!batchPath!\" + \"\"\" \" + args >> \"%vbsGetPrivileges%\"\n  ECHO UAC.ShellExecute \"%SystemRoot%\\%winSysFolder%\\cmd.exe\", args, \"\", \"runas\", 1 >> \"%vbsGetPrivileges%\"\n\n:ExecElevation\n \"%SystemRoot%\\%winSysFolder%\\WScript.exe\" \"%vbsGetPrivileges%\" %*\n exit \/B\n\n:gotPrivileges\n setlocal & cd \/d %~dp0\n if '%1'=='ELEV' (del \"%vbsGetPrivileges%\" 1>nul 2>nul  &  shift \/1)\n\n ::::::::::::::::::::::::::::\n ::START\n ::::::::::::::::::::::::::::\n REM Run shell as admin (example) - put here code as you like\n<\/code><\/pre>\n<p>Add your instructions to this .bat file under the &#8220;START&#8221; label.<\/p>\n<p>This batch file creates a Vbscript file which then re-launches the batch file as administrator (if it&#8217;s not already running under administrator privileges) using the &#8220;runas&#8221; parameter which is needed to elevate it. The Vbscript &#038; &#8220;runas&#8221; method has been covered in my old post <a href=\"https:\/\/www.winhelponline.com\/blog\/vbscripts-and-uac-elevation\/\">Vbscripts and UAC elevation in Windows Vista and Higher<\/a>.<\/p>\n<p><i>Credits to Matt at Stack Overflow for this method. <a target=\"_blank\" href=\"http:\/\/stackoverflow.com\/questions\/7044985\/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator\/25238418\" rel=\"noopener noreferrer\">windows &#8211; How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?<\/a><\/i><\/p>\n<h2>Running Batch Files Elevated by Default<\/h2>\n<p>Besides the above automatic elevation method, from the client&#8217;s side you can tweak the registry so that batch files will always start elevated, showing the UAC prompt. This is done by changing the default double-click action from &#8220;open&#8221; to &#8220;runas&#8221; in the registry. Use these steps:<\/p>\n<p>Start Regedit.exe and go to the following location:<\/p>\n<pre>HKEY_CLASSES_ROOT\\batfile\\shell<\/pre>\n<p>Double-click (default) and set its value data as <strong>runas<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/09\/bat-elevated-2.png\" alt=\"bat file default runas elevated\" width=\"529\" height=\"240\" class=\"alignnone size-full wp-image-3993\" \/><\/p>\n<p>Exit the Registry Editor.<\/p>\n<p>After this change, batch files will always run elevated when double-clicked. Run as administrator would be the default option shown when you right-click a .bat file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/09\/bat-elevated.png\" alt=\"bat file default runas elevated\" width=\"390\" height=\"476\" class=\"alignnone size-full wp-image-3992\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To elevate batch files manually, you would right-click on it and choose Run as Administrator. Here is a way to automatically elevate a batch file that requires elevated privileges to run correctly. This is equivalent to choosing &#8220;Run as Administrator&#8221; by right-clicking a batch file. In either case, the UAC prompt would still show up.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3968","post","type-post","status-publish","format-standard","hentry","category-windows"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1512,"url":"https:\/\/www.winhelponline.com\/blog\/vbscripts-and-uac-elevation\/","url_meta":{"origin":3968,"position":0},"title":"How to Launch a Vbscript in Elevated Mode (Run as Administrator)","author":"Ramesh","date":"April 6, 2007","format":false,"excerpt":"With the introduction of User Account Control (UAC) in Windows Vista, you usually open an elevated Command Prompt in order to run batch files and scripts that need administrative privileges. Applications can make use of manifest files (using the RequireAdministrator flag) to automatically run elevated. For scripts, this article provides\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/"},"img":{"alt_text":"automatically elevate vbscript runas","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2007\/04\/uac-vbscript-elevation.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":6,"url":"https:\/\/www.winhelponline.com\/blog\/create-an-internet-shortcut-using-batch-file\/","url_meta":{"origin":3968,"position":1},"title":"Create an Internet Shortcut Using Batch File","author":"Ramesh","date":"June 13, 2006","format":false,"excerpt":"Q:\u00a0 Is there a way to create a batch file which would put a desktop icon onto the desktop which invokes an HTML page in a web browser and change the associated icon for that particular desktop item, all in one batch file? Thanks A: Yes. This is possible. Here\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.winhelponline.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8794,"url":"https:\/\/www.winhelponline.com\/blog\/clear-windows-10-clipboard-history-shortcut\/","url_meta":{"origin":3968,"position":2},"title":"How to Clear Windows 10 Clipboard History Using Shortcut","author":"Ramesh","date":"April 22, 2019","format":false,"excerpt":"The new Windows 10 Clipboard lets you save multiple items in the clipboard so that you can use it later. You can also pin the required items in the Clipboard history panel so that they're not discarded by the system, or accidentally deleted by the user. You may every now\u2026","rel":"","context":"In &quot;Windows 10&quot;","block_context":{"text":"Windows 10","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/windows-10\/"},"img":{"alt_text":"clipboard history auto-paste to text editor","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/notepad-clipboard-history.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/notepad-clipboard-history.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/notepad-clipboard-history.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":425,"url":"https:\/\/www.winhelponline.com\/blog\/run-bat-files-invisibly-without-displaying-command-prompt\/","url_meta":{"origin":3968,"position":3},"title":"How to run .BAT files invisibly, without displaying the Command Prompt window","author":"Ramesh","date":"August 5, 2008","format":false,"excerpt":"Batch files (.BAT) and Windows NT Command Script (.CMD) files run in console window when double-clicked. This means that the Command Prompt window will be visible until the .BAT or .CMD file execution is complete. To make .BAT or .CMD file execution less intrusive, you can configure it to run\u2026","rel":"","context":"In &quot;Utilities&quot;","block_context":{"text":"Utilities","link":"https:\/\/www.winhelponline.com\/blog\/category\/utilities\/"},"img":{"alt_text":"run batch file hidden - nircmd exec","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2008\/08\/nircmd-batch-file-hidden.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1097,"url":"https:\/\/www.winhelponline.com\/blog\/backup-pinned-taskbar-shortcuts-restore\/","url_meta":{"origin":3968,"position":4},"title":"How to Backup and Restore Pinned Taskbar Shortcuts in Windows 10","author":"Ramesh","date":"October 2, 2010","format":false,"excerpt":"When you install Windows on a new computer, customizing Windows takes a lot of time and effort. One such thing is the taskbar. You may have to redo the procedure of pinning the applications which you frequently use. If you're planning to move to a new Windows PC or a\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/"},"img":{"alt_text":"taskband registry key","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2010\/10\/w10-taskband-key.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2010\/10\/w10-taskband-key.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2010\/10\/w10-taskband-key.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2010\/10\/w10-taskband-key.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":9357,"url":"https:\/\/www.winhelponline.com\/blog\/find-and-delete-zero-byte-files-recursively-windows\/","url_meta":{"origin":3968,"position":5},"title":"How to Delete Empty files (0 byte) in Windows?","author":"Ramesh","date":"May 18, 2019","format":false,"excerpt":"Some applications create empty 0-byte files in their data folders and fail to clear them up. Over time, if you see many zero-byte files in a folder hierarchy, here are some methods to find all those 0-byte files and delete them. Find and\/or delete empty (0 byte) files in Windows:\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/"},"img":{"alt_text":"delete empty files recursively","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/05\/delempty-0-byte-files-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/05\/delempty-0-byte-files-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/05\/delempty-0-byte-files-1.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/05\/delempty-0-byte-files-1.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts\/3968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/comments?post=3968"}],"version-history":[{"count":0,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts\/3968\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/media?parent=3968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/categories?post=3968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/tags?post=3968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}