Powershell,尝试仅输出目录上的路径和lastwritetime

ome*_*men 2 powershell

我正在尝试编写一个脚本,该脚本将输出90天内未更改的任何目录.我希望脚本显示整个路径名和lastwritetime.我写的脚本只显示路径名,但不显示lastwritetime.下面是脚本.

Get-ChildItem | Where {$_.mode -match "d"} | Get-Acl | 
    Format-Table @{Label="Path";Expression={Convert-Path $_.Path}},lastwritetime
Run Code Online (Sandbox Code Playgroud)

当我运行此脚本时,我得到以下输出:

Path                                                        lastwritetime
----                                                        ----------
C:\69a0b021087f270e1f5c
C:\7ae3c67c5753d5a4599b1a
C:\cf
C:\compaq
C:\CPQSYSTEM
C:\Documents and Settings
C:\downloads

我发现get-acl命令没有lastwritetime作为成员.那么如何才能获得路径和lastwritetime所需的输出?

Kei*_*ill 6

您不需要使用Get-Acl和perf来使用$ _.PSIsContainer而不是在Mode属性上使用正则表达式匹配.试试这个:

Get-ChildItem -Recurse -Force | 
    ? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} | 
    Format-Table FullName,LastWriteTime -auto
Run Code Online (Sandbox Code Playgroud)

您可能还想使用-Force列出隐藏/系统目录.要将此数据输出到文件,您有以下几种选择:

Get-ChildItem -Recurse -Force | 
    ? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} | 
    Select LastWriteTime,FullName | Export-Csv foo.txt
Run Code Online (Sandbox Code Playgroud)

如果您对CSV格式不感兴趣,请尝试以下方法:

Get-ChildItem -Recurse -Force | 
    ? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} | 
    Foreach { "{0,23} {1}" -f $_.LastWriteTime,$_.FullName} > foo.txt
Run Code Online (Sandbox Code Playgroud)

还可以尝试使用Get-Member查看文件和目录上的属性,例如:

Get-ChildItem $Home | Get-Member
Run Code Online (Sandbox Code Playgroud)

并且看到所有值都这样做:

Get-ChildItem $Home | Format-List * -force
Run Code Online (Sandbox Code Playgroud)