# Get logged-in user
$loggedInUser = (Get-CimInstance Win32_ComputerSystem).UserName
$username = $loggedInUser.Split("\")[-1]
# Output file
$OutputFile = "C:\Users\$username\Downloads\PC_Specs_Report-OJT-3.csv"
# ------------------------------------------------------------
# Function: Decode Windows Product Key
# ------------------------------------------------------------
function Get-WindowsProductKey {
try {
$keyPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$digitalProductId = (Get-ItemProperty -Path $keyPath -Name DigitalProductId -ErrorAction SilentlyContinue).DigitalProductId
if (-not $digitalProductId) { return "Not Available" }
$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,"-")
}
catch {
return "Not Available"
}
}
# ------------------------------------------------------------
# Function: Get Corporate Email (Primary Azure AD UPN)
# ------------------------------------------------------------
function Get-CorporateEmail {
$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"
}
# ------------------------------------------------------------
# Collect System Info
# ------------------------------------------------------------
$computer = Get-CimInstance Win32_ComputerSystem
$bios = Get-CimInstance Win32_BIOS
$baseboard = Get-CimInstance Win32_BaseBoard
$cpuInfo = Get-CimInstance Win32_Processor
$osInfo = Get-CimInstance Win32_OperatingSystem
# Detect device type (2 = Laptop)
$isLaptop = $computer.PCSystemType -eq 2
# Assign correct Brand/Model/Serial
if ($isLaptop) {
$brand = $computer.Manufacturer
$model = $computer.Model
$serial = $bios.SerialNumber
$deviceType = "Laptop"
} else {
$brand = $baseboard.Manufacturer
$model = $baseboard.Product
$serial = $baseboard.SerialNumber
$deviceType = "Desktop"
}
# RAM (Installed)
$ramBytes = (Get-CimInstance Win32_PhysicalMemory |
Measure-Object -Property Capacity -Sum).Sum
$ram = [math]::Round($ramBytes / 1GB, 0)
# Physical Storage (true hardware size)
$diskBytes = (Get-CimInstance Win32_DiskDrive |
Measure-Object -Property Size -Sum).Sum
$storage = [math]::Round($diskBytes / 1000000000, 0)
# Other Info
$hostname = $env:COMPUTERNAME
$corporateEmail = Get-CorporateEmail
$productKey = Get-WindowsProductKey
# ------------------------------------------------------------
# EXACT 11 Columns (Order Matters)
# ------------------------------------------------------------
$result = [PSCustomObject]@{
CorporateEmail = $corporateEmail
Hostname = $hostname
DeviceType = $deviceType
Brand = $brand
Model = $model
SerialNumber = $serial
CPU = $cpuInfo.Name
OS_Windows = $osInfo.Caption
RAM_GB = "$ram GB"
Storage_GB = "$storage GB"
ProductKey = $productKey
}
# Export CSV
$result | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
Write-Host "PC Spec report saved to: $OutputFile"0 views