Adding Additional Information to Objects

by Aug 18, 2015

When you retrieve results, you may want to add additional properties to the results so later you know where they came from.

Attaching additional information to complex objects works a little different than attaching them to primitive data (as described in an earlier tip).

Get-Process | 
  Add-Member -MemberType NoteProperty -Name PC -Value $env:COMPUTERNAME -PassThru |
  Select-Object -Property Name, Company, Description, PC |
  Out-GridView

Here, the processes returned by Get-Process get an additional property called "PC" which contains the computer name where they were taken.

To view custom properties, make sure you either use Select-Object and specify the property name, or use the dot notation:

$list = Get-Process | 
  Add-Member -MemberType NoteProperty -Name PC -Value $env:COMPUTERNAME -PassThru
  
$list | ForEach-Object { 'Process {0} on {1}' -f $_.Name, $_.PC }

Twitter This Tip! ReTweet this Tip!