Elaborating on @mklement0's answer --
In my experience, PowerShell works best when you can defer formatting until the very end -- i.e. all processing should be done on objects, and then the final task is rendering strings.
Starting at the most basic script:
Get-ComputerInfo -Property $SYSLST1
OsName OsVersion TimeZone CsName
------ --------- -------- ------
Microsoft Windows Server 2019 Standard 10.0.17763 (UTC-08:00) Pacific Time (US & Canada) WIN-IJ0UD3SK4HR
The result is a single System.Management.Automation.PSCustomObject
and by default, the object is run through Format-Table
A cool trick of PSCustomObject
is that you can iterate over its properties:
$(Get-ComputerInfo -Property $SYSLST1).PSObject.Properties |
ForEach-Object { " * $($_.Name) : $($_.Value))" }
* OsName : Microsoft Windows Server 2019 Standard)
* OsVersion : 10.0.17763)
* TimeZone : (UTC-08:00) Pacific Time (US & Canada))
* CsName : WIN-IJ0UD3SK4HR)
This gives us full control over the string rendering and allows us to make tweaks to it without fear of breaking other parts of the script.