我正在尝试在目录树中search2个文件并删除它们。 我想保留已被删除的文件的path。
到目前为止,这是我的:
#set the path to search for the file $path = ".\" #File name $file = "GeneratedCode.cs" $file2 = "TypedEnums.cs" #Look for the file and delete every instance of it in all directories. Get-Childitem $path -include $file -recurse | foreach ($_) {remove-item $_.fullname} #Look for the file and delete every instance of it in all directories. Get-Childitem $path -include $file2 -recurse | foreach ($_) {remove-item $_.fullname}
编辑:我忘了提及,我想删除文件的每个实例。
我会将Get-ChildItem
cmdlet的结果存储到一个变量,比如$filesToDelete
。 然后,您可以简单地将对象传递给Remove-Item
cmdlet(您不需要那里的foreach
):
$filesToDelete = Get-Childitem $path -include $file -recurse $filesToDelete | Remove-Item