Long Paths
PowerShell, actually .Net on which it is built, has a limitation that no directory path can be longer than 248 characters and no file path longer than 260. See http://blogs.msdn.com/b/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx As a result if you try to use powershell to traverse long paths you will get an error. "Get-ChildItem : 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." There is no way to get around this limitation with native powershell.
Before we begin, it should be stated, the ultimate solution is to shorten long directory and filenames. Even though Windows NTFS can handle the long paths, it does slow file handling down.
Some workarounds.
- One technique for getting around the problem is to map a drive or UNC path to a place deep in the directory structure. All these methods are a kludge at best.
- Within PowerShell you can create an entry point deep into a directory structure with new-psdrive. For example, the following command creates a PowerShell drive which can be accessed by MyDocs:.
New-PSDrive -Name MyDocs -PSProvider FileSystem -Root "C:\Documents and Settings\User01\My Documents" -Description "Maps to my My Documents folder."
- The subst.exe program can be used to create fake drive letters deep in the directory structure and access the directory through the created drive.
- Finally shares can be created with the net command and these can be accessed with through the UNC path.
- Within PowerShell you can create an entry point deep into a directory structure with new-psdrive. For example, the following command creates a PowerShell drive which can be accessed by MyDocs:.
- If you are simply listing, copying, or moving files use the Microsoft Utility Robocopy. http://technet.microsoft.com/en-us/library/cc733145.aspx For example the following command will create a directory listing of all exe files on x: created in the last 6 days.
robocopy x:\ d:\ "*.exe" /S /MaxAGE:6 /L /NS /NC /NDL /LOG:X_FileExport.txt
No files will be copied. - Another nice tool, useful for getting file lists including information such as attributes, owner, and hashes, is filelist from jam-software. http://www.jam-software.com/filelist/ The following example returns all the exe files on the z: drive created since 12-20-2014 including the owner and md5 hash.
.\FileList.exe /filter *.exe /fullpath /md5 /mindate 2014-12-20 /owners z:\
- If you need to delete files with a long path just resort to the command shell and del.