Extended Get-ChildItem output

S

Shay Levi

I'm trying to extend the output of Get-ChildItem using synthetic members,
display additional
columns, such as file age and file size in a "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



Any suggestions?

Thanks,

Shay
http://www.scriptolog.blogspot.com




########################### 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>
"&lt;DIR&gt;"
</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>

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

My Computer

J

John Vottero

out-default looks at the first object in the pipeline to determine how to
format ALL of the objects in the pipeline. If it finds a different object
in the pipeline, it drops into a generic list output.

Take a look at the FileSystem.Format.ps1xml file, at the very beginning you
will see the definition of the FileSystemTypes <SelectionSet>, this is what
tells out-default that System.IO.DirectoryInfo and System.IO.FileInfo are
related. Later, you see the FileSystemTypes selection set referenced in
View definitions.

You need to either create your own <SelectionSet> or, you might be able to
override the <ViewDefinitions> (I haven't tried that).


"Shay Levi" <[email protected]> wrote in message
news:[email protected]
>
>
> I'm trying to extend the output of Get-ChildItem using synthetic members,
> display additional
> columns, such as file age and file size in a "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
>
>
>
> Any suggestions?
>
> Thanks,
>
> Shay
> http://www.scriptolog.blogspot.com
>
>
>
>
> ########################### 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> "&lt;DIR&gt;"
> </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>
>
> #######################################################
>
>
 

My Computer

S

Shay Levi

I converted it to a view file (attached) and updated it with
Update-FormatData -PrependPath myViews1.ps1xml
Then invoked it with :

dir . | ft -view ExtDir;

It works as expected. The only difference is the use of
System.IO.FileSystemInfo type.

When I changed the type in the format file to System.IO.FileSystemInfo
output was reverted to default.

Adding SelectionSets also didn't help



Shay
http://www.scriptolog.blogspot.com

> out-default looks at the first object in the pipeline to determine how
> to format ALL of the objects in the pipeline. If it finds a different
> object in the pipeline, it drops into a generic list output.
>
> Take a look at the FileSystem.Format.ps1xml file, at the very
> beginning you will see the definition of the FileSystemTypes
> <SelectionSet>, this is what tells out-default that
> System.IO.DirectoryInfo and System.IO.FileInfo are related. Later,
> you see the FileSystemTypes selection set referenced in View
> definitions.
>
> You need to either create your own <SelectionSet> or, you might be
> able to override the <ViewDefinitions> (I haven't tried that).
>
> "Shay Levi" <[email protected]> wrote in message
> news:[email protected]
>
>> I'm trying to extend the output of Get-ChildItem using synthetic
>> members,
>> display additional
>> columns, such as file age and file size in a "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
>> Any suggestions?
>>
>> Thanks,
>>
>> Shay
>> http://www.scriptolog.blogspot.com
>> ########################### 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> "&lt;DIR&gt;"
>> </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>
>> #######################################################
>>
 

My Computer

J

John Vottero

You only have one type (System.IO.FileSystemInfo) listed in the
<ViewSelectedBy> element, two, like this:

<ViewSelectedBy>
<TypeName>System.IO.DirectoryInfo</TypeName>
<TypeName>System.IO.FileSystemInfo</TypeName>
</ViewSelectedBy>

and it works.

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

PS C:\Users\John> Update-FormatData -prependpath myViews.ps1xml
PS C:\Users\John> dir

Mode LastWriteTime Length Name Age
---- ------------- ------ ---- ---
d-r-- 29/05/2007 13:02 <DIR> Contacts 1.61 M
d-r-- 08/03/2007 11:42 <DIR> Desktop 4.34 M
d-r-- 02/07/2007 16:36 <DIR> Documents 14.06 D
d-r-- 22/03/2007 13:25 <DIR> Downloads 3.87 M
d-r-- 03/07/2007 12:09 <DIR> Favorites 13.25 D
d-r-- 08/06/2007 10:27 <DIR> Links 1.28 M
d-r-- 27/03/2007 21:31 <DIR> Music 3.70 M
d-r-- 08/03/2007 11:42 <DIR> Pictures 4.34 M
d-r-- 08/03/2007 11:42 <DIR> Saved Games 4.34 M
d-r-- 08/03/2007 11:42 <DIR> Searches 4.34 M
d-r-- 08/03/2007 11:42 <DIR> Videos 4.34 M
-a--- 18/05/2007 15:09 480 B 1 1.97 M
-a--- 11/05/2007 23:22 342.00 KB CertifiedforWindowsVis... 2.19 M
-a--- 20/03/2007 17:15 169.24 KB CurrentSettings-2007-0... 3.93 M
-a--- 07/07/2007 20:08 13 B delete 8.92 D
-a--- 30/05/2007 15:04 1.19 KB JAMS Exception.txt 1.57 M
-a--- 08/06/2007 13:58 5.71 KB JAMS Scheduler V4_3_81... 1.27 M
-a--- 12/06/2007 15:11 314 B LoadTest.ps1 1.14 M
-a--- 16/07/2007 18:05 2.89 KB myViews.ps1xml 1 m
-a--- 26/05/2007 16:16 92 B pt.ps1 1.70 M
-a--- 01/04/2007 20:23 600 B PUTTY.RND 3.53 M
-a--- 07/05/2007 14:15 148 B ScriptErrors.ps1 2.34 M
-a--- 30/05/2007 15:04 787.48 KB Submit R584211WR JAMS.zip 1.57 M
-a--- 30/06/2007 18:01 0 B test.tmp 16.00 D
-a--- 07/05/2007 14:17 24 B test.txt 2.34 M


PS C:\Users\John>
"Shay Levi" <[email protected]> wrote in message
news:[email protected]
>I converted it to a view file (attached) and updated it with
> Update-FormatData -PrependPath myViews1.ps1xml
> Then invoked it with :
>
> dir . | ft -view ExtDir;
>
> It works as expected. The only difference is the use of
> System.IO.FileSystemInfo type.
>
> When I changed the type in the format file to System.IO.FileSystemInfo
> output was reverted to default.
>
> Adding SelectionSets also didn't help
>
>
>
> Shay
> http://www.scriptolog.blogspot.com
>
>> out-default looks at the first object in the pipeline to determine how
>> to format ALL of the objects in the pipeline. If it finds a different
>> object in the pipeline, it drops into a generic list output.
>>
>> Take a look at the FileSystem.Format.ps1xml file, at the very
>> beginning you will see the definition of the FileSystemTypes
>> <SelectionSet>, this is what tells out-default that
>> System.IO.DirectoryInfo and System.IO.FileInfo are related. Later,
>> you see the FileSystemTypes selection set referenced in View
>> definitions.
>>
>> You need to either create your own <SelectionSet> or, you might be
>> able to override the <ViewDefinitions> (I haven't tried that).
>>
>> "Shay Levi" <[email protected]> wrote in message
>> news:[email protected]
>>
>>> I'm trying to extend the output of Get-ChildItem using synthetic
>>> members,
>>> display additional
>>> columns, such as file age and file size in a "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
>>> Any suggestions?
>>>
>>> Thanks,
>>>
>>> Shay
>>> http://www.scriptolog.blogspot.com
>>> ########################### 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> "&lt;DIR&gt;"
>>> </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>
>>> #######################################################
>>>
 

My Computer

S

Shay Levi

John, The view file is working well even with one type System.IO.FileSystemInfo.
My problem is not the view file, it's the format file discussed in the first
thread of this post.

Anyway, to run the view file I need to tell Format table to use the View name
dir . | ft -view ExtDir;

This is long enough when working interactively. Basically, I want to make
it the default
output so that I only have to type "dir" with no other parameters.


When listing the type names for each file system type (dir or file) you can
see
that the only item I didn't reference inside the format file is System.IO.FileSystemInfo.
When I do include it, the dir output is reverted back to the default PowerShell
view,
as if the output format didn't flow through the format file.


PS C:\Scripts> (dir | ? {$_.PSIsContainer})[0].pstypenames
System.IO.DirectoryInfo
System.IO.FileSystemInfo
System.MarshalByRefObject
System.Object


PS C:\Scripts> (dir | ? {! $_.PSIsContainer})[0].pstypenames
System.IO.FileInfo
System.IO.FileSystemInfo
System.MarshalByRefObject
System.Object


I also checked the Out-Default cmdlet help:

SYNOPSIS
Send the output to the default formatter and the default output cmdlet.
This cmdlet has no effect on the formatting or output. It is a
placeholder that lets you write your own Out-Default function or cmdlet.


As the synopsis states "This cmdlet has no effect... "

I Just hope it's not right under the nose :)


Shay
http://www.scriptolog.blogspot.com

> You only have one type (System.IO.FileSystemInfo) listed in the
> <ViewSelectedBy> element, two, like this:
>
> <ViewSelectedBy>
> <TypeName>System.IO.DirectoryInfo</TypeName>
> <TypeName>System.IO.FileSystemInfo</TypeName>
> </ViewSelectedBy>
> and it works.
>
> Windows PowerShell
> Copyright (C) 2006 Microsoft Corporation. All rights reserved.
> PS C:\Users\John> Update-FormatData -prependpath myViews.ps1xml PS
> C:\Users\John> dir
>
> Mode LastWriteTime Length Name
> Age ---- ------------- ------ ----
> --- d-r-- 29/05/2007 13:02 <DIR> Contacts
> 1.61 M d-r-- 08/03/2007 11:42 <DIR> Desktop
> 4.34 M d-r-- 02/07/2007 16:36 <DIR> Documents
> 14.06 D d-r-- 22/03/2007 13:25 <DIR> Downloads
> 3.87 M d-r-- 03/07/2007 12:09 <DIR> Favorites
> 13.25 D d-r-- 08/06/2007 10:27 <DIR> Links
> 1.28 M d-r-- 27/03/2007 21:31 <DIR> Music
> 3.70 M d-r-- 08/03/2007 11:42 <DIR> Pictures
> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Saved Games
> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Searches
> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Videos
> 4.34 M -a--- 18/05/2007 15:09 480 B 1
> 1.97 M -a--- 11/05/2007 23:22 342.00 KB
> CertifiedforWindowsVis... 2.19 M -a--- 20/03/2007 17:15 169.24
> KB CurrentSettings-2007-0... 3.93 M -a--- 07/07/2007 20:08
> 13 B delete 8.92 D -a--- 30/05/2007 15:04
> 1.19 KB JAMS Exception.txt 1.57 M -a--- 08/06/2007 13:58
> 5.71 KB JAMS Scheduler V4_3_81... 1.27 M -a--- 12/06/2007 15:11
> 314 B LoadTest.ps1 1.14 M -a--- 16/07/2007 18:05
> 2.89 KB myViews.ps1xml 1 m -a--- 26/05/2007 16:16
> 92 B pt.ps1 1.70 M -a--- 01/04/2007 20:23
> 600 B PUTTY.RND 3.53 M -a--- 07/05/2007 14:15
> 148 B ScriptErrors.ps1 2.34 M -a--- 30/05/2007 15:04
> 787.48 KB Submit R584211WR JAMS.zip 1.57 M -a--- 30/06/2007 18:01
> 0 B test.tmp 16.00 D -a--- 07/05/2007 14:17
> 24 B test.txt 2.34 M
>
> PS C:\Users\John>
> "Shay Levi" <[email protected]> wrote in message
> news:[email protected]
>> I converted it to a view file (attached) and updated it with
>> Update-FormatData -PrependPath myViews1.ps1xml
>> Then invoked it with :
>> dir . | ft -view ExtDir;
>>
>> It works as expected. The only difference is the use of
>> System.IO.FileSystemInfo type.
>>
>> When I changed the type in the format file to
>> System.IO.FileSystemInfo output was reverted to default.
>>
>> Adding SelectionSets also didn't help
>>
>> Shay
>> http://www.scriptolog.blogspot.com
>>> out-default looks at the first object in the pipeline to determine
>>> how to format ALL of the objects in the pipeline. If it finds a
>>> different object in the pipeline, it drops into a generic list
>>> output.
>>>
>>> Take a look at the FileSystem.Format.ps1xml file, at the very
>>> beginning you will see the definition of the FileSystemTypes
>>> <SelectionSet>, this is what tells out-default that
>>> System.IO.DirectoryInfo and System.IO.FileInfo are related. Later,
>>> you see the FileSystemTypes selection set referenced in View
>>> definitions.
>>>
>>> You need to either create your own <SelectionSet> or, you might be
>>> able to override the <ViewDefinitions> (I haven't tried that).
>>>
>>> "Shay Levi" <[email protected]> wrote in message
>>> news:[email protected]
>>>> I'm trying to extend the output of Get-ChildItem using synthetic
>>>> members,
>>>> display additional
>>>> columns, such as file age and file size in a "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
>>>> Any suggestions?
>>>> Thanks,
>>>>
>>>> Shay
>>>> http://www.scriptolog.blogspot.com
>>>> ########################### 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> "&lt;DIR&gt;"
>>>> </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>
>>>> #######################################################
 

My Computer

J

John Vottero

I took the file that you attached, added the DirectoryInfo type in the
<ViewSelectedBy> element, did an Update-FormatData and I can type "dir" and
get a directory listing that includes an "Age" column. It's hard to see
because of wrapping but, a cut/paste of that was in my last post.

"Shay Levi" <[email protected]> wrote in message
news:[email protected]
> John, The view file is working well even with one type
> System.IO.FileSystemInfo.
> My problem is not the view file, it's the format file discussed in the
> first thread of this post.
>
> Anyway, to run the view file I need to tell Format table to use the View
> name
> dir . | ft -view ExtDir;


You only need to use -View if you want something other than the default.
Adding type names to the <ViewSelectedBy> element tells out-default to
select that view when it's formating the type.

>
> This is long enough when working interactively. Basically, I want to make
> it the default
> output so that I only have to type "dir" with no other parameters.
>
>
> When listing the type names for each file system type (dir or file) you
> can see
> that the only item I didn't reference inside the format file is
> System.IO.FileSystemInfo.
> When I do include it, the dir output is reverted back to the default
> PowerShell view,
> as if the output format didn't flow through the format file.
>
>
> PS C:\Scripts> (dir | ? {$_.PSIsContainer})[0].pstypenames
> System.IO.DirectoryInfo
> System.IO.FileSystemInfo
> System.MarshalByRefObject
> System.Object
>
>
> PS C:\Scripts> (dir | ? {! $_.PSIsContainer})[0].pstypenames
> System.IO.FileInfo
> System.IO.FileSystemInfo
> System.MarshalByRefObject
> System.Object
>
>
> I also checked the Out-Default cmdlet help:
>
> SYNOPSIS
> Send the output to the default formatter and the default output cmdlet.
> This cmdlet has no effect on the formatting or output. It is a
> placeholder that lets you write your own Out-Default function or
> cmdlet.
>
>
> As the synopsis states "This cmdlet has no effect... "
>
> I Just hope it's not right under the nose :)
>
>
> Shay
> http://www.scriptolog.blogspot.com
>
>> You only have one type (System.IO.FileSystemInfo) listed in the
>> <ViewSelectedBy> element, two, like this:
>>
>> <ViewSelectedBy>
>> <TypeName>System.IO.DirectoryInfo</TypeName>
>> <TypeName>System.IO.FileSystemInfo</TypeName>
>> </ViewSelectedBy>
>> and it works.
>>
>> Windows PowerShell
>> Copyright (C) 2006 Microsoft Corporation. All rights reserved.
>> PS C:\Users\John> Update-FormatData -prependpath myViews.ps1xml PS
>> C:\Users\John> dir
>>
>> Mode LastWriteTime Length Name
>> Age ---- ------------- ------ ----
>> --- d-r-- 29/05/2007 13:02 <DIR> Contacts
>> 1.61 M d-r-- 08/03/2007 11:42 <DIR> Desktop
>> 4.34 M d-r-- 02/07/2007 16:36 <DIR> Documents
>> 14.06 D d-r-- 22/03/2007 13:25 <DIR> Downloads
>> 3.87 M d-r-- 03/07/2007 12:09 <DIR> Favorites
>> 13.25 D d-r-- 08/06/2007 10:27 <DIR> Links
>> 1.28 M d-r-- 27/03/2007 21:31 <DIR> Music
>> 3.70 M d-r-- 08/03/2007 11:42 <DIR> Pictures
>> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Saved Games
>> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Searches
>> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Videos
>> 4.34 M -a--- 18/05/2007 15:09 480 B 1
>> 1.97 M -a--- 11/05/2007 23:22 342.00 KB
>> CertifiedforWindowsVis... 2.19 M -a--- 20/03/2007 17:15 169.24
>> KB CurrentSettings-2007-0... 3.93 M -a--- 07/07/2007 20:08
>> 13 B delete 8.92 D -a--- 30/05/2007 15:04
>> 1.19 KB JAMS Exception.txt 1.57 M -a--- 08/06/2007 13:58
>> 5.71 KB JAMS Scheduler V4_3_81... 1.27 M -a--- 12/06/2007 15:11
>> 314 B LoadTest.ps1 1.14 M -a--- 16/07/2007 18:05
>> 2.89 KB myViews.ps1xml 1 m -a--- 26/05/2007 16:16
>> 92 B pt.ps1 1.70 M -a--- 01/04/2007 20:23
>> 600 B PUTTY.RND 3.53 M -a--- 07/05/2007 14:15
>> 148 B ScriptErrors.ps1 2.34 M -a--- 30/05/2007 15:04
>> 787.48 KB Submit R584211WR JAMS.zip 1.57 M -a--- 30/06/2007 18:01
>> 0 B test.tmp 16.00 D -a--- 07/05/2007 14:17
>> 24 B test.txt 2.34 M
>>
>> PS C:\Users\John>
>> "Shay Levi" <[email protected]> wrote in message
>> news:[email protected]
>>> I converted it to a view file (attached) and updated it with
>>> Update-FormatData -PrependPath myViews1.ps1xml
>>> Then invoked it with :
>>> dir . | ft -view ExtDir;
>>>
>>> It works as expected. The only difference is the use of
>>> System.IO.FileSystemInfo type.
>>>
>>> When I changed the type in the format file to
>>> System.IO.FileSystemInfo output was reverted to default.
>>>
>>> Adding SelectionSets also didn't help
>>>
>>> Shay
>>> http://www.scriptolog.blogspot.com
>>>> out-default looks at the first object in the pipeline to determine
>>>> how to format ALL of the objects in the pipeline. If it finds a
>>>> different object in the pipeline, it drops into a generic list
>>>> output.
>>>>
>>>> Take a look at the FileSystem.Format.ps1xml file, at the very
>>>> beginning you will see the definition of the FileSystemTypes
>>>> <SelectionSet>, this is what tells out-default that
>>>> System.IO.DirectoryInfo and System.IO.FileInfo are related. Later,
>>>> you see the FileSystemTypes selection set referenced in View
>>>> definitions.
>>>>
>>>> You need to either create your own <SelectionSet> or, you might be
>>>> able to override the <ViewDefinitions> (I haven't tried that).
>>>>
>>>> "Shay Levi" <[email protected]> wrote in message
>>>> news:[email protected]
>>>>> I'm trying to extend the output of Get-ChildItem using synthetic
>>>>> members,
>>>>> display additional
>>>>> columns, such as file age and file size in a "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
>>>>> Any suggestions?
>>>>> Thanks,
>>>>>
>>>>> Shay
>>>>> http://www.scriptolog.blogspot.com
>>>>> ########################### 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> "&lt;DIR&gt;"
>>>>> </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>
>>>>> #######################################################

>
>
 

My Computer

S

Shay Levi

Cheers John. Problem solved!

Shay
http://www.scriptolog.blogspot.com

> I took the file that you attached, added the DirectoryInfo type in the
> <ViewSelectedBy> element, did an Update-FormatData and I can type
> "dir" and get a directory listing that includes an "Age" column. It's
> hard to see because of wrapping but, a cut/paste of that was in my
> last post.
>
> "Shay Levi" <[email protected]> wrote in message
> news:[email protected]
>
>> John, The view file is working well even with one type
>> System.IO.FileSystemInfo.
>> My problem is not the view file, it's the format file discussed in
>> the
>> first thread of this post.
>> Anyway, to run the view file I need to tell Format table to use the
>> View
>> name
>> dir . | ft -view ExtDir;

> You only need to use -View if you want something other than the
> default. Adding type names to the <ViewSelectedBy> element tells
> out-default to select that view when it's formating the type.
>
>> This is long enough when working interactively. Basically, I want to
>> make
>> it the default
>> output so that I only have to type "dir" with no other parameters.
>> When listing the type names for each file system type (dir or file)
>> you
>> can see
>> that the only item I didn't reference inside the format file is
>> System.IO.FileSystemInfo.
>> When I do include it, the dir output is reverted back to the default
>> PowerShell view,
>> as if the output format didn't flow through the format file.
>> PS C:\Scripts> (dir | ? {$_.PSIsContainer})[0].pstypenames
>> System.IO.DirectoryInfo
>> System.IO.FileSystemInfo
>> System.MarshalByRefObject
>> System.Object
>> PS C:\Scripts> (dir | ? {! $_.PSIsContainer})[0].pstypenames
>> System.IO.FileInfo
>> System.IO.FileSystemInfo
>> System.MarshalByRefObject
>> System.Object
>> I also checked the Out-Default cmdlet help:
>>
>> SYNOPSIS
>> Send the output to the default formatter and the default output
>> cmdlet.
>> This cmdlet has no effect on the formatting or output. It is a
>> placeholder that lets you write your own Out-Default function or
>> cmdlet.
>> As the synopsis states "This cmdlet has no effect... "
>>
>> I Just hope it's not right under the nose :)
>>
>> Shay
>> http://www.scriptolog.blogspot.com
>>> You only have one type (System.IO.FileSystemInfo) listed in the
>>> <ViewSelectedBy> element, two, like this:
>>>
>>> <ViewSelectedBy>
>>> <TypeName>System.IO.DirectoryInfo</TypeName>
>>> <TypeName>System.IO.FileSystemInfo</TypeName>
>>> </ViewSelectedBy>
>>> and it works.
>>> Windows PowerShell
>>> Copyright (C) 2006 Microsoft Corporation. All rights reserved.
>>> PS C:\Users\John> Update-FormatData -prependpath myViews.ps1xml PS
>>> C:\Users\John> dir
>>> Mode LastWriteTime Length Name
>>> Age ---- ------------- ------ ----
>>> --- d-r-- 29/05/2007 13:02 <DIR> Contacts
>>> 1.61 M d-r-- 08/03/2007 11:42 <DIR> Desktop
>>> 4.34 M d-r-- 02/07/2007 16:36 <DIR> Documents
>>> 14.06 D d-r-- 22/03/2007 13:25 <DIR> Downloads
>>> 3.87 M d-r-- 03/07/2007 12:09 <DIR> Favorites
>>> 13.25 D d-r-- 08/06/2007 10:27 <DIR> Links
>>> 1.28 M d-r-- 27/03/2007 21:31 <DIR> Music
>>> 3.70 M d-r-- 08/03/2007 11:42 <DIR> Pictures
>>> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Saved Games
>>> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Searches
>>> 4.34 M d-r-- 08/03/2007 11:42 <DIR> Videos
>>> 4.34 M -a--- 18/05/2007 15:09 480 B 1
>>> 1.97 M -a--- 11/05/2007 23:22 342.00 KB
>>> CertifiedforWindowsVis... 2.19 M -a--- 20/03/2007 17:15
>>> 169.24
>>> KB CurrentSettings-2007-0... 3.93 M -a--- 07/07/2007 20:08
>>> 13 B delete 8.92 D -a--- 30/05/2007 15:04
>>> 1.19 KB JAMS Exception.txt 1.57 M -a--- 08/06/2007 13:58
>>> 5.71 KB JAMS Scheduler V4_3_81... 1.27 M -a--- 12/06/2007 15:11
>>> 314 B LoadTest.ps1 1.14 M -a--- 16/07/2007 18:05
>>> 2.89 KB myViews.ps1xml 1 m -a--- 26/05/2007 16:16
>>> 92 B pt.ps1 1.70 M -a--- 01/04/2007 20:23
>>> 600 B PUTTY.RND 3.53 M -a--- 07/05/2007 14:15
>>> 148 B ScriptErrors.ps1 2.34 M -a--- 30/05/2007 15:04
>>> 787.48 KB Submit R584211WR JAMS.zip 1.57 M -a--- 30/06/2007
>>> 18:01
>>> 0 B test.tmp 16.00 D -a--- 07/05/2007 14:17
>>> 24 B test.txt 2.34 M
>>> PS C:\Users\John>
>>> "Shay Levi" <[email protected]> wrote in message
>>> news:[email protected]
>>>> I converted it to a view file (attached) and updated it with
>>>> Update-FormatData -PrependPath myViews1.ps1xml
>>>> Then invoked it with :
>>>> dir . | ft -view ExtDir;
>>>> It works as expected. The only difference is the use of
>>>> System.IO.FileSystemInfo type.
>>>>
>>>> When I changed the type in the format file to
>>>> System.IO.FileSystemInfo output was reverted to default.
>>>> Adding SelectionSets also didn't help
>>>>
>>>> Shay
>>>> http://www.scriptolog.blogspot.com
>>>>> out-default looks at the first object in the pipeline to determine
>>>>> how to format ALL of the objects in the pipeline. If it finds a
>>>>> different object in the pipeline, it drops into a generic list
>>>>> output.
>>>>>
>>>>> Take a look at the FileSystem.Format.ps1xml file, at the very
>>>>> beginning you will see the definition of the FileSystemTypes
>>>>> <SelectionSet>, this is what tells out-default that
>>>>> System.IO.DirectoryInfo and System.IO.FileInfo are related.
>>>>> Later, you see the FileSystemTypes selection set referenced in
>>>>> View definitions.
>>>>>
>>>>> You need to either create your own <SelectionSet> or, you might be
>>>>> able to override the <ViewDefinitions> (I haven't tried that).
>>>>>
>>>>> "Shay Levi" <[email protected]> wrote in message
>>>>> news:[email protected]
>>>>>> I'm trying to extend the output of Get-ChildItem using synthetic
>>>>>> members,
>>>>>> display additional
>>>>>> columns, such as file age and file size in a "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
>>>>>> Any suggestions?
>>>>>> Thanks,
>>>>>> Shay
>>>>>> http://www.scriptolog.blogspot.com
>>>>>> ########################### 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> "&lt;DIR&gt;"
>>>>>> </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>
>>>>>> #######################################################
 

My Computer

Top