在CMD/Power Shell中获取CPU温度

20

在我的电脑上,我正在尝试获取CPU温度。在StackOverflow上搜索,我找到了这个:

C:\WINDOWS\system32>wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature

但是我收到了这个错误:

Node - ADMIN
ERROR:
Description = Not supported

3
“不支持” - 这台机器上没有其他任何东西能够运行。 BIOS 简单地不支持读取它。 - Christian.K
7个回答

24
你可以使用这段代码:
function Get-Temperature {
    $t = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
    $returntemp = @()

    foreach ($temp in $t.CurrentTemperature)
    {


    $currentTempKelvin = $temp / 10
    $currentTempCelsius = $currentTempKelvin - 273.15

    $currentTempFahrenheit = (9/5) * $currentTempCelsius + 32

    $returntemp += $currentTempCelsius.ToString() + " C : " + $currentTempFahrenheit.ToString() + " F : " + $currentTempKelvin + "K"  
    }
    return $returntemp
}

Get-Temperature

12
这不是直接的CPU温度,而是在主板上的某个地方:https://dev59.com/v1_Va4cB1Zd3GeqPYfA8#17083409 - Tilo
2
更新至新版本:Get-CimInstance MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" - Efren

6

管理员身份在命令提示符中运行以下命令:

wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature

这将为您提供如下输出:

CurrentTemperature 3000 3010

但请确保您正在以管理员身份运行cmd。


8
有没有管理员都没有影响。显然,我的Windows系统不支持该查询。 - Jari Turkia
在我的电脑上 - CurrentTemperature 3000 3010它是多少摄氏度? - MILAN SAHANA
1
@MILANSAHANA 这是以开尔文为单位的第10级,因此3010 = 301.0 K = 27.85℃ - Matthieu
这个方法是有效的,但它并不是指CPU温度。结果以C*10的单位给出,以避免传感器产生浮点数。因此,在这种情况下,您需要将传感器值除以10,并将其用于C = K - 273.15中的K。 - not2qubit

5

你可以使用 Open Hardware Monitor,这是一款开源软件(根据 MPL v2 许可证)。你可以在此处访问命令行版本:

OpenHardwareMonitorReport.zip

以下是输出结果的示例部分:

PS C:\Users\myuser\OpenHardwareMonitorReport> .\OpenHardwareMonitorReport.exe

Open Hardware Monitor Report

--------------------------------------------------------------------------------

Version: 0.8.0.2

--------------------------------------------------------------------------------

Common Language Runtime: 4.0.30319.42000
Operating System: Microsoft Windows NT 6.2.9200.0
Process Type: 32-Bit

--------------------------------------------------------------------------------

Sensors

|
+- HP 00F52W (/mainboard)
|
+- Intel Core i7-3770 (/intelcpu/0)
|  +- Bus Speed      :  99.7734  99.7734  99.7784 (/intelcpu/0/clock/0)
|  +- CPU Core #1    :  3691.62  3691.62  3791.58 (/intelcpu/0/clock/1)
|  +- CPU Core #2    :  3691.62  3691.62  3791.58 (/intelcpu/0/clock/2)
|  +- CPU Core #3    :  3791.39  3791.39  3891.36 (/intelcpu/0/clock/3)
|  +- CPU Core #4    :  3691.62  3691.62  3891.36 (/intelcpu/0/clock/4)
|  +- CPU Core #1    :       42       42       43 (/intelcpu/0/temperature/0)
|  +- CPU Core #2    :       43       37       43 (/intelcpu/0/temperature/1)
|  +- CPU Core #3    :       42       35       42 (/intelcpu/0/temperature/2)
|  +- CPU Core #4    :       45       41       45 (/intelcpu/0/temperature/3)
|  +- CPU Package    :       45       43       45 (/intelcpu/0/temperature/4)

4

在我的笔记本电脑上,以上所有方法都给出了错误的结果。只有这个方法能以摄氏度显示CPU温度:

$data = Get-WMIObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
@($data)[0].HighPrecisionTemperature

我猜,每个 CPU 版本可能有不同的地方/公式来获取正确的 CPU 温度。

有趣而巧妙的变化,但不适合重复运行。查询语句很慢。并且不要期望获取CPU核心温度。 - not2qubit
谢谢这个。我没有从一些来源获取到数据,但这个可以用。我的主板显然有几个不同的传感器(包括一个返回0的?),所以我将您的查询调整为这个单行代码:$data = Get-WMIObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"; @($data)[0].HighPrecisionTemperature/10 - 273.1; @($data)[1].HighPrecisionTemperature/10 - 273.1; @($data)[2].HighPrecisionTemperature/10 - 273.1; - undefined

0

有了新的传感器,或者使用我已经有的和高度信息。 它还显示关键温度和百分比(以摄氏度为单位)。 它生成一个名为Temperatures.txt的文件,以便轻松调试,并提供序列化对象的xml文件。

function Get-Temperature {
    $TempFormat = "#"
    $TempFile = "temperature"

    $Command = 'Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" ' + " > $pwd\$TempFile.txt"
    $Command = 'Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" ' + " | Export-Clixml $pwd\$TempFile.xml"

    $p = Start-Process -Verb runas -FilePath "powershell" -ArgumentList $command -WorkingDirectory $pwd -PassThru
    $p.WaitForExit()

    $t = Import-Clixml pippo.xml

    $returntemp = @()

    foreach ($Sensor in $t)
    {
    $Active = if($sensor.Active){"On "}else{"Off"}
    $temp = $Sensor.CurrentTemperature
    $Critical = $Sensor.CriticalTripPoint

    $currentTempKelvin = $temp / 10
    $currentTempCelsius = $currentTempKelvin - 273.15
    $currentTempFahrenheit = (9/5) * $currentTempCelsius + 32

    $StrKelvin = $currentTempKelvin.ToString($TempFormat).PadLeft(3, " ")
    $StrCelsius = $currentTempCelsius.ToString($TempFormat).PadLeft(3, " ")
    $StrFahrenheit = $currentTempFahrenheit.ToString($TempFormat).PadLeft(3, " ")

    $CriticalKelvin = $Critical / 10
    $CriticalCelsius = $CriticalKelvin - 273.15
    $CriticalFahrenheit = (9/5) * $CriticalCelsius + 32

    $StrCritKelvin = $CriticalKelvin.ToString($TempFormat).PadRight(3, " ")
    $StrCritCelsius = $CriticalCelsius.ToString($TempFormat).PadRight(3, " ")
    $StrCritFahrenheit = $CriticalFahrenheit.ToString($TempFormat).PadRight(3, " ")

    $PerCrit = ($currentTempCelsius/$CriticalCelsius * 100)
    $StrPerCrit = $PerCrit.ToString($TempFormat).PadLeft(3, " ")

    $returntemp += "$Active $StrPerCrit% $StrCelsius/$StrCritCelsius C : $StrFahrenheit/$StrCritFahrenheit  F : $StrKelvin/$StrCritKelvin K - " + $Sensor.InstanceName 
    }
    return $returntemp
}

Get-Temperature

3
导入-Clixml pippo.xml,pippo.xml文件在哪里创建? - David Johnson

0
获取CPU温度的最稳定可靠的方法是使用LibreHardwareMonitor项目中的DLL(该项目取代了OpenHardwareMonitor)。
以下是Powershell中的代码片段:
# script to get the current CPU temperature, needs to run as admin/system
# requires an external DLL from the GitHub-Project "LibreHardwareMonitor"
# on first execution the script downloads the DLL from the gitHub-Project
# carsten.giese@googlemail.com


#Requires -RunAsAdministrator

cls
$dll = "$env:windir\system32\LibreHardwareMonitorLib.dll"
$storeDll = $true

if (!(Test-Path $dll)) {
    $web = [System.Net.WebClient]::new()

    # download the package:
    $url = "https://github.com/LibreHardwareMonitor/LibreHardwareMonitor/releases/download/v0.9.2/LibreHardwareMonitor-net472.zip"
    $zip = $web.DownloadData($url)

    # extract the dll:
    Add-Type -AssemblyName System.IO.Compression
    $stream = [System.IO.Memorystream]::new()
    $stream.Write($zip,0,$zip.Length)
    $archive = [System.IO.Compression.ZipArchive]::new($stream)
    $entry = $archive.GetEntry('LibreHardwareMonitorLib.dll')
    $bytes = [byte[]]::new($entry.Length)
    [void]$entry.Open().Read($bytes, 0, $bytes.Length)

    # check MD5:
    $md5  = [Security.Cryptography.MD5CryptoServiceProvider]::new().ComputeHash($bytes)
    $hash = [string]::Concat($md5.foreach{$_.ToString("x2")})
    if ($hash -ne 'd4a02aa7bd43a5c5ffd3d673bb20b54d') {break}

    # save the dll:
    if ($storeDll) {
        [System.IO.File]::WriteAllBytes($dll, $bytes)
        Unblock-File -LiteralPath $dll
    }
} else {
    $bytes = [System.IO.File]::ReadAllBytes($dll)
    if (!$storeDll) {Remove-Item $dll -Force}
}

Add-Type -LiteralPath $dll
$monitor = [LibreHardwareMonitor.Hardware.Computer]::new()
$monitor.IsCPUEnabled = $true
$monitor.Open()
foreach ($sensor in $monitor.Hardware.Sensors) {
    if ($sensor.SensorType -eq 'Temperature' -and $sensor.Name -eq 'CPU Package'){
        $temp = $sensor.Value
        break
    }
}
$monitor.Close()
write-host "CPU-Package Temperature = $temp°C" -f y 

-1
根据这个问题的答案

要获取CPU(和每个核心)的确切温度,您需要编写内核驱动程序,这更加复杂。

CurrentTemperature返回位于主板某个热区的温度。

这就解释了为什么此页面上的某些答案会返回一个温度,但它与实际的CPU温度相差甚远。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接