How can I add a column to an object in PowerShell?
For example, the Get-Childitem returns an object, with Mode, LastWriteTime, Length Name, etc.... And I want to extend this object with an extra column, that is computed from LastWriteTime.
This is original Get-Childitem output:
Mode                LastWriteTime     Length Name                                                                                                                                                        
----                -------------     ------ ----                                                                                                                                                        
-a---       2012.12.15.     17:02       5390 Log_20121215.txt                                                                                                                       
-a---       2013.01.02.     17:10      14014 Log_20130102.txt                                                                                                                
-a---       2013.01.07.     17:08       2200 Log_20130107.txt
And I want this output:
Mode                LastWriteTime     Length Name                      ComputedColumn                                                                                                                                  
----                -------------     ------ ----                      --------------                                                                                                                                  
-a---       2012.12.15.     17:02       5390 Telenor_Log_20121215.txt  20131215                                                                                                                                  
-a---       2013.01.02.     17:10      14014 Telenor_Log_20130102.txt  20140102                                                                                                                                  
-a---       2013.01.07.     17:08       2200 Telenor_Log_20130107.txt  20140207
Thanks for any help.
Use the –ExpandProperty parameter from Select-Object to expand objects in Windows PowerShell.
To use Add-Member , pipe the object to Add-Member , or use the InputObject parameter to specify the object. The MemberType parameter indicates the type of member that you want to add. The Name parameter assigns a name to the new member, and the Value parameter sets the value of the member.
A NoteProperty is a static property name, often added by Select-Object or Add-Member . Finally, you might also see PropertySet . Think of this as a prepackaged bundle of properties. These are defined by PowerShell or cmdlet developers—for example, a process object as a PSResources property set.
Long description. The [pscustomobject] type accelerator was added in PowerShell 4.0. Prior to adding this type accelerator, creating an object with member properties and values was more complicated. Originally, you had to use New-Object to create the object and Add-Member to add properties.
Use Add-Member or a custom expression in select depending on how you need it.
Compute and store. Keeps original object, but adds one custom column
$data = dir | % { Add-Member -InputObject $_ -MemberType NoteProperty -Name "ComputedColumn" -Value $_.LastWriteTime.AddYears(1).ToString("yyyyMMdd") -PassThru }
Compute it before displaying (or exporting to csv etc.)
dir | select Mode, LastWriteTime, Length, Name, @{name="ComputedColumn";expression={ $_.LastWriteTime.AddYears(1).ToString("yyyyMMdd") }}
Ex. with format-table to show properly
dir | select Mode, LastWriteTime, Length, Name, @{name="ComputedColumn";expression={ $_.LastWriteTime.AddYears(1).ToString("yyyyMMdd") }} | ft -AutoSize
Mode  LastWriteTime       Length Name                    ComputedColumn
----  -------------       ------ ----                    --------------
d-r-- 14.04.2013 17:47:18        Contacts                20140414      
d-r-- 15.05.2013 14:19:45        Desktop                 20140515      
d-r-- 14.04.2013 18:03:33        Documents               20140414      
d-r-- 11.05.2013 18:22:57        Downloads               20140511      
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With