powershell实战:无须使用第三方工具进行查看电脑相关配置信息(CPU、内存、硬盘、显卡等)

# =========================
# 一键硬件信息综合检测 - PowerShell
# 显示到控制台 + 自动保存到桌面报告
# =========================
# 报告文件
$OutPath = "$env:USERPROFILE\Desktop\硬件信息报告_{0}.txt" -f (Get-Date -Format "yyyyMMdd_HHmm")
New-Item -Path $OutPath -ItemType File -Force | Out-Null
function Write-Section($title) {
Write-Host ""
Write-Host ("===== {0} =====" -f $title) -ForegroundColor Cyan
"===== {0} =====" -f $title | Out-File -FilePath $OutPath -Append -Encoding UTF8
}
function Append-Table($obj) {
$txt = $obj | Format-Table -AutoSize | Out-String -Width 200
$txt | Write-Host
$txt | Out-File -FilePath $OutPath -Append -Encoding UTF8
}
# ---------- CPU ----------
Write-Section "CPU 信息"
$cpu = Get-CimInstance Win32_Processor | Select-Object `
Name, NumberOfCores, NumberOfLogicalProcessors, MaxClockSpeed
Append-Table $cpu
# ---------- 内存(插槽 + 通道 + 汇总) ----------
Write-Section "内存插槽与通道信息"
# 获取总插槽数量(Win32_PhysicalMemoryArray.MemoryDevices 可能返回总可用插槽)
try {
$memArray = Get-CimInstance Win32_PhysicalMemoryArray -ErrorAction Stop
$totalSlots = ($memArray | Measure-Object -Property MemoryDevices -Sum).Sum
} catch {
$totalSlots = $null
}
$ramRaw = Get-CimInstance Win32_PhysicalMemory | Select-Object `
BankLabel, DeviceLocator, Manufacturer, Capacity, Speed, SMBIOSMemoryType
# DDR 类型映射
function Get-DdrType($code) {
switch ($code) {
24 { "DDR3" }
26 { "DDR4" }
27 { "LPDDR4" }
28 { "DDR5" }
29 { "LPDDR5" }
default { "未知" }
}
}
$ram = $ramRaw | ForEach-Object {
[PSCustomObject]@{
BankLabel = $_.BankLabel
DeviceLocator = $_.DeviceLocator
Manufacturer = $_.Manufacturer
'Capacity(GB)'= [math]::Round($_.Capacity/1GB,2)
Speed = $_.Speed
Type = Get-DdrType $_.SMBIOSMemoryType
}
}
Append-Table $ram
# 汇总
$totalMemGB = [math]::Round(($ramRaw | Measure-Object -Property Capacity -Sum).Sum/1GB,2)
$usedSlots = ($ramRaw | Where-Object { $_.Capacity -gt 0 }).Count
# 简单通道判断(基于 DeviceLocator 命名约定,非绝对)
$channels = ($ramRaw.DeviceLocator | Select-Object -Unique | Where-Object { $_ -match 'Channel[A-Z]' }) `
| ForEach-Object { ($_ -replace '^.*(Channel[A-Z]).*$', '$1') } | Select-Object -Unique
$channelHint = if ($channels.Count -ge 2) { "疑似双通道或以上(检测到:{0})" -f ($channels -join ', ') } `
elseif ($channels.Count -eq 1) { "疑似单通道(检测到:{0})" -f $channels } `
else { "无法从命名推断通道(仅供参考)" }
$memSummary = @()
$memSummary += "总内存容量: $totalMemGB GB"
if ($totalSlots) { $memSummary += "插槽数量(总): $totalSlots" }
$memSummary += "已使用插槽: $usedSlots"
$memSummary += "通道情况: $channelHint"
$memSummaryTxt = ($memSummary -join "`r`n")
Write-Host $memSummaryTxt
$memSummaryTxt | Out-File -FilePath $OutPath -Append -Encoding UTF8
# ---------- 硬盘 ----------
Write-Section "硬盘信息"
function Get-DiskInfo {
if (Get-Command Get-PhysicalDisk -ErrorAction SilentlyContinue) {
# 新式存储栈(Windows 10/11 通常可用)
return Get-PhysicalDisk | Select-Object `
@{n='Name';e={$_.FriendlyName}},
MediaType,
@{n='Size(GB)';e={[math]::Round($_.Size/1GB,2)}},
SerialNumber
} else {
# 兼容方案(老系统 / 无Get-PhysicalDisk)
$d = Get-CimInstance Win32_DiskDrive | Select-Object `
@{n='Name';e={$_.Model}},
InterfaceType,
MediaType,
@{n='Size(GB)';e={[math]::Round($_.Size/1GB,2)}}
return $d
}
}
$disks = Get-DiskInfo
Append-Table $disks
# ---------- 显卡 ----------
Write-Section "显卡信息"
$gpu = Get-CimInstance Win32_VideoController | Select-Object `
Name, DriverVersion, `
@{n='AdapterRAM(GB)';e={ if ($_.AdapterRAM) { [math]::Round($_.AdapterRAM/1GB,2) } else { $null } }}
Append-Table $gpu
# ---------- 主板 ----------
Write-Section "主板信息"
$board = Get-CimInstance Win32_BaseBoard | Select-Object `
Manufacturer, Product, SerialNumber
Append-Table $board
# ---------- 操作系统 ----------
Write-Section "操作系统"
$os = Get-CimInstance Win32_OperatingSystem | Select-Object `
Caption, Version, OSArchitecture, `
@{n='InstallDate';e={ ([Management.ManagementDateTimeConverter]::ToDateTime($_.InstallDate)).ToString("yyyy-MM-dd HH:mm") }}
Append-Table $os
# ---------- 完成 ----------
Write-Host ""
Write-Host ("硬件信息报告已保存到:{0}" -f $OutPath) -ForegroundColor Green
"硬件信息报告已保存到:$OutPath" | Out-File -FilePath $OutPath -Append -Encoding UTF8
# =========================
# 一键硬件信息综合检测 - PowerShell
# 显示到控制台 + 自动保存到桌面报告
# =========================
# 报告文件
$OutPath = "$env:USERPROFILE\Desktop\硬件信息报告_{0}.txt" -f (Get-Date -Format "yyyyMMdd_HHmm")
New-Item -Path $OutPath -ItemType File -Force | Out-Null
function Write-Section($title) {
Write-Host ""
Write-Host ("===== {0} =====" -f $title) -ForegroundColor Cyan
"===== {0} =====" -f $title | Out-File -FilePath $OutPath -Append -Encoding UTF8
}
function Append-Table($obj) {
$txt = $obj | Format-Table -AutoSize | Out-String -Width 200
$txt | Write-Host
$txt | Out-File -FilePath $OutPath -Append -Encoding UTF8
}
# ---------- CPU ----------
Write-Section "CPU 信息"
$cpu = Get-CimInstance Win32_Processor | Select-Object `
Name, NumberOfCores, NumberOfLogicalProcessors, MaxClockSpeed
Append-Table $cpu
# ---------- 内存(插槽 + 通道 + 汇总) ----------
Write-Section "内存插槽与通道信息"
# 获取总插槽数量(Win32_PhysicalMemoryArray.MemoryDevices 可能返回总可用插槽)
try {
$memArray = Get-CimInstance Win32_PhysicalMemoryArray -ErrorAction Stop
$totalSlots = ($memArray | Measure-Object -Property MemoryDevices -Sum).Sum
} catch {
$totalSlots = $null
}
$ramRaw = Get-CimInstance Win32_PhysicalMemory | Select-Object `
BankLabel, DeviceLocator, Manufacturer, Capacity, Speed, SMBIOSMemoryType
# DDR 类型映射
function Get-DdrType($code) {
switch ($code) {
24 { "DDR3" }
26 { "DDR4" }
27 { "LPDDR4" }
28 { "DDR5" }
29 { "LPDDR5" }
default { "未知" }
}
}
$ram = $ramRaw | ForEach-Object {
[PSCustomObject]@{
BankLabel = $_.BankLabel
DeviceLocator = $_.DeviceLocator
Manufacturer = $_.Manufacturer
'Capacity(GB)'= [math]::Round($_.Capacity/1GB,2)
Speed = $_.Speed
Type = Get-DdrType $_.SMBIOSMemoryType
}
}
Append-Table $ram
# 汇总
$totalMemGB = [math]::Round(($ramRaw | Measure-Object -Property Capacity -Sum).Sum/1GB,2)
$usedSlots = ($ramRaw | Where-Object { $_.Capacity -gt 0 }).Count
# 简单通道判断(基于 DeviceLocator 命名约定,非绝对)
$channels = ($ramRaw.DeviceLocator | Select-Object -Unique | Where-Object { $_ -match 'Channel[A-Z]' }) `
| ForEach-Object { ($_ -replace '^.*(Channel[A-Z]).*$', '$1') } | Select-Object -Unique
$channelHint = if ($channels.Count -ge 2) { "疑似双通道或以上(检测到:{0})" -f ($channels -join ', ') } `
elseif ($channels.Count -eq 1) { "疑似单通道(检测到:{0})" -f $channels } `
else { "无法从命名推断通道(仅供参考)" }
$memSummary = @()
$memSummary += "总内存容量: $totalMemGB GB"
if ($totalSlots) { $memSummary += "插槽数量(总): $totalSlots" }
$memSummary += "已使用插槽: $usedSlots"
$memSummary += "通道情况: $channelHint"
$memSummaryTxt = ($memSummary -join "`r`n")
Write-Host $memSummaryTxt
$memSummaryTxt | Out-File -FilePath $OutPath -Append -Encoding UTF8
# ---------- 硬盘 ----------
Write-Section "硬盘信息"
function Get-DiskInfo {
if (Get-Command Get-PhysicalDisk -ErrorAction SilentlyContinue) {
# 新式存储栈(Windows 10/11 通常可用)
return Get-PhysicalDisk | Select-Object `
@{n='Name';e={$_.FriendlyName}},
MediaType,
@{n='Size(GB)';e={[math]::Round($_.Size/1GB,2)}},
SerialNumber
} else {
# 兼容方案(老系统 / 无Get-PhysicalDisk)
$d = Get-CimInstance Win32_DiskDrive | Select-Object `
@{n='Name';e={$_.Model}},
InterfaceType,
MediaType,
@{n='Size(GB)';e={[math]::Round($_.Size/1GB,2)}}
return $d
}
}
$disks = Get-DiskInfo
Append-Table $disks
# ---------- 显卡 ----------
Write-Section "显卡信息"
$gpu = Get-CimInstance Win32_VideoController | Select-Object `
Name, DriverVersion, `
@{n='AdapterRAM(GB)';e={ if ($_.AdapterRAM) { [math]::Round($_.AdapterRAM/1GB,2) } else { $null } }}
Append-Table $gpu
# ---------- 主板 ----------
Write-Section "主板信息"
$board = Get-CimInstance Win32_BaseBoard | Select-Object `
Manufacturer, Product, SerialNumber
Append-Table $board
# ---------- 操作系统 ----------
Write-Section "操作系统"
$os = Get-CimInstance Win32_OperatingSystem | Select-Object `
Caption, Version, OSArchitecture, `
@{n='InstallDate';e={ ([Management.ManagementDateTimeConverter]::ToDateTime($_.InstallDate)).ToString("yyyy-MM-dd HH:mm") }}
Append-Table $os
# ---------- 完成 ----------
Write-Host ""
Write-Host ("硬件信息报告已保存到:{0}" -f $OutPath) -ForegroundColor Green
"硬件信息报告已保存到:$OutPath" | Out-File -FilePath $OutPath -Append -Encoding UTF8
235

被折叠的 条评论
为什么被折叠?



