且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

powershell - 使用文件名列表删除文件

更新时间:2022-06-08 07:21:24

$targetFolder = "D:\TEST_123"
$fileList = "D:\DeleteList.txt"

Get-ChildItem -Path "$targetFolder\*" -Recurse -Include @(Get-Content $fileList) | Remove-Item -Verbose

要使 -Include 工作,您应该在删除列表中的文件夹名称和文件名的末尾指定 \*.上面的代码对我有用,只删除文件夹及其所有子文件夹中的指定文件.

For -Include to work you should specify \* at the end of a folder name and filename with extension in your deletion list. The code above works for me, deleting only specified files in folder and all of its subfolders.

我还使用了 -Verbose 而不是 foreachWrite-Host.

I also used -Verbose instead of foreach and Write-Host.