Skip to content

Instantly share code, notes, and snippets.

@jschlackman
Last active October 13, 2018 23:25
Show Gist options
  • Save jschlackman/b37f9ebc2bddb1b43242d2cec7a65a26 to your computer and use it in GitHub Desktop.
Save jschlackman/b37f9ebc2bddb1b43242d2cec7a65a26 to your computer and use it in GitHub Desktop.
Creates a basic hardware inventory of the current computer from WMI and emails it as a formatted HTML report. I typically use this to create a specification summary for old laptops that have been removed from production use and that are being prepared for resale.
# Name: Send-Inventory.ps1
# Author: James Schlackman
# Last Modified: Oct 13 2018
#
# Creates a basic hardware inventory of the current computer from WMI and emails it as a formatted HTML report.
# Configure your mail relay and destination email address here
$MailRelay = "smtp.contoso.com"
$FromAddress = "youraccount@contoso.com"
# Get computer details from WMI
$computerSystem = Get-WmiObject Win32_ComputerSystem
$computerBIOS = Get-WmiObject Win32_BIOS
$computerOS = Get-WmiObject Win32_OperatingSystem
$computerCPU = Get-WmiObject Win32_Processor
$computerHDD = Get-WmiObject Win32_DiskDrive -Filter "DeviceID like '%PHYSICALDRIVE0'" # Only the primary drive will be inventoried
# Report title used at top of report and email subject
$reportTitle = "System Report for $($computerSystem.Name)"
# Define basic CSS formatting
$reportHeader = @"
<style>body {font-family: Calibri, sans-serif; font-size: 14pt} tr td:first-child {font-weight: bold; padding-right: 1em}</style>
"@
# Build report object from WMI info
$computerInfo = $computersystem | Select-Object -Property `
@{Name='Manufacturer'; Expression={$computerSystem.Manufacturer}},`
@{Name='Serial Number'; Expression={$computerBIOS.SerialNumber}},`
@{Name='CPU'; Expression={$computerCPU.Name}},`
@{Name='HDD Model'; Expression={$computerHDD.Model}},`
@{Name='HDD Capacity'; Expression={"{0:N0}" -f ($computerHDD.Size/1000000000) + "GB"}},`
@{Name='RAM'; Expression={"{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"}},`
@{Name='Operating System'; Expression={$computerOS.caption}}
# Write report data to console
$computerInfo | Format-List
# Format report as HTML table with CSS formatting
$reportBody = $computerInfo | ConvertTo-Html -As List -Title $reportTitle -PreContent "<h1>$reportTitle</h1>" -Head $reportHeader | Out-String
# Uncomment this if you want a copy of the report saved to the current path
# $reportBody | Out-File -FilePath "$($computerSystem.Name).html"
# Send the report in the body of an email message
Send-MailMessage -SmtpServer $MailRelay -Subject $reportTitle -From $FromAddress -BodyAsHtml $reportBody -To $FromAddress
@jschlackman
Copy link
Author

This script deliberately uses the (technically deprecated) Get-WmiObject command so it can be used on a default Windows 7 SP1 install without having to install a later version of PowerShell. If you really want to use the recommended CIM commands instead, replace lines 13-17 with the following:

$computerSystem = Get-CimInstance CIM_ComputerSystem
$computerBIOS = Get-CimInstance CIM_BIOSElement
$computerOS = Get-CimInstance CIM_OperatingSystem
$computerCPU = Get-CimInstance CIM_Processor
$computerHDD = Get-CimInstance CIM_DiskDrive -Filter "DeviceID like '%PHYSICALDRIVE0'"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment