This is a simple function that uses pinvoke and PowerShell to remove a file that is buried in a folder structure in the file system that would normally cause an error about the path being too long. You can simply supply the full path and file to the function and it will be removed. This is a work in progress function that I hope to expand on in the future to do do more than just delete a file as it cannot delete a folder currently.
This function is featured the following Hey, Scripting Guy! article: http://blogs.technet.com/b/heyscriptingguy/archive/2015/12/27/weekend-scripter-remove-a-long-path-file.aspx
Updated January 7, 2016: This is now Remove-Item2 and fully supports removing directories (including child files/folders) and files.
Remember to dot source this script to load the function into the current session.
. .\Remove-Item2.ps1
. .\Remove-Item2.ps1
Help Examples:
<#
.SYNOPSIS
Gets the files and folders in a file system drive beyond the 256 character limitation
.DESCRIPTION
Gets the files and folders in a file system drive beyond the 256 character limitation
.PARAMETER Fullname
Path to a folder/file
.NOTES
Name: Remove-Item2
Author: Boe Prox
Version History:
1.0 //Boe Prox
- Initial version
.EXAMPLE
Remove-Item2 -Fullname C:\SomeLongPath
Description
-----------
Removes SomeLongPath and all of its children
#>
<#
.SYNOPSIS
Gets the files and folders in a file system drive beyond the 256 character limitation
.DESCRIPTION
Gets the files and folders in a file system drive beyond the 256 character limitation
.PARAMETER Fullname
Path to a folder/file
.NOTES
Name: Remove-Item2
Author: Boe Prox
Version History:
1.0 //Boe Prox
- Initial version
.EXAMPLE
Remove-Item2 -Fullname C:\SomeLongPath
Description
-----------
Removes SomeLongPath and all of its children
#>