Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell extend an object

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.

like image 358
gazsiazasz Avatar asked May 15 '13 13:05

gazsiazasz


People also ask

How do you expand an object in PowerShell?

Use the –ExpandProperty parameter from Select-Object to expand objects in Windows PowerShell.

How do I add a value to an object in 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.

What is NoteProperty in PowerShell?

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.

What is Pscustomobject PowerShell?

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.


1 Answers

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      
like image 177
Frode F. Avatar answered Sep 29 '22 16:09

Frode F.