Is there a short-cut to creating a new [pscustomobject]?

I have the ff. PS code:

 $d = import-csv     # the original CSV file has 3 properties Prop1,Prop2 and Prop3

$d | Add-member -Membertype Scriptproperty -Name "AddProp4" -Value xxx

Foreach -process {

[pscustomobject]@{ Prop1=$_.Prop1;   Prop2=$_.Prop2;  Prop3=$_.Prop3;  Prop4 = $_.AddProp4 }

}  | Sort {$_.Prop4} |  Out-gridview

As can be seen, the 3 original propertied are just repeated into the new, “expanded” object. Is there a shorter way to “repeat” the same old properties without having to retype them one by one?

Thanks for your help/advice.

 

Using Select-Object generates a new PSObject. Using a calculated expression you can generate a static value for all rows or run pretty much any code as well

$d = import-csv | Select *,@{Name='Prop4';Expression={'A Static Value'}}

#OR

$d = import-csv | Select Prop1,Prop2,Prop3,@{Name='Prop4';Expression={'A Static Value'}}

#OR (Use label)

$d = import-csv | Select *,@{Label='Prop4';Expression={'A Static Value'}}

#OR shorthand

$d = import-csv | Select *,@{n='Prop4';e={'A Static Value'}}

#OR code expression (replace 1 with 2 in Prop3)

$d = import-csv | Select *,@{n='Prop4';e={$_.Prop3 -replace '1','2'}}

Personally, I use Select-Object first. If I’m joining multiple objects, I would do something like this:

$os   = Get-CimInstance -ClassName Win32_OperatingSystem -Property Caption,FreePhysicalMemory,FreeSpaceInPagingFiles
$bios = Get-CimInstance -ClassName Win32_Bios -Property BiosVersion

$results = $os | 
           Select -Property Caption, 
                            FreePhysicalMemory,
                            FreeSpaceInPagingFiles,
                            @{Name='BiosVersion';Expression={$bios.Version}}

Add-member has a -passthru option.

import-csv test.csv | 
Add-member -membertype Scriptproperty -name AddProp4 -value {'hi'} -PassThru

prop1 prop2 prop3 AddProp4
—–--- —–--- —–--- ——–-----
1     2     3     hi

Many thanks Mr Simmers, much appreciated.