2009-11-05 26 views
2

我想編寫一個腳本,將輸出,至今尚未超過90天更換任何目錄的路徑和lastwritetime。我希望腳本顯示整個路徑名和lastwritetime。我寫的腳本僅顯示路徑名稱,但不顯示最後寫入時間。以下是腳本。PowerShell中,試圖輸出目錄上只

Get-ChildItem | Where {$_.mode -match "d"} | Get-Acl | 
    Format-Table @{Label="Path";Expression={Convert-Path $_.Path}},lastwritetime 

當我運行該腳本,我得到下面的輸出:

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

我發現了get-acl命令不lastwritetime有作爲成員。那麼我怎麼才能得到所需的輸出只有路徑和lastwritetime?

回答

6

你不需要使用Get-ACL和PERF使用$ _。PSIsContainer而不是使用上Mode屬性正則表達式匹配。試試這個:

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

您可能還想使用-Force列出隱藏/系統目錄。要輸出這個數據到一個文件,你有幾種選擇:

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

如果你不感興趣的CSV格式試試這個:

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

也可以嘗試使用Get-Member來看看屬性是什麼文件&迪爾斯如:

Get-ChildItem $Home | Get-Member 

而且看到所有值做到這一點:

Get-ChildItem $Home | Format-List * -force 
+0

LastWriteTime更改子項(文件及迪爾斯)隻影響包含這些項目的目錄LastWriteTime。 – 2009-11-05 18:57:56

+0

將-Recurse參數添加到Get-ChildItem可解決該特定問題。 – 2009-11-05 18:59:35

+0

是的,我發佈後,我發現了關於-recurse選項的問題。但還有一個問題,我會擺脫你的頭髮。 即使是格式表有自動選項,當你出把數據傳輸到lastwritetime列被丟棄的文件。此外,使用換行選項會使輸出看起來有些雜亂。 我相信格式表會自動獲取屏幕的列寬並將其放入文件中。 是否有可能使用英尺命令創建自定義的列寬? – 2009-11-05 19:25:42