检查 Windows 终端是否以管理员身份运行

用 WindowsPrincipal 一行代码检测当前 PowerShell 是否有管理员权限。

Windows特权管理员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
    Microsoft Learn · vendor-doc · 2026-09-17
    依据: Returns Domain, Private and Public firewall profiles with their Enabled state.
  • Whoami - Windows CMD
    SS64 · technical · 2026-09-17
    依据: whoami /groups output includes the Mandatory Label integrity level; S-1-16-12288 indicates a high-integrity (elevated) process.

最后验证: 2026-09-18 · 分类: 安全与隐私

06相关方案