Powershell基于CSV删除文件

Powershell基于CSV删除文件,powershell,Powershell,我正在尝试删除基于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

我正在尝试删除基于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
}

根据您的评论,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"
  }
}

你的代码删除了我所有的文件。幸运的是,我再次做了一个测试,c:\temp文件夹不是实际的文件夹。因此,我更改了代码以删除Item-verbose$filename,但出现以下错误。删除项:无法将参数绑定到参数“Path”,因为它是一个空字符串。很抱歉,Swoogan,我并不是想否定它。你的解决方案很有魅力。您是对的,csv文件中有一些空行。我需要确保没有空行。再次感谢,没有冒犯。我很高兴你能单独工作。但这是一个很好的教训;PowerShell cmdlet通常使用空字符串表示所有内容。我以前也被这件事弄得焦头烂额。