Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0powershell-remoting

Remove-Item not working when a file name is provided in a variable


I am very curious about this as it took me a while but I couldn't figure it out

First, I ran the following script to get all Zip files in a dir

$entryList = New-Object System.Collections.ArrayList

Get-ChildItem -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName" -ErrorAction Stop | sort -Property "LastWriteTime" | ForEach-Object {
    if($_.Name.Contains(".zip")) {
            $entryList.Add($_.Name) | Out-Null
        }
    }

it showed as below:

2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy.zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (2).zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (3).zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy.zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (6).zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy (2).zip

Then I tried to delete the first one(2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy.zip) with remove-item like this:

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\$entryList[0]" -ErrorAction Stop

Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:1
+ Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (\\toolsbackup....lback\autopatch:String) [Remove-Item], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

I got a path too long exception. However, if I put the file name in "Remove-Item" instead of passing it by $entryList[0], it worked

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (2).zip" -ErrorAction Stop

Solution

  • Your issue is using '$entryList[0]' in your quoted string.

    Run this code to see how this works (or doesn't work)...

    $entryList = New-Object System.Collections.ArrayList
    $entryList.Add("This is an entry.")
    
    "Broken"
    # This is a string with: This is an entry.[0]
    Write-Output "This is a string with: $entryList[0]"
    
    "Fixed1"
    # This is a string with: This is an entry.
    Write-Output "This is a string with: $($entryList[0])"
    
    # or...
    "Fixed2"
    # This is a string with: This is an entry.
    $item = "This is a string with: {0}" -f $entryList[0]
    Write-Output $item
    

    You can try something like:

    Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\$($entryList[0])" -ErrorAction Stop
    

    Also, instead of using Name, you might refactor your code to use FullName...

    $entryList.Add($_.FullName)
    

    Enjoy.