Home > PowerShell > PowerShell Script To Get Path Lengths

PowerShell Script To Get Path Lengths

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!

  1. Kieran
    February 20th, 2014 at 05:27 | #1

    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.

  2. February 23rd, 2014 at 15:07 | #2

    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 🙁

  3. helper
    February 25th, 2014 at 03:51 | #3


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

  4. Kieran
    February 27th, 2014 at 04:22 | #4

    Ha!, of course 🙂

  5. Yomodo
    September 2nd, 2014 at 08:03 | #5

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


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

  6. stephano
    April 12th, 2015 at 02:38 | #6

    “Long Path Tool” program is the best solution for your problem

  1. No trackbacks yet.