$loggedInUser = (Get-CimInstance Win32_ComputerSystem).UserName
$username = $loggedInUser.Split("\")[-1]
$OutputFile = "C:\Users\$username\Downloads\PC_Specs_Report.csv"
# Create array
$results = @()
# Function to decode Windows Product Key
function Get-WindowsProductKey {
$keyPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$digitalProductId = (Get-ItemProperty -Path $keyPath -Name DigitalProductId).DigitalProductId
$chars = "BCDFGHJKMPQRTVWXY2346789"
$hexPid = $digitalProductId[52..67]
$result = ""
for ($i = 24; $i -ge 0; $i--) {
$current = 0
for ($j = 14; $j -ge 0; $j--) {
$current = ($current * 256) -bxor $hexPid[$j]
$hexPid[$j] = [math]::Floor($current / 24)
$current = $current % 24
}
$result = $chars[$current] + $result
}
return $result.Insert(5, "-").Insert(11, "-").Insert(17, "-").Insert(23, "-")
}
# Function: Get Primary User UPN (Corporate Email)
function Get-PrimaryUserUPN {
$path = "HKLM:\SOFTWARE\Microsoft\Enrollments"
$enrollments = Get-ChildItem $path -ErrorAction SilentlyContinue
foreach ($item in $enrollments) {
$upn = (Get-ItemProperty -Path $item.PSPath -ErrorAction SilentlyContinue).UPN
if ($upn) { return $upn }
}
return "Not Available"
}
# Hostname
$hostname = $env:COMPUTERNAME
# Brand + Model
$computer = Get-WmiObject -Class Win32_ComputerSystem
$brand = $computer.Manufacturer
$model = $computer.Model
# Serial Number
$serial = (Get-WmiObject -Class Win32_BIOS).SerialNumber
# CPU
$cpu = (Get-WmiObject -Class Win32_Processor).Name
# RAM (GB)
$ram = [math]::Round($computer.TotalPhysicalMemory / 1GB, 2)
# OS Version
$os = (Get-WmiObject -Class Win32_OperatingSystem).Caption
# Storage (GB)
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3"
$storageTotal = [math]::Round(($disk.Size | Measure-Object -Sum).Sum / 1GB, 2)
# Corporate Email (UPN)
$corporateEmail = Get-PrimaryUserUPN
# Add to results (ORDER MATTERS)
$results += [PSCustomObject]@{
CorporateEmail = $corporateEmail
Hostname = $hostname
Brand = $brand
Model = $model
SerialNumber = $serial
CPU = $cpu
OS_Windows = $os
Storage_GB = $storageTotal
RAM_GB = $ram
}
# Export to CSV
$results | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
Write-Host "PC Spec report saved to: $OutputFile"