本文目录导读:

获取Windows系统激活状态可以通过多种脚本方式实现,以下是几种常用的方法:
PowerShell脚本(推荐)
使用SoftwareLicensingProduct类
# 获取激活状态
$activation = (Get-CimInstance -ClassName SoftwareLicensingProduct | Where-Object {$_.PartialProductKey -ne $null}).LicenseStatus
switch ($activation) {
0 { Write-Host "未激活 (Unlicensed)" -ForegroundColor Red }
1 { Write-Host "已激活 (Licensed)" -ForegroundColor Green }
2 { Write-Host "超出宽限期 (Out-of-Box Grace)" -ForegroundColor Yellow }
3 { Write-Host "超出宽限期通知 (Out-of-Tolerance)" -ForegroundColor Yellow }
4 { Write-Host "非正版 (Non-Genuine)" -ForegroundColor Red }
5 { Write-Host "通知 (Notification)" -ForegroundColor Yellow }
default { Write-Host "未知状态" -ForegroundColor Red }
}
使用slmgr.vbs
# 执行slmgr命令并解析输出
$result = cscript //nologo "$env:SystemRoot\System32\slmgr.vbs" /dli
if ($result -match "许可证状态:已授权" -or $result -match "License Status: Licensed") {
Write-Host "系统已激活" -ForegroundColor Green
} else {
Write-Host "系统未激活" -ForegroundColor Red
}
VBScript脚本
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMI.ExecQuery("SELECT * FROM SoftwareLicensingProduct WHERE PartialProductKey IS NOT NULL")
For Each objItem in colItems
Select Case objItem.LicenseStatus
Case 0
WScript.Echo "未激活 (Unlicensed)"
Case 1
WScript.Echo "已激活 (Licensed)"
Case 2
WScript.Echo "超出宽限期 (Out-of-Box Grace)"
Case 3
WScript.Echo "超出宽限期通知 (Out-of-Tolerance)"
Case 4
WScript.Echo "非正版 (Non-Genuine)"
Case 5
WScript.Echo "通知 (Notification)"
Case Else
WScript.Echo "未知状态"
End Select
Next
批处理脚本 (CMD)
@echo off
cscript //nologo %windir%\system32\slmgr.vbs /dli | findstr /i "许可证状态" >nul
if %errorlevel% equ 0 (
echo 系统已激活
) else (
cscript //nologo %windir%\system32\slmgr.vbs /dli | findstr /i "License Status" >nul
if %errorlevel% equ 0 (
echo System is activated
) else (
echo 系统未激活
)
)
更详细的PowerShell脚本(包含所有信息)
# 获取详细的激活信息
$products = Get-CimInstance -ClassName SoftwareLicensingProduct | Where-Object {$_.PartialProductKey -ne $null}
foreach ($product in $products) {
$status = switch ($product.LicenseStatus) {
0 { "未激活" }
1 { "已激活" }
2 { "超出宽限期" }
3 { "超出宽限期通知" }
4 { "非正版" }
5 { "通知" }
default { "未知" }
}
Write-Host "产品名称: $($product.Name)" -ForegroundColor Cyan
Write-Host "激活状态: $status" -ForegroundColor $(if ($product.LicenseStatus -eq 1) {"Green"} else {"Red"})
Write-Host "产品密钥: $($product.PartialProductKey)"
Write-Host "------------------------"
}
# 获取到期日期
$gracePeriod = Get-CimInstance -ClassName SoftwareLicensingProduct | Where-Object {$_.PartialProductKey -ne $null}
if ($gracePeriod.GracePeriodRemaining) {
$days = [math]::Round($gracePeriod.GracePeriodRemaining / (24*60))
Write-Host "剩余宽限期: $days 天" -ForegroundColor Yellow
}
注意事项
- 管理员权限:某些命令需要以管理员身份运行
- 系统版本:不同Windows版本可能返回不同结果
- KMS激活:企业版通常使用KMS激活,状态可能显示为"已授权"
- 兼容性:Windows 7/8/10/11均支持上述方法
推荐使用第一种PowerShell方法,它最可靠且跨版本兼容性好。