Frage

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.

War es hilfreich?

Lösung

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      

Andere Tipps

The easiest way to extend the object is to pipe it to the cmdlet Select-Object. See the example below.

# Add the extended property IsVeryBig
Get-ChildItem c:\temp | Select-Object *, IsVeryBig | 

    ForEach-Object { $_.IsVeryBig = $True }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top