OpsaC - Operating as PowerShell code
published: February 12, 2019 author: Tinu tags: PowerShell categories: PowerShell-Basic
Loop through a collection and create a PSCustomObject.
$collection = Get-CimInstance -Class Win32_Product
$resultset = foreach($item in $collection){
# Add names and values to an PSCustomObject
[PSCustomObject]@{
InstallDate = $item.InstallDate
Vendor = $item.Vendor
Name = $item.Name
#PackageName = $item.PackageName
#Description = $item.Description
Version = $item.Version
}
}
$resultset | Sort-Object Name | Format-Table -AutoSize
Output:
InstallDate Vendor Name Version
----------- ------ ---- -------
20180822 Adobe Systems Incorporated Adobe Acrobat Reader DC MUI 18.009.20044
20180918 Microsoft Corporation PowerShell 6-x64 6.1.0.0
Formats a hashtable to a PSCustomObject.
$hash = [ordered]@{}
$hash.Nickname = 'Tinu'
$hash.Surname = 'Walther'
$hash.Address = 'Switzerland'
$hash.EMail = 'it@martin-walther.ch'
[PSCustomObject]$hash
Output:
Nickname Surname Address EMail
-------- ------- ------- -----
Tinu Walther Switzerland it@martin-walther.ch