批处理bat和powershell提权运行

批处理bat提权

在windows更新25H2后,许多提权的脚本都失效了,建议使用方法一进行提权。
在windows更新25H2后,许多提权的脚本都失效了,建议使用方法一进行提权。

方式一:

使用poershell提权

# 无需传递参数使用这个
net session >nul 2>&1 || powershell -NoP -C "Start-Process -FilePath '%~f0' -Verb RunAs" && exit

# 如果需要传递参数使用这个
net session >nul 2>&1 || (powershell -NoP -C "Start-Process -FilePath 'cmd.exe' -ArgumentList '/c', '\"\"%~f0\" %*\"' -Verb RunAs" && exit)

方式二:

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

缺点:如果已经是管理员的话,运行可能会有问题。

方式三:

方法二的改进版本,解决了先判断是否是管理员的问题。

net session >nul 2>&1||mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c cd /d ""%~dp0"" && %~s0 %*","","runas",1)(window.close)&&exit

缺点:无法解决因使用Windows终端(windows terminal)导致无法修改窗口大小问题。

安装 2023 年 5 月 23 日更新 KB5026435 后,在所有版本的 Windows 11 和 Windows 10 22H2 版本中均可使用Windows终端(windows terminal)。参见: Windows 终端文档

方式四:

使用conhost.exe运行。

Windows 控制台主机(或 conhost.exe)既是所有 Windows 控制台 API 的服务器应用程序,也是用于处理命令行应用程序的经典 Windows 用户界面

if "%1" neq "runas" mshta vbscript:CreateObject("Shell.Application").ShellExecute("conhost.exe","cmd.exe /c ""%~f0"" runas %*","","runas",1)(window.close)&&exit
shift && cd /d "%~dp0"

缺点:在还没有开启cmd界面的时候去鼠标移动窗口会导致无法退出,只能任务管理器结束进程。

方式五:

在一个绿化脚本里面看到的提权方式

>NUL 2>&1 REG.exe query "HKU\S-1-5-19" || ( ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs" ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs" "%TEMP%\Getadmin.vbs" DEL /f /q "%TEMP%\Getadmin.vbs" 2>NUL Exit /b )

powershell提权

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb runAs; exit}
posted @ 2025-07-15 13:36  雨中遐想  阅读(117)  评论(0)    收藏  举报