Search code examples
powershellarraylistspecial-characters

Powershell ArrayList remove item with special characters


I do this

[System.Collections.ArrayList]$packageList = (Get-ChildItem "\\path-to-files" -Filter *.zip )
Write-Host $packageList
$packageList.Remove("ABC_Export-Cars.zip")
Write-Host $packageList

Remove doesn't work with this kind of item nevertheless it's in the array. With a simple array e.g. containing "A","B","C" it works. Are "_" or "-" characters which need a special processing?


Solution

  • The problem is that your ArrayList contains a collection of FileInfo objects returned by Get-ChildItem. So none of those items match the string you're trying to remove. Obviously when you use a collection of strings (a,b,c) then you can match a string for removal.

    You probably just want the names of the files in your ArrayList, in which case do this:

    [System.Collections.ArrayList]$packageList = (Get-ChildItem "\\path-to-files" -Filter *.zip).name
    

    The .Remove operation then should work for a specified file name.