批处理命令 | 改变字体编码、右键菜单上以管理员身份运行CMD



1. 回显相关命令
命令 作用
@echo on 打开回显
@echo off 关闭回显

提示:
当不想批处理文件中的命令一行一行在DOS中显示出来时可以使用@echo off,将其放于程序的最上方就行了

2.字体编码

chcp

批处理命令 | 改变字体编码、右键菜单上以管理员身份运行CMD_第1张图片

提示:

  • UTF-8 65001
  • GB2312 936

参阅:代码页标识符

4. 命令行结果不显示

若要命令行结果不显示,则输出重定向到null
>nul
例如:chcp 65001 >nul

5.自动以管理员身份运行bat
  • 第一种
@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

  • 第二种

fltmc>nul||cd/d %~dp0&&mshta vbscript:CreateObject("Shell.Application").ShellExecute("%~nx0","%1","","runas",1)(window.close)&&exit

  • 第三种

::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 4
:: Automatically check & get admin rights
::::::::::::::::::::::::::::::::::::::::::::
 @echo off
 CLS
 ECHO.
 ECHO =============================
 ECHO  Running CMD As Admin
 ECHO =============================

:init
 setlocal DisableDelayedExpansion
 set cmdInvoke=1
 set winSysFolder=System32
 set "batchPath=%~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.
  ECHO **************************************
  ECHO Invoking UAC for Privilege Escalation
  ECHO **************************************

  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
 ::::::::::::::::::::::::::::

提醒:将自己代码放在最后面即可以管理员身份运行批处理命令

6. 右键菜单运行CMD
  • 第一种:批处理命令
@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

@echo off
echo.
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCMDHere /v icon /t REG_SZ /d C:\windows\system32\cmd.exe /f || echo **失败**
echo.
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCMDHere /v ShowBasedOnVelocityId /t REG_DWORD /d 0x00639bc8 /f || echo **失败**
echo.
reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCMDHere\command /ve  /t REG_SZ /d "cmd.exe /s /k pushd \"%%V\" " /f || echo **失败**
echo.
pause

  • 第二种:reg文件
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCMDHere]
"ShowBasedOnVelocityId"=dword:00639bc8

[HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCMDHere\command]
@="cmd.exe /s /k pushd \"%V\""

reg add HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCMDHere /v icon /t REG_SZ /d C:\windows\system32\cmd.exe /f

6. 右键菜单以管理员身份运行CMD
Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\Directory\shell\runas]
[HKEY_CLASSES_ROOT\Directory\shell\runas]
@="Run cmd as Admin"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

[-HKEY_CLASSES_ROOT\Directory\Background\shell\runas]
[HKEY_CLASSES_ROOT\Directory\Background\shell\runas]
@="Run cmd as Admin"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

[-HKEY_CLASSES_ROOT\Drive\shell\runas]
[HKEY_CLASSES_ROOT\Drive\shell\runas]
@="Run cmd as Admin"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Drive\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

[-HKEY_CLASSES_ROOT\LibraryFolder\background\shell\runas]
[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\runas]
"HasLUAShield"=""
@="Run cmd as Admin"

[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\runas\command]

参阅:

  • windows 批处理脚本自动以管理员权限运行

更新中......


你可能感兴趣的:(批处理命令 | 改变字体编码、右键菜单上以管理员身份运行CMD)