I am trying to delete bunch of files based on a csv file. Files listed in the csv file should be deleted from the target file share. When I run the script I am in the target folder but I am getting the following error:
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
Code below:
$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
}
According to your comment, you have some empty filenames in the csv file. This solution should be more robust:
$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"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.