如何用PowerShell查询域内所有机器的Windows版本?
获取网络内Windows机器版本的PowerShell方案
单机器快速查询
用Get-ComputerInfo(PowerShell 5.1+支持)直接获取核心版本信息,这是最准确的原生命令:
Get-ComputerInfo -Property OsName, OsVersion, OsBuildNumber, CsName
CsName:机器名称OsName:完整Windows版本名称(如Windows 10 Enterprise 22H2)OsVersion:版本号(如10.0.19045)OsBuildNumber:内部版本号(用于补丁匹配)
如果是老版本PowerShell(低于5.1),可以用Get-CimInstance替代:
Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, BuildNumber, CSName
批量查询网络内多机器
- 先准备一个包含目标机器名/IP的文本文件(比如
computers.txt),每行一个地址:
PC-001 PC-002 192.168.1.100
- 执行批量查询命令(需具备目标机器的管理员权限,域环境通常已开启WinRM,工作组机器需提前配置WinRM):
# 读取机器列表 $computerList = Get-Content .\computers.txt # 批量执行查询并收集结果 $versionResults = Invoke-Command -ComputerName $computerList -ScriptBlock { Get-ComputerInfo -Property OsName, OsVersion, OsBuildNumber, CsName } -ErrorAction SilentlyContinue -ErrorVariable connectionErrors # 整理结果(PSComputerName为实际连接的机器标识,避免CsName与实际机器名不一致) $cleanResults = $versionResults | Select-Object CsName, OsName, OsVersion, OsBuildNumber, PSComputerName
输出到可筛选的文件
将结果导出为CSV格式(最适合筛选、搜索和后续分析,Excel或PowerShell都能直接处理):
$cleanResults | Export-Csv -Path .\WindowsOSVersions.csv -NoTypeInformation -Encoding UTF8
后续筛选示例
- 用Excel打开CSV文件,直接用筛选功能筛选特定版本
- 用PowerShell搜索指定版本:
Import-Csv .\WindowsOSVersions.csv | Where-Object OsName -match "Windows 11"
可选:收集连接错误信息
如果部分机器无法连接,可导出错误日志排查:
$connectionErrors | Select-Object TargetObject, Exception.Message | Export-Csv -Path .\ConnectionFailures.csv -NoTypeInformation -Encoding UTF8
内容的提问来源于stack exchange,提问作者Tomsta
相关产品推荐
相关产品推荐

