Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ChildItem with CreationTime instead of LastWriteTime?

Tags:

powershell

if you use Get-ChildItem you get something like

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          3/1/2006   9:03 AM            Bluetooth Software
d---s         5/10/2006   8:55 AM            Cookies
d----          5/9/2006   2:09 PM            Desktop

Thats fine. I just want now to change the LastWriteTime output to CreationTime. Everything else should be the same. Any ideas?

like image 522
silla Avatar asked Sep 26 '12 16:09

silla


4 Answers

You can select it with Select-Object or any of the Format-* cmdlets

Get-ChildItem | Select-Object Mode,CreationTime,Length,Name
like image 53
Shay Levy Avatar answered Nov 14 '22 05:11

Shay Levy


in V3 you can use dynamic type data:

    PS III> # UNTESTED: if work...you can paste this in your profile
    PS III>
    PS III> Update-TypeData -TypeName System.IO.FileInfo,System.IO.DirectoryInfo -MemberName DFPR DefaultDisplayPropertySet Mode,CreationTime,Length,Name
like image 43
walid2mi Avatar answered Nov 14 '22 06:11

walid2mi


For one-off changes to displayed columns, piping to select or Format-Table is easiest. If you want to make this a persistent change, you need to deal with the format files which govern how powershell displays filesystem objects.

Editing the existing format file (likely at $env:SystemRoot\system32\WindowsPowershell\v1.0\FileSystem.format.ps1xml) is not recommended, since that file has a signature block at the bottom. Changing the file content will invalidate the signature, which can cause problems.

Instead, you can define your own format file which will override the default one. Save the below file as FileFormat.format.ps1xml and run

Update-FormatData -Prepend c:\FileFormat.format.ps1xml

By default, CreationTime will be shown, not LastWriteTime.

Format file content (copied from real format file, just changed the relevant bits):

<Configuration>
    <SelectionSets>
        <SelectionSet>
            <Name>FileSystemTypes</Name>
            <Types>
                <TypeName>System.IO.DirectoryInfo</TypeName>
                <TypeName>System.IO.FileInfo</TypeName>
            </Types>
        </SelectionSet>
    </SelectionSets>
    <ViewDefinitions>
       <View>
            <Name>children</Name>
            <ViewSelectedBy>
                <SelectionSetName>FileSystemTypes</SelectionSetName>
            </ViewSelectedBy>
            <GroupBy>
                <PropertyName>PSParentPath</PropertyName> 
                <CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>  
            </GroupBy>
            <TableControl>
                <TableHeaders>
                   <TableColumnHeader>
                      <Label>Mode</Label>
                      <Width>7</Width>
                      <Alignment>left</Alignment>
                   </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>CreationTime</Label>
                        <Width>25</Width>
                        <Alignment>right</Alignment>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>Length</Label>
                        <Width>10</Width>
                        <Alignment>right</Alignment>
                    </TableColumnHeader>
                    <TableColumnHeader/>
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <Wrap/>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Mode</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <ScriptBlock>
                                    [String]::Format("{0,10}  {1,8}", $_.CreationTime.ToString("d"), $_.CreationTime.ToString("t"))
                                </ScriptBlock>
                            </TableColumnItem>
                            <TableColumnItem>
                            <PropertyName>Length</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>Name</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>
like image 22
latkin Avatar answered Nov 14 '22 04:11

latkin


If you meant that you wanted to show the property CreationTime instead of LastWriteTime, then you could pipe the output of Get-ChildItem to Select-Object and specify which properties to select:

Get-ChildItem | Select Mode, CreationTime, Length, Name
like image 3
dugas Avatar answered Nov 14 '22 05:11

dugas