Get CPU Serial Number (Windows)
Description
The get_cpu_serial_number_windows
function retrieves the CPU serial number on a Windows system using the wmi
library. If an error occurs, it returns the exception message.
Function Signature:
def get_cpu_serial_number_windows() -> str:
Parameters
This function does not take any parameters.
Returns
- str: The CPU serial number if successful. If an error occurs, it returns the error message as a string.
Example Usage
Here’s an example of how the function works:
serial_number = get_cpu_serial_number_windows()
print(serial_number)
Example Output:
F1D12D8E9E1A5F23B9F3E9F4F101E16C
Code
def get_cpu_serial_number_windows() -> str:
try:
import wmi
wmi_object = wmi.WMI()
for processor in wmi_object.Win32_Processor():
return processor.ProcessorId.strip()
except Exception as ex:
return str(ex)
Errors and Exceptions
- If an error occurs while trying to fetch the CPU serial number, the exception message will be returned.
Notes
This function requires the wmi
library, which can be installed via pip install wmi
. It retrieves the processor's unique identifier on Windows systems.