我正在尝试删除基于csv文件的一堆文件。应从目标文件共享中删除csv文件中列出的文件。当我运行脚本时,我在目标文件夹中,但是我收到以下错误:
Remove-Item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties
do not match any of the parameters that take pipeline input.
At line:9 char:56
+ Get-Childitem | where {$_.Name -match $filename} | Remove-Item -verbose -$fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (gp.html:PSObject) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.RemoveItemCommand
以下代码:
$csvFile = Import-csv "C:\\DeleteFiles.csv"
foreach($user in $csvFile)
{
$filename = $user.FileName
write-host $filename
Get-Childitem | where {$_.Name -match $filename} | Remove-Item -verbose -$fileName
}
答案 0 :(得分:1)
根据您的评论,您在csv文件中有一些空文件名。这个解决方案应该更加强大:
$csvFile = Import-csv "C:\\DeleteFiles.csv"
foreach($user in $csvFile)
{
$filename = $user.FileName
write-host "The filename is '$filename'"
if (test-path $filename)
{
Remove-Item -verbose $fileName
}
else
{
Write-Host "File '$filename' not found"
}
}