PowerShell Long File Paths

Unfortunately working in the tech world, you sometimes come across things that make you just scratch your head and go “huh?”. These head scratching moments typically are dumb restrictions in current operating systems which are a result of past decisions, and these limitations no longer have a reason to be present. But in the name of compatibility - we keep these limitations around. One such limitation is the max file length that windows applications (and in turn powershell) can work with. By default the max length is 256 characters in length, but with a few tricks, you are able to work with file paths which are way longer. I’m not gonna go into the technical reasons behind how all of this works but this link has some good background on the issue.

Instead what I want to do is document what I am currently using to bypass this 256 character limit of file paths in Windows.

All of the magic is contained in one module: NTFSSecurity, which can be downloaded from here. It is recommended that you download the module from this site instead of using Find/Install-Module as the powershell gallery does NOT have the latest version of the module as of 4/6/2016.

Once this module is installed it is recommended that you test to make sure the module is in fact fully loaded. I recommend this due to some issues I have noticed with the current 4.2 version of the module where sometimes the get-childitem2 portion of the module does not load while all other portions load without issue. The quickest and easiest way to check if the module is installed/loaded correctly is to use the command get-childitem2. If no error is returned and instead a list of folders/files is outputted to the screen, the module has been successfully installed/loaded. Once the module is confirmed working run:

get-command -module ntfssecurity | select name, commandtype

This will return a list of all commands available in the module. Looking at the list of commands available, there are commands which are eerily similar to well established/known commands. Examples of commands which have corresponding commands in NTFSSecurity are: get-childitem, copy-item, and remove-item. The usage of these commands are almost identical to their original command counterparts - but with a few important caveats. There are a few missing parameters and some parameters work differently in the NTFSSecurity version than the original. For example, get-childitem2 has a -path parameter but does not have a -literalpath parameter, instead the -path parameter works exactly like -literalpath from get-childitem. It is important to be aware of this as this influences how the command can/is used. For more differences, you can use get-help to get more information on that command.

Even with these slight differences in the commands, this module is extremely helpful for one important reason. The code behind the module was written to allow for the use of file paths greater than 256 characters in length. Under the hood this module is converting traditional ANSI based file paths(“\testps01\C$”) to unicode formated file paths(“\UNC\testops1\c$”). The whole reason this works is that NTFS itself can handle file names this long and Microsoft has implemented support for it in their API. Unfortunately due to Microsoft wanting to try to make everything backward compatible - this left the support for long file names something of a mystery as well as something that was very poorly supported unless you knew what you were looking for.