PowerShell – Monitor CPU Cores Temperatures

In order to Monitor the Temperature of your CPU Cores it is best to make use of OpenHardwareMonitorLib

 

image

You can download it here.

This nice application has a .Net Library that you can use to access the Hardware Sensors Data

It also exposes the values to WMI when the GUI is started, but that is not so convenient for monitoring

SOLUTION :

# Needs admin privileges and the .NET OpenHardwareMonitorLib.dll

#Requires -RunAsAdministrator

CLS

Add-Type -Path "C:\OpenHardwareMonitor\OpenHardwareMonitorLib.dll"

$Comp = New-Object -TypeName OpenHardwareMonitor.Hardware.Computer

$Comp.Open()

$Comp.CPUEnabled = $true

$Comp.RAMEnabled = $true

$Comp.MainboardEnabled = $true

$Comp.FanControllerEnabled = $true

$Comp.GPUEnabled = $true

$Comp.HDDEnabled = $true

ForEach ($HW in $Comp.Hardware) {
    $hw.HardwareType.ToString() + ' - ' + $hw.name.ToString()

    If ( $hw.HardwareType -eq "CPU"){
        ForEach ($Sensor in $HW.Sensors) {

        If ($Sensor.SensorType -eq "Temperature"){
            
            $Sensor.Name + ' - Temp : ' + $Sensor.Value.ToString() + ' C - Min. : ' + $Sensor.Min.ToString() + ' C - Max : ' + $Sensor.Max.ToString() + ' C'
        }
      }
    }
   
    # $hw.Sensors
    $hw.SubHardware
}
$Comp.Close()

image

Apparently there has been a fork on this marvelous Library called LibreHardwareMonitor.

See here 

image

Someone asked me to update the code to this more recent maintained Library using PS.

So here you go :


image

If you add an Email Notifications when it reaches the MAX values, you have a nice Monitoring System Smile

Enjoy !

8 Responses to PowerShell – Monitor CPU Cores Temperatures

  1. Any chances to update the script to work with PS7 and Libre Hardware Monitor? (An up to date fork of Open Hardware Monitor)

  2. rx580soul says:

    Execute the script running Windows PowerShell ISE (x86).

  3. Your solution is also fine,

    Thanks

  4. Peter says:

    This worked for me:

    #Requires -RunAsAdministrator

    cls
    $dll = “$env:windir\system32\OpenHardwareMonitorLib.dll”
    if (!(Test-Path $dll)) {
    # download the package:
    $web = [System.Net.WebClient]::new()
    $url = “https://openhardwaremonitor.org/files/openhardwaremonitor-v0.9.5.zip”
    $zip = $web.DownloadData($url)
    if ($zip.Count -ne 503320) {break}

    # 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(‘OpenHardwareMonitor/OpenHardwareMonitorLib.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 ‘67789487630645be12a67223d16cf6c6’) {break}

    # save the dll:
    [System.IO.File]::WriteAllBytes($dll, $bytes)
    Unblock-File -LiteralPath $dll
    }

    Add-Type -LiteralPath $dll
    $monitor = [OpenHardwareMonitor.Hardware.Computer]::new()
    $monitor.CPUEnabled = $true
    $monitor.Open()
    $cpuPackage = foreach ($sensor in $monitor.Hardware.Sensors) {
    if ($sensor.SensorType -eq ‘Temperature’ -and $sensor.Name -eq ‘CPU Package’){$sensor}
    }
    $monitor.Close()
    write-host “CPU-Package Temperature = $($cpuPackage.Value)°C”

  5. Hi Robb,

    You problem is the following :

    The error “Could not load file or assembly” (0x80131515) is a catch-all error. For instance, it is also reported when the exe or dll was downloaded from an unsafe zone.

    Solution :
    https://thirtysix.zendesk.com/hc/en-us/articles/202921675-How-to-Unblock-a-File-Downloaded-from-an-Email-or-the-Internet

    This is fixed by right-clicking the assembly file and choosing “Unlock” from the General tab.

    Hope this will fix the error.

    Enjoy !

  6. Hi Robb,

    I tested the script on different servers windows 2008 to 2019 and all work fine on my side.

    Make sure that you have the GUI OpenHardwareMonitor.exe running when you run the PowerShell script !!

    This is a prerequisite for getting data back in PS.

  7. Robb says:

    I’ve followed your step and get the error below:

    Add-Type : Could not load file or assembly ‘file:///C:\Users\%user%\Downloads\openhardwaremonitor-v0.9.5\OpenHardwareMonitor\OpenHardwareMonitorLib.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
    At line:7 char:1

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.