Share via


Another Way to Create NoteProperty Entries

We've seen the Select-Object synthetic property method and the Add-Member -name AddNoteProperty method, but here's probably the most straightforward way to output object data.

$object = $true | Select ComputerName, Path, ContentCount;
$object.ComputerName =$env:COMPUTERNAME.ToLower();
$object.Path =Get-Location;
$object.ContentCount = (Get-ChildItem).Count;
$object;

This works because the first line creates an object with the NoteProperty properties we need.  When we Select-Object on properties that don't exist, the cmdlet will create them and set the value to $null. The rest of the script is just a matter of populating the properties, then the last line outputs it.