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
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.
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.
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.