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