【发布时间】:2017-03-28 10:51:51
【问题描述】:
我是 PowerShell 新手,我创建了以下代码来删除特定文件和文件夹:
$myFolderPath = "C:\Test"
$myLimit = (Get-Date).AddDays(-14)
# Delete files according to filter.
Get-ChildItem -Path $myFolderPath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $myLimit} | Remove-Item -Force
# Delete empty folders.
Get-ChildItem -Path $myFolderPath -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
是否可以在执行实际的 Remove-Item 操作之前将要删除的每个项目的完整路径打印到控制台?
我猜……必须在这里添加....
... | Remove-Item -Force
这里……
... | Remove-Item -Force -Recurse
但我不知道如何以优雅的方式实现它(没有代码重复)。
【问题讨论】:
-
我猜这是how-to-delete-empty-subfolders-with-powershell 的编辑不当的副本。无论如何,当文件夹被删除时,打印文件夹有什么用。也许你应该看看 Remove-Item 的 -whatif 选项
-
我想将它们打印到控制台,以便能够查看大树中的哪些文件夹已被例程删除。
-
感谢您对 -whatif 选项的提示。但这不是我需要的。
标签: powershell