WMI and Clean Code

I couldn’t extract all of your code because of the bad formatting the forum created but since you’ve asked for minimal code I think there’s some room for improvement on part of your code. :wink:

$CimSession = New-CimSession -ComputerName $ComputerName

$cs = Get-CimInstance -ClassName win32_computersystem -CimSession $CimSession -ErrorAction Stop
$os = Get-CimInstance -ClassName Win32_operatingsystem -CimSession $CimSession -ErrorAction Stop
$bs = Get-CimInstance -ClassName win32_bios -CimSession $CimSession  -ErrorAction Stop
$powerplan = Get-CimInstance -Namespace root\cimv2\power -Class win32_PowerPlan -CimSession $CimSession  | Where-Object { $_.IsActive } | Select-Object -ExpandProperty ElementName -ErrorAction Stop

[PSCustomObject]@{
    Name            = $cs.Name
    System          = $cs.AdminPasswordStatus
    Serial          = $bs.SerialNumber
    OperatingSystem = $os.Caption
    LastBootUpTime  = $os.LastBootUpTime
    DomainConnected = $cs.PartOfDomain
    OSArchitecture  = $os.OSArchitecture
    PowerPlan       = $powerplan
}

Try if you can use some of it.