問題は、-Exclude
コマンドをこのスクリプトに挿入して、 '.pst'などのファイルの種類を指定しないようにすることです。 Where-Object
フィールドに$exclude
を含める方法を今すぐ確認しました。Remove-Itemを呼び出す前に特定のファイルを除外する方法
$limit = (Get-Date).AddDays(2555)
$path = "\\File Path"
$log = "C:\Log output"
$exclude = ".pst"
# Delete files older than the $limit. <Use -WhatIf when you want to see what files/folders will be deleted before>
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit} >$log
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } >> $log
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit}| Remove-Item -Force -WhatIf
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force -WhatIf
# Delete any empty directories left behind after deleting the old files. <Use -WhatIf when you want to see what files/folders will be deleted before>
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } >> $log
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse -WhatIf
いずれのアイデアも大歓迎です。
これは必要に応じて簡単に統合できます。どのPowerShellバージョンをお持ちですか?初心者には 'Tee-Object'を見てください。 – Matt
'Where-Object'まで待つのはなぜですか? Get-ChildItemで '-Exclude'を使わないのはなぜでしょうか?最後の行は空のフォルダを削除していますか? – Matt
PSVersion 4.0を使用しています。 – Josh