检查 Windows 终端是否以管理员身份运行
用 WindowsPrincipal 一行代码检测当前 PowerShell 是否有管理员权限。
运行 `([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)`,输出 True 表示已提权,False 表示没有。
01命令
命令
$ ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)02作用说明
很多命令在没有管理员权限时会报出莫名其妙错误,所以先检查一下很有必要。这行代码把当前令牌与内置管理员角色比较,直接输出 True 或 False。在 CMD 里也可以用 `net session` 快速判断:如果提示拒绝访问,说明当前控制台没有管理员权限。
03风险
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Get-NetFirewallProfile依据: Returns Domain, Private and Public firewall profiles with their Enabled state.
- Whoami - Windows CMD依据: whoami /groups output includes the Mandatory Label integrity level; S-1-16-12288 indicates a high-integrity (elevated) process.
最后验证: 2026-09-18 · 分类: 安全与隐私