You know what’s LOTS of fun when you’re playing a game on your computer?
Did you guess “Emergency Shut Down Due To Overheating?”
That would not have been my first guess, either, but it did give me a good idea for a blog post. So there’s that.
I’ve dual booted between Windows and Linux for a long time, and one thing I loved on Ubuntu that was missing from Windows was seeing the CPU temperature on my terminal window (check out Byobu).
Normally that’s just a nifty fact, but once your computer begins to spontaneously shut off, you suddenly become much, much more interested in how hot it really is.
That’s why I began searching the internets for a way to figure out the temp under Windows.
Note: The code I’m about to share may well not work on your computer. I’m a programmer, so the best I can ever say is, “It works on my machine.” Seriously, though it will only work if your BIOS supports reporting the temperature. And, you know, your CPU has a thermometer.
Also Note: I’m using CPU lightly here. This code has no idea where the manufacturer put the temp sensor.
Long story short, the secret lies in Windows Management Instrumentation (WMI).
WMI is a Microsoft initiative to implement a blah blah, Oh, my God, just go read the link.
You can call WMI from .Net, but for you folks who don’t have or want Visual Studio, you can also call it from PowerShell.
Since I want to use this multiple ways, I’m going to put it in a function.
PowerShell likes things to be named with Verb - Noun format, let’s call our function
Following their own convention, they named the commandlet we want
We’ll start off by creating an object we can reference that accesses the WMI Object we need:
$t = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace “root/wmi”
As you can see, we are just passing into the commandlet the object and namespace where it can find it.
Now we can ask for the current temperature.
No can do.
NOTE: You must run as administrator to access this command.
Right click PowerShell in task bar, Run As Administrator, etc.
Now let’s just run that again and… well there’s the problem!
My CPU is 3,442 degrees! No wonder it keeps shutting down.
Hang on, the Sun is only about 5,500 degrees on the surface. I have trouble believing my notebook is that hot.
Google, Google, Google.
Ah, the results are in 10ths of Kelvins.
That means the temperature is actually 344.2 degrees Kelvin.
OK, zero Kelvin is absolute zero, so this is 344.2 degrees higher than that.
Nope. I don’t normally think in Kelvin (although he was my favorite chipmunk).
Let’s convert Kelvin to Celsius.
$currentTempKelvin = $t.CurrentTemperature / 10
$currentTempCelsius = $currentTempKelvin - 273.15
Once we have that, we’ll go Farenheit.
$currentTempCelsius = (9/5) * $currentTempCelsius + 32
Now for the whole function:
Now if we just save that in a file in a subdirectory of our PowerShells Modules directory, we should be able to call it from PowerShell.
Should be something like:
Now any time we start PowerShell as administrator, we will be able to call
get-temperature and see something like
72.05 C : 161.69 F : 345.2K
Just for the record, I do actually know that Alvin was the Chipmunk, not Kelvin.
Hey! Now that you can get PowerShell to tell you the temperature, why not teach it to speak?