使用CreationTime代替LastWriteTime的Get-ChildItem命令?

10
如果你使用Get-ChildItem,你会得到类似这样的东西。
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

好的。我现在只想将LastWriteTime输出更改为CreationTime。其他方面应该保持不变。有什么建议吗?


你是不是拼错了CreationTime,还是你真的想用CreatingTime替换LastWriteTime? - dugas
5个回答

9
您可以使用 Select-Object 或任何 Format-* 命令来选择它。
Get-ChildItem | Select-Object Mode,CreationTime,Length,Name

5

如果要对显示的列进行单次更改,可以使用selectFormat-Table命令。 如果您想将其作为持久更改,则需要处理格式文件,该文件控制powershell如何显示文件系统对象。

不建议编辑现有的格式文件(可能位于$env:SystemRoot\system32\WindowsPowershell\v1.0\FileSystem.format.ps1xml),因为该文件在底部具有签名块。 更改文件内容将使签名无效,可能会引起问题。

相反,您可以定义自己的格式文件,该文件将覆盖默认文件。 将以下文件保存为FileFormat.format.ps1xml并运行:

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

默认情况下,将显示CreationTime而不是LastWriteTime

格式文件内容(从真实格式文件复制,只更改了相关部分):

<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>

3

在 V3 版本中,您可以使用动态类型数据:

    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

3
如果您想显示CreationTime属性而不是LastWriteTime属性,那么可以将Get-ChildItem的输出导入到Select-Object中,并指定要选择哪些属性:
Get-ChildItem | Select Mode, CreationTime, Length, Name

0
请尝试执行以下 PowerShell 命令:
> get-childitem | sort-object -property name, creationtime, lastwritetime

您将会在 PowerShell 中看到如下结果:

 Directory: C:\project\CNN I\Maya                             

Mode                 LastWriteTime         Length Name 
----                 -------------         ------ ---- 
-a----         1/11/2021  11:36 PM          32187 ocean wake foam.JPG                                                   
-a----          1/6/2021  12:28 AM         121100 ocean_clean.ma                                                        
-a----          1/4/2021   3:59 PM         122199 ocean_clean_1.ma           

你看不到创建日期,但实际上它是按名称和创建日期 (creationtime) 排序,然后按修改日期 (lastwritetime) 排序的。如果你想验证这一点,请尝试使用 > get-childitem | sort-object lastwritetime 命令。它将按修改日期列出文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接