PowerShell Script To Get Path Lengths

1 minute read October 24, 2013

A while ago I created a Path Length Checker tool in C# that has a “nice” GUI, and put it up on CodePlex. One of the users reported that he was trying to use it to scan his entire C: drive, but that it was crashing. Turns out that the System.IO.Directory.GetFileSystemEntries() call was throwing a permissions exception when trying to access the “C:\Documents and Settings” directory. Even when running the app as admin it throws this exception. In the meantime while I am working on implementing a workaround for the app, I wrote up a quick PowerShell script that the user could use to get all of the path lengths. That is what I present to you here.

$pathToScan = "C:\Some Folder"  # The path to scan and the the lengths for (sub-directories will be scanned as well).
$outputFilePath = "C:\temp\PathLengths.txt" # This must be a file in a directory that exists and does not require admin rights to write to.
$writeToConsoleAsWell = $true   # Writing to the console will be much slower.

# Open a new file stream (nice and fast) and write all the paths and their lengths to it.
$outputFileDirectory = Split-Path $outputFilePath -Parent
if (!(Test-Path $outputFileDirectory)) { New-Item $outputFileDirectory -ItemType Directory }
$stream = New-Object System.IO.StreamWriter($outputFilePath, $false)
Get-ChildItem -Path $pathToScan -Recurse -Force | Select-Object -Property FullName, @{Name="FullNameLength";Expression={($_.FullName.Length)}} | Sort-Object -Property FullNameLength -Descending | ForEach-Object {
    $filePath = $_.FullName
    $length = $_.FullNameLength
    $string = "$length : $filePath"

    # Write to the Console.
    if ($writeToConsoleAsWell) { Write-Host $string }

    #Write to the file.

Happy coding!



Hi Daniel, Thanks for the script - it is exactly what I’m looking for, but I find when I run it, as soon as it comes up against the 260+ pathlenghts I get the usual 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.”

When I look at the output log it starts at 259 characters as the 260+ ones fail. I’m using PowerShell 4 on server 2012 R2.

Thanks for any help. Kieran.


@Kieran I tried to setup some test data to let me try and recreate this issue to find a fix for it, but I can’t get Windows to allow me to create a path longer than 260 characters, so I’m not able to test any workarounds :(



there is somthing about to get over 260 characters .. hope it help


Hi, this WILL help you. Get AlphaFS.dll: https://alphafs.codeplex.com/SourceControl/list/changesets


Import-Module [Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $True)

Leave a Comment

Your email address will not be published. Required fields are marked *