使用CreationTime而不是LastWriteTime获取Get-ChildItem?

sil*_*lla 9 powershell

如果你使用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
Run Code Online (Sandbox Code Playgroud)

没关系.我现在只想将LastWriteTime输出更改为CreationTime.其他一切都应该是一样的.有任何想法吗?

Sha*_*evy 5

您可以使用Select-Object或任何Format-*cmdlet 选择它

Get-ChildItem | Select-Object Mode,CreationTime,Length,Name
Run Code Online (Sandbox Code Playgroud)


lat*_*kin 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>
Run Code Online (Sandbox Code Playgroud)