如何在调用 Remove-Item 之前排除特定文件
How can I exclude a specific file before calling Remove-Item
我遇到的问题是将 -Exclude
命令插入此脚本以帮助避免使用“.pst”或任何其他指定的文件类型。我刚刚确定如何在 Where-Object
字段中包含 $exclude
。
$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
非常感谢任何想法。
要回答您的具体问题,您可以在 Where-Object
中添加另一个子句来查看文件扩展名。请注意,这是有效的,因为您只有一个扩展名。如果您想添加更多内容,则需要更改运算符。
Get-ChildItem -Path $path -Recurse -Force |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit -and $_.Extension -ne $exclude } > $log
但是,您应该在代码中查看更好的选项。让 windows 文件系统完成大部分工作而不是 post 使用 Where-Object
处理可以节省您的时间和复杂性。您甚至可以根据需要组合前几行。因为你有 v4,你可以使用 -File
和 -Directory
开关来只拉那些相应的项目。
Get-ChildItem -Path $path -Recurse -Force -File |
Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} |
Add-Content $log
虽然不完全是您前几行的意思,但我认为它确实按照您的意思去做。请注意 -File
开关和组合日期子句。
如果您想记录您删除的内容,您还可以使用 Tee-Object
删除一些重复(不是解决此问题的唯一方法)
Get-ChildItem -Path $path -Recurse -Force -File |
Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} |
Tee-Object -FilePath $log |
Remove-Item -Force -WhatIf
我不知道你在哪里需要它,但你也可以只使用 -Exclude
或 Get-ChildItem
来省略 pst 文件。
Get-ChildItem -Path $_.FullName -Exclude $exclude -Recurse -Force
我遇到的问题是将 -Exclude
命令插入此脚本以帮助避免使用“.pst”或任何其他指定的文件类型。我刚刚确定如何在 Where-Object
字段中包含 $exclude
。
$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
非常感谢任何想法。
要回答您的具体问题,您可以在 Where-Object
中添加另一个子句来查看文件扩展名。请注意,这是有效的,因为您只有一个扩展名。如果您想添加更多内容,则需要更改运算符。
Get-ChildItem -Path $path -Recurse -Force |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit -and $_.Extension -ne $exclude } > $log
但是,您应该在代码中查看更好的选项。让 windows 文件系统完成大部分工作而不是 post 使用 Where-Object
处理可以节省您的时间和复杂性。您甚至可以根据需要组合前几行。因为你有 v4,你可以使用 -File
和 -Directory
开关来只拉那些相应的项目。
Get-ChildItem -Path $path -Recurse -Force -File |
Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} |
Add-Content $log
虽然不完全是您前几行的意思,但我认为它确实按照您的意思去做。请注意 -File
开关和组合日期子句。
如果您想记录您删除的内容,您还可以使用 Tee-Object
删除一些重复(不是解决此问题的唯一方法)
Get-ChildItem -Path $path -Recurse -Force -File |
Where-Object {$_.LastWriteTime -lt $limit -and $_.CreationTime -lt $limit} |
Tee-Object -FilePath $log |
Remove-Item -Force -WhatIf
我不知道你在哪里需要它,但你也可以只使用 -Exclude
或 Get-ChildItem
来省略 pst 文件。
Get-ChildItem -Path $_.FullName -Exclude $exclude -Recurse -Force