Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

PSCustomObject

published: February 12, 2019 author: Tinu tags: PowerShell categories: PowerShell-Basic


Table of Contents

Loop through a collection

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

Format a hashtable

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

← Previous Post [ Top ] Copyright © 2024 by tinuwalther [ Blog ] Next Post →