How-to: Get hardware and software information [Pshinfo.ps1]

Get information about a machine - Ram, Disk Space, Service pack, Uptime, rather like psinfo only written in PowerShell.

Param($machine)

function GetComputerInfo{
    Param($mc)
    # Ping the machine to see if it is online
    If (Test-Connection $mc -count 1 -quiet) {
# Ping Success # ComputerSystem info $CompInfo = Get-CimInstance Win32_ComputerSystem -comp $mc # OS info $OSInfo = Get-CimInstance Win32_OperatingSystem -comp $mc # Serial No $BiosInfo = Get-CimInstance Win32_BIOS -comp $mc # CPU Info $CPUInfo = Get-CimInstance Win32_Processor -comp $mc # Create custom Object $myobj = "" | Select-Object Name,Domain,Model,MachineSN,OS,ServicePack,WindowsSN,Uptime,RAM,Disk $myobj.Name = $CompInfo.Name $myobj.Domain = $CompInfo.Domain $myobj.Model = $CompInfo.Model $myobj.MachineSN = $BiosInfo.SerialNumber $myobj.OS = $OSInfo.Caption $myobj.ServicePack = $OSInfo.servicepackmajorversion $myobj.WindowsSN = $OSInfo.SerialNumber $myobj.uptime = (Get-Date) - [System.DateTime]::ParseExact($OSInfo.LastBootUpTime.Split(".")[0],'yyyyMMddHHmmss',$null) $myobj.uptime = "$($myobj.uptime.Days) days, $($myobj.uptime.Hours) hours," +` " $($myobj.uptime.Minutes) minutes, $($myobj.uptime.Seconds) seconds" $myobj.RAM = "{0:n2} GB" -f ($CompInfo.TotalPhysicalMemory/1gb) $myobj.Disk = GetDriveInfo $mc #Return Custom Object" $myobj } else { # Ping Failed! Write-Host "Error: $mc not Pingable" -fore RED } } function GetDriveInfo{ Param($comp) # Get disk sizes $logicalDisk = Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $comp foreach($disk in $logicalDisk) { $diskObj = "" | Select-Object Disk,Size,FreeSpace $diskObj.Disk = $disk.DeviceID $diskObj.Size = "{0:n0} GB" -f (($disk | Measure-Object -Property Size -Sum).sum/1gb) $diskObj.FreeSpace = "{0:n0} GB" -f (($disk | Measure-Object -Property FreeSpace -Sum).sum/1gb) $text = "{0} {1} Free: {2}" -f $diskObj.Disk,$diskObj.size,$diskObj.Freespace $msg += $text + [char]13 + [char]10 } $msg } # Main - run all the functions GetComputerInfo ($machine)

Example

Assuming the script above is saved in the current directory as pshinfo.ps1, run it passing a computername:

PS C:\> ./pshinfo FileServer02

This script is based on an original by Brandon Shell [MVP]

“In any collection of data, the figure most obviously correct, beyond all need of checking, is the mistake” ~ Finagle's third law.

Related PowerShell Cmdlets:

psinfo - List information about a system.


 
Copyright © 1999-2021 SS64.com
Some rights reserved