The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Shay (10) [Avatar] Offline
#1
Hi All,

I'm trying to extend the output of Get-ChildItem to display additional
columns, such as file age and formatting the file sizes to "human readable" format.

I created a new format .ps1xml file (see below) and loaded it using the Update-FormatData cmdlet.
Update-FormatData -pre <path>Dir.Format.PS1xml;

all is working as expected when listing directories only:
PS C:Scripts> dir | ? {$_.PSIsContainer};


Mode LastWriteTime Length Name Age
---- ------------- ------ ---- ---
d---- 26/06/2007 13:27:32 <DIR> Backup 13.91 D
d---- 20/06/2007 13:17:13 <DIR> Database 19.92 D
d---- 18/06/2007 13:56:26 <DIR> Eventlog 21.89 D
d---- 10/06/2007 16:14:35 <DIR> Exchange 29.79 D
d---- 01/07/2007 00:33:16 <DIR> Extend 9.45 D
d---- 03/06/2007 12:30:43 <DIR> files 1.23 M

when listing files only, the output is also ok

PS C:Scripts> dir | ? {!$_.PSIsContainer};

Mode LastWriteTime Length Name Age
---- ------------- ------ ---- ---
-a--- 07/06/2007 17:57:38 155 B IIsBackup.ps1 1.09 M
-a--- 21/06/2007 23:20:47 1.48 KB SpamCheck.ps1 18.50 D
-a--- 07/06/2007 18:15:37 24 B outlook.ps1 1.09 M
-a--- 03/06/2007 12:37:45 788 B timeZone.ps1 1.23 M



when executing dir with no additional filtering, the output for files is displayed like format-list

PS C:Scripts> dir



Mode LastWriteTime Length Name Age
---- ------------- ------ ---- ---
d---- 26/06/2007 13:27:32 <DIR> Backup 13.91 D
d---- 20/06/2007 13:17:13 <DIR> Database 19.92 D
d---- 18/06/2007 13:56:26 <DIR> Eventlog 21.89 D
d---- 10/06/2007 16:14:35 <DIR> Exchange 29.79 D
d---- 01/07/2007 00:33:16 <DIR> Extend 9.45 D
d---- 03/06/2007 12:30:43 <DIR> files 1.23 M


LastWriteTime : 07/06/2007 17:57:38
Length : 155
Name : IIsBackup.ps1


LastWriteTime : 21/06/2007 23:20:47
Length : 1513
Name : SpamCheck.ps1


LastWriteTime : 07/06/2007 18:15:37
Length : 24
Name : outlook.ps1


LastWriteTime : 03/06/2007 12:37:45
Length : 788
Name : timeZone.ps1


Even dir | ft didnt help.

I also created a new View ps1xml file (not included). with that file loaded (Update-FormatData) all output is priniting as it should:
dir | ft -view myview;

Any suggestions?

Thanks,

Shay


########################### Dir.Format.PS1xml ##########################

<Configuration>
<ViewDefinitions>
<View>
<Name>DirectoryView</Name>
<ViewSelectedBy>
<TypeName>System.IO.DirectoryInfo</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Mode</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>LastWriteTime</Label>
<Width>19</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Length</Label>
<Width>10</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Name</Label>
<Width>25</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Age</Label>
<Width>8</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Mode</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>LastWriteTime</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
"<DIR>"
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$fileAge = New-TimeSpan $_.lastwritetime $(Get-Date);
if($fileAge.TotalDays -gt 365){[string]::Format("{0:0.00} Y",$fileAge.TotalDays / 365 )}
elseif($fileAge.TotalDays -gt 30){[string]::Format("{0:0.00} M",$fileAge.TotalDays / 30)}
elseif($fileAge.TotalHours -gt 24){[string]::Format("{0:0.00} D",$fileAge.TotalHours / 24)}
elseif($fileAge.TotalMinutes -gt 60){[string]::Format("{0:0.00} H",$fileAge.TotalMinutes / 60)}
else{[string]::Format("{0:0} m",$fileAge.TotalMinutes)}
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>FileView</Name>
<ViewSelectedBy>
<TypeName>System.IO.FileInfo</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Mode</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>LastWriteTime</Label>
<Width>19</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Length</Label>
<Width>10</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Name</Label>
<Width>25</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Age</Label>
<Width>8</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Mode</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>LastWriteTime</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
if($_.length -gt 1gb){[string]::Format("{0:0.00} GB",$_.length / 1gb)}
elseif($_.length -gt 1mb){[string]::Format("{0:0.00} MB",$_.length / 1mb)}
elseif($_.length -gt 1kb){[string]::Format("{0:0.00} KB",$_.length / 1kb)}
else{[string]::Format("{0} B ",$_.length)}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$fileAge = New-TimeSpan $_.lastwritetime $(Get-Date);
if($fileAge.TotalDays -gt 365){[string]::Format("{0:0.00} Y",$fileAge.TotalDays / 365 )}
elseif($fileAge.TotalDays -gt 30){[string]::Format("{0:0.00} M",$fileAge.TotalDays / 30)}
elseif($fileAge.TotalHours -gt 24){[string]::Format("{0:0.00} D",$fileAge.TotalHours / 24)}
elseif($fileAge.TotalMinutes -gt 60){[string]::Format("{0:0.00} H",$fileAge.TotalMinutes / 60)}
else{[string]::Format("{0:0} m",$fileAge.TotalMinutes)}
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>

#######################################################