Windowsのバッチファイルを管理者権限かどうかで分岐させてみる

バッチファイルを管理者権限で走らせたい時とか、一般ユーザ権限で実行したい場合とか(例えば net use による環境を分けたい時とか)に参考にしてみて下さい。

C:\>type test.bat
@echo off
openfiles > NUL 2>&1
if NOT %ERRORLEVEL% EQU 0 goto NotAdmin
echo **********************
echo * 管理者権限で実行中 *
echo **********************
sleep 3
hoge
fuga
goto End

:NotAdmin
echo 管理者権限で実行ください
:End

C:\>

 

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny