如何在调用Remove-Item之前排除特定文件

时间:2016-07-11 18:35:40

标签: file powershell powershell-v4.0

我遇到的问题是在此脚本中插入-Exclude命令以帮助避免文件类型,例如' .pst'或任何其他指定的。我现在确定如何在$exclude字段中加入Where-Object

$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 

非常感谢任何想法。

1 个答案:

答案 0 :(得分:0)

To answer your specific question you could add another clause into your Where-Object that looks at the file extension. Note this works because you have only the one extension. If you wanted to add more you would need to change the operator.

Get-ChildItem -Path $path -Recurse -Force | 
    Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit -and $_.Extension -ne $exclude } > $log

However there are better options available that you should look at in your code. Letting the windows file system do most of the work instead of post processing with Where-Object could save you time and complexity. You can even combine your first couple of lines depending. Since you have v4 you can use the -File and -Directory switches to only pull those respective items.

Get-ChildItem -Path $path -Recurse -Force -File | 
    Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} | 
    Add-Content $log

While not exactly what your first couple of lines were doing I think it does what you meant it to do. Note the -File switch and combined date clauses.

If you wanted to log what you were removing you can also remove some repetition with Tee-Object (Not the only way to address this)

Get-ChildItem -Path $path -Recurse -Force -File | 
    Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} | 
    Tee-Object -FilePath $log | 
    Remove-Item -Force -WhatIf

I don't know where you need it but you can also just use -Exclude of Get-ChildItem to omit pst files.

Get-ChildItem -Path $_.FullName -Exclude $exclude -Recurse -Force