# WMI and Clean Code

**URL:** <https://forums.powershell.org/t/wmi-and-clean-code/15627>\
**Category:** PowerShell Help\
**Created:** [February 11, 2021, 5:29pm UTC](https://forums.powershell.org/t/wmi-and-clean-code/15627 "2021-02-11T17:29:11Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 11, 2021, 8:34pm UTC](https://forums.powershell.org/t/wmi-and-clean-code/15627/2 "2021-02-11T20:34:41Z")

</div>

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. 😉

```
$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.

---

_[View the full topic](https://forums.powershell.org/t/wmi-and-clean-code/15627)._
