Remove-Item: the specified path, file name are too long

We have many deep long directory structure that we need to delete but the file path is too long to delete. how can I work around this with PowerShell. Any ideas.
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:28

  • Get-ChildItem | Remove-Item <<<<
    • CategoryInfo : WriteError: (Z:\Folder:String) [Remove-Item], PathTooLongException
    • FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

The 255-character limit is actually embedded in .NET, which PowerShell is written in, so you can’t really work around it. You could potentially call Cmd.exe, and have it run the Del command - that gets you out of PowerShell. Another option is to map a PSDrive (New-PSDrive) somewhere into your path, so that instead of deleting C:\Really\Long\Path\That\Will\Not\Work, you could map Short: to \Really\Long\Path and then delete Short:\That\Will\Not\Work instead. I’d suggest the latter approach - mapping a drive into the directory structure.

thanks!

File name too long is a common issue, and I also had it before, but I figured out under the help of long path tool.

Don, this comes up so often, maybe it needs to be added to “The Big Book of PowerShell Gotchas.”

Could you not use rename-item as part of a script to remove the file ? For example if you do a count on the file and if bigger than 255 in character length rename to a shortened name and if you have more than one file with this length increment a digit each time.
Then remove the file/files.
Not sure if its possible as a workaround, not had chance to test.

Yes that works, just tried it. Put the long file name into a variable.

So in my example i created a 255 character text file called 'aaaaaaaaaaa…txt", you get the gist !!

$long = gci “aaaaaa*.txt” which of course puts the file into a variable.

Then did the rename,
Rename-Item $long -NewName Smaller.txt

Which allowed me to remove the file,
Remove-Item “smaller.txt”

As i said, if you have more than one could wrap them in a foreach loop and increment them.

i utilize alphafs for this task, here is the quick script i wrote to utilize an input file to delete multiple folders that exceed the 255 path

$path         = Split-Path -parent $MyInvocation.MyCommand.Definition
#insert full path to the alphafs.dll file for the import-module
Import-Module AlphaFS.2.0.1\lib\net45\AlphaFS.dll
$input  = $path + "\FolderDelete.csv"
$csv = Import-Csv $input
$log = $path + "\delete_log.txt"      

Start-Transcript $log -append
			
ForEach ($item in $csv)
	{
	try
		{
		$path = $item.path
		
		Write-Host "Deleting $($item.path)"
		[Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $true, $true)
		}
	catch
		{
		Write-Host "Error deleting $($item.path)"
		}
	}

Long Path Tool

Long path tool is the best solution for your problem. try it and solve your problem.

Path Too Long Auto Fixer finds and auto corrects long file names and paths to fit under the 260 character limit. https://pathtoolongautofixer.blogspot.com

I have recently used a program called Long Path Tool & i am so happy with the results that this particular program does. I have more than 50 GB of duplicate files in my computer and i was going crazy with the hard disk space that was being consumed and thanks to the “Long Path Tool” where i don’t need to search each & every file to delete. Long Path Tool did the job for me within a very short period of time. So no need to google each problem to remove duplicate & unwanted files, just grab the program called Long Path Tool.