# Is my code a Cleaner code?

**URL:** <https://forums.powershell.org/t/is-my-code-a-cleaner-code/15626>\
**Category:** PowerShell Help\
**Created:** [February 11, 2021, 5:24pm UTC](https://forums.powershell.org/t/is-my-code-a-cleaner-code/15626 "2021-02-11T17:24:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shane-taubman](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@shane-taubman](https://forums.powershell.org/u/shane-taubman)\
**Post date:** [February 11, 2021, 5:24pm UTC](https://forums.powershell.org/t/is-my-code-a-cleaner-code/15626/1 "2021-02-11T17:24:58Z")

</div>

Hi Guys,

I’m looking at the best way to display WMI information which will be in a HTML report but which In I have the minimal code required. See Below what I have. I’m pulling data from 4 WMI objects. Thanks

```auto
Write-Output “*Collecting System Information”

try
{

#Get WMI Objects using CIM

$cs = Get-CimInstance -ClassName win32_computersystem -ComputerName $ComputerName -ErrorAction Stop
$os = Get-CimInstance -ClassName Win32_operatingsystem -ComputerName $ComputerName -ErrorAction Stop
$bs = Get-CimInstance -ClassName win32_bios -ComputerName $ComputerName -ErrorAction Stop
$power = Get-CimInstance -Namespace root\cimv2\power -Class win32_PowerPlan | Where-Object {$_.IsActive -eq “True”}|Select ElementName -ErrorAction Stop
$powerplan = if ($OSPower.ElementName -notlike “*Performance”) {"$($OSPower.ElementName)(Recommended Plan: High Performance)"}
else {$OSPower.ElementName}

$csinfo = @()
$csinfoObj = New-Object PSObject

$csinfoObj | Add-Member -Name ‘Name’ -MemberType NoteProperty -Value $cs.Name
$csinfoObj | Add-Member -Name ‘System Uptime’ -MemberType NoteProperty -Value $cs.AdminPasswordStatus
$csinfoObj | Add-Member -Name ‘Serial Number’ -MemberType NoteProperty -Value $bs.SerialNumber
$csinfoObj | Add-Member -Name ‘Operating System’ -MemberType NoteProperty -Value $os.Caption
$csinfoObj | Add-Member -Name ‘Last Boot’ -MemberType NoteProperty -Value $os.LastBootUpTime
$csinfoObj | Add-Member -Name ‘Domain Connected’ -MemberType NoteProperty -Value $cs.PartOfDomain
$csinfoObj | Add-Member -Name ‘OS Architecture’ -MemberType NoteProperty -Value $os.OSArchitecture
$csinfoObj | Add-Member -Name ‘Power Plan’ -MemberType NoteProperty -Value $powerplan

$csinfo += $csinfoObj

$SystemInfoHTML += $csinfo | ConvertTo-Html -Fragment -As List -PreContent “

System Information
” | ForEach {$_ -replace “”,"<table class=“list”>"}
}
catch
{
Write-Warning $.Exception.Message
$htmlbody += "<p>An error was encountered. $($.Exception.Message)</p>"
$htmlbody += $spacer
}

```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [February 21, 2021, 4:56pm UTC](https://forums.powershell.org/t/is-my-code-a-cleaner-code/15626/3 "2021-02-21T16:56:04Z")

</div>

IMO, as a standalone script. This looks okay.

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [February 25, 2021, 8:43pm UTC](https://forums.powershell.org/t/is-my-code-a-cleaner-code/15626/4 "2021-02-25T20:43:25Z")

</div>

Agree code looks ok, but I have a question and maybe a couple suggestions.

1. It looks like you are referencing a variable $OSPower but I never see it defined. I see an $os and a $power defined but no $OSPower.
2. When you define $power you don’t pass a -computername argument which means it will return data from the current host. Is this intentional?
3. If you are always passing -computername $ComputerName and -ErrorAction Stop, you might want to consider using splatting to make it more concise.
4. What version of PowerShell are you running? If 3.1 or later, I’d recommend using the [pscustomobject] type accelerator with a hashtable to create your custom object. It just looks a little cleaner.

---

<div class="post-metadata">

**Author:** ![RobinDadswell](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/robindadswell/32/1024_2.png) [@RobinDadswell](https://forums.powershell.org/u/RobinDadswell)\
**Post date:** [February 26, 2021, 12:08pm UTC](https://forums.powershell.org/t/is-my-code-a-cleaner-code/15626/5 "2021-02-26T12:08:27Z")

</div>

On point 4 it also seems to have a performance increase from what I have seen running some very large scripts in both ways!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/is-my-code-a-cleaner-code/15626/6 "2024-05-16T20:27:48Z")

</div>

Closed on May 16, 2024, 8:27pm UTC


