看板 Windows 關於我們 聯絡資訊
作業系統:Server 2012R2 PowerShell 4.0 問題內容: 各位好 我現在要用powershell移除掉一個目錄裡面超過30天未使用的檔案及資料夾 但是需要保留幾個子目錄及底下的檔案 |-C:\TEST \-C:\TEST\A \-C:\TEST\A\A1 \-C:\TEST\A\A2.txt \-C:\TEST\B \-C:\TEST\B\B1 \-C:\TEST\B\B2.txt \-C:\TEST\C \-C:\TEST\C\C1 \-C:\TEST\C\C2.txt \-C:\TEST\D.doc \-C:\TEST\E.txt 除了A跟B資料夾外的檔案,超過30天未使用必須刪除 我嘗試了exclude跟.Fullname -notlike都沒有成功 請教一下各位該怎麼寫? 謝謝 #notlike $before=(Get-Date).AddDays(-30) $exfolder=('C:\TEST\A*' , 'C:\TEST\B*') Get-ChildItem -path 'C:\TEST' -Recurse Where-Object {($_.LastWriteTime -lt $before ) -and ( $_.FullName -notlike $exfolder)} | Remove-Item -force -Recurse #exclude $before=(Get-Date).AddDays(-30) $exfolder=('C:\TEST\A*' , 'C:\TEST\B*' ) Get-ChildItem -path 'C:\TEST' -Recurse -Exclude $exfolder Where-Object { $_.LastWriteTime -lt $before } | Remove-Item -force -Recurse -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.146.10.18 ※ 文章網址: https://www.ptt.cc/bbs/Windows/M.1538020557.A.CE9.html