Overview
This PowerShell script is designed to provide a quick overview of some crucial system information about a Windows machine. It gathers details about the processor, memory, and operating system. The output is formatted into a custom object for easy reading or further processing. This can be particularly useful for system administrators and IT professionals who need to audit or report on the hardware and software specifications of the systems they manage.
Explanation with Inline Comments
# Retrieve processor details using CIM (Common Information Model)
$Processor = Get-CimInstance -ClassName Win32_Processor |
Select-Object -Property Name, NumberOfCores, NumberOfLogicalProcessors
# Get the total physical memory, converting the value from bytes to gigabytes and rounding it to the nearest whole number
$Memory = Get-CimInstance -ClassName Win32_ComputerSystem |
ForEach-Object { [Math]::Round($_.TotalPhysicalMemory / 1GB) }
# Fetch operating system details including the name, version, and architecture
$OS = Get-CimInstance -ClassName Win32_OperatingSystem |
Select-Object -Property Caption, Version, OSArchitecture
# Create a custom object to neatly format the fetched data
[PSCustomObject]@{
ProcessorName = $Processor.Name # Name of the processor
NumberOfCores = $Processor.NumberOfCores # Total number of physical cores
NumberOfLogicalProcessors = $Processor.NumberOfLogicalProcessors # Total number of logical processors
TotalPhysicalMemory_GB = $Memory # Total physical memory in gigabytes
OSCaption = $OS.Caption # Operating system name
OSVersion = $OS.Version # Operating system version
OSArchitecture = $OS.OSArchitecture # Architecture of the OS (e.g., 64-bit)
}
Retrieving Processor Details
The processor (CPU) is the brain of your computer, responsible for executing instructions and performing calculations. Using PowerShell’s Common Information Model (CIM), we can easily retrieve key information about the processor.
# Retrieve processor details using CIM (Common Information Model)
$Processor = Get-CimInstance -ClassName Win32_Processor |
Select-Object -Property Name, NumberOfCores, NumberOfLogicalProcessors
In this snippet, we’re querying the system for the processor’s name, number of cores, and number of logical processors. This provides insights into the processing power available on your system.
Determining Total Physical Memory
Memory (RAM) is another critical component that directly impacts your computer’s performance. PowerShell enables us to fetch the total physical memory installed on the system and convert it into a more human-readable format.
# Get the total physical memory, converting the value from bytes to gigabytes and rounding it to the nearest whole number
$Memory = Get-CimInstance -ClassName Win32_ComputerSystem |
ForEach-Object { [Math]::Round($_.TotalPhysicalMemory / 1GB) }
Here, we’re converting the total physical memory from bytes to gigabytes and rounding it to the nearest whole number for clarity.
Fetching Operating System Details
Understanding the operating system environment is crucial for compatibility and software development. PowerShell allows us to retrieve essential information about the OS, including its name, version, and architecture.
# Fetch operating system details including the name, version, and architecture
$OS = Get-CimInstance -ClassName Win32_OperatingSystem |
Select-Object -Property Caption, Version, OSArchitecture
By querying the Win32_OperatingSystem class, we can gather details such as the OS name, version, and architecture (e.g., 64-bit).
Creating a Neatly Formatted Output
To present the retrieved information in a structured and readable manner, we can create a custom object that combines all the collected data.
# Create a custom object to neatly format the fetched data
[PSCustomObject]@{
ProcessorName = $Processor.Name # Name of the processor
NumberOfCores = $Processor.NumberOfCores # Total number of physical cores
NumberOfLogicalProcessors = $Processor.NumberOfLogicalProcessors # Total number of logical processors
TotalPhysicalMemory_GB = $Memory # Total physical memory in gigabytes
OSCaption = $OS.Caption # Operating system name
OSVersion = $OS.Version # Operating system version
OSArchitecture = $OS.OSArchitecture # Architecture of the OS (e.g., 64-bit)
}
This custom object neatly organizes the processor details, memory information, and OS characteristics into a single output.