导入混乱的CSV文件名



我只想从文本文件中获取一个文件名列表并将其删除。我有一些非常简单的代码,但不知怎么的出了问题。

$files = Import-Csv C:tempPSduplicate3.csv
foreach ($file in $files) {  
Remove-Item -LiteralPath $file -WhatIf      
}
write-host -foregroundcolor blue  "Delete complete"

csv文件中只有一个路径列表,如

C:temptest.txt
D:PhotosCameraIMG_20190523_151236(1).jpg

我得到错误,每行

删除项目:找不到驱动器。名为"@{C"的驱动器不存在。

发生了什么,我该如何解决?

如果使用Import-Csv,命令将假定第一行是标题行。如果第一行中没有页眉,则需要指定它。然后,如果以后要引用列,则必须指定正确的页眉。Import-Csv被设计用于处理一个表,因此假设每一行都有多个属性。

$files = Import-Csv C:tempPSduplicate3.csv -Header Path
foreach ($file in $files) {  
Remove-Item -LiteralPath $file.Path -WhatIf      
}
write-host -foregroundcolor blue  "Delete complete"

如果我正确理解您的文件格式,您可能应该使用Get-Content而不是Import-Csv

$files = Get-Content C:tempPSduplicate3.csv
foreach ($file in $files) {  
Remove-Item -LiteralPath $file -WhatIf      
}
write-host -foregroundcolor blue  "Delete complete"