Powershell to find URL lengths greater than 260 characters

I need the Powershell to find URL lengths greater than 260 characters, I've tried using scripts from both the sites below.  1 doesn't output to a text file and the other tries to locate all files in the top level site and anything beneath it even when i specify a certain URL such as http://site1/subsite/siteb

https://sharepointnomad.wordpress.com/2010/07/31/locating-files-and-pages-with-urls-that-are-too-long-for-sharepoint/

http://www.nikcraik.ca/extract-sharepoint-files-with-long-names/

# Call this script as below
# powershell   C:\scripts\url.ps1  "http://spdev/sandbox" 
# This script enumerates all files at the specified URL,
# and outputs full URL for each file,
# along with the length of the URL.
 
# define EnumPages function

function EnumPages
{
param ($URL, $objFolder)
 
 # enumerate files in the root folder
 
 foreach ($file in $objFolder.Files)
 {
 
 $result = "OK"
 $output = $URL + "/" + $objFolder.URL + "/" + $file.Name

#$output,  $URL ";" ,$objFolder.URL ,$file.Name |Out-File C:\Scripts\files.csv -append

 
# evaluate string length
 if ($output.length -ge 260) { $result = "TOO LONG" }
 
# write output
 
 # write-output $output, ";" , $output.length, ";" $result, ";" 

$output ,$output.length,$result |Out-File C:\Scripts\files.csv -append
 
}
 # enumerate subfolders
 
 foreach ($subfolder in $objFolder.SubFolders)
 {
 
 EnumPages $URL $subfolder
 }
 
 }
 
 # begin script body
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
 
 $siteURL = $args[0]
 
# create a new SPSite object
 
$site=new-object Microsoft.SharePoint.SPSite("http://sitename/site/project1")
$web = $site.OpenWeb() 

 # enumerate files in the Rootweb
 
 foreach ($file in $site.Rootweb.Files)
 {
 
$result = "OK"
 
$output = $site.Rootweb.URL + "/" + $file.Name 
#$output| Export-Csv c:/scripts/test.csv -NoTypeInformation
 
 if ($output.length -ge 260) { $result = "TOO LONG" }
 
  #write-host $output, ";", $output.length, ";" , $result

$output ,$output.length,$result |Out-File C:\longfilenames\files.csv -append

 
 }
 
 # enumerate folders in the Rootweb
 
 foreach ($folder in $site.Rootweb.Folders)
 {
 
 EnumPages $site.Rootweb.URL $folder
 
 }
 
 # enumerate subsites
 
 foreach ($web in $site.Allwebs)
 {
 
  # enumerate files in the root web
 
  foreach ($file in $web.Files)
  {
 
$result = "OK"
 
$output = $web.URL + "/" + $file.Name 
 
 if ($output.length -ge 260) { $result = "TOO LONG" }
 
 #write-host $output, ";", $output.length, ";" , $result

$output ,$output.length,$result |Out-File C:\Scripts\files.csv -append
 
  }
 
  # enumerate folders
 
  foreach ($folder in $web.Folders)
  {
 
  EnumPages $web.URL $folder
 
  }

 
   }
 


$site.Dispose(); ##ENFORCED DISPOSAL!!!    

June 19th, 2015 3:39am

I removed code for 1) Running on subsites and other sites in site collections 2)Used transcript to collect logs (being lazy):

#mentions output file path
$PAth = "D:\Output.txt"
Start-Transcript -Path $PAth

# This script enumerates all files at the specified URL,
# and outputs full URL for each file,
# along with the length of the URL.
# define EnumPages function
function EnumPages
{
    param ($URL, $objFolder) 
    # enumerate files in the root folder
    foreach ($file in $objFolder.Files)
    {
        $result = "OK"
        $output = $URL + "/" + $objFolder.URL + "/" + $file.Name
 
        # evaluate string length
        if ($output.length -ge 260) { $result = "TOO LONG" }
 
        # write output
 
        write-host $output, ";" , $output.length, ";" $result, ";"
    }
    # enumerate subfolders
 
    foreach ($subfolder in $objFolder.SubFolders)
    {
        EnumPages $URL $subfolder
    } 
}
 
# begin script body
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | Out-Null 
$siteURL = $args[0]
 
# create a new SPSite object 
$site=new-object Microsoft.SharePoint.SPSite($siteURL)
$web = $site.openweb() 

# enumerate files in the root web
foreach ($file in $web.Files)
{
    $result = "OK"
    $output = $web.URL + "/" + $file.Name 
    if ($output.length -ge 260) { $result = "TOO LONG" }
    write-host $output, ";", $output.length, ";" , $result 
}
 
# enumerate folders 
foreach ($folder in $web.Folders)
{
    EnumPages $web.URL $folder
}
  
$site.Dispose(); ##ENFORCED DISPOSAL!!!    

Stop-Transcript

Free Windows Admin Tool Kit Click here and download it now
June 19th, 2015 9:22am

Thanks Mohit, almost there.  Can it be changed so it only reports problem urls/files greater than 260 characters?  It currently reports everything but I'm only interested in problem files, it would help if it could be formatted in the output file on separate lines also.

thanks!

June 19th, 2015 9:52am

Removed these two lines of code for hiding un-problematic urls:

$result = "OK"
$output = $URL + "/" + $objFolder.URL + "/" + $file.Name

Use below code:

#mentions output file path
$PAth = "D:\Output.txt"
Start-Transcript -Path $PAth

# This script enumerates all files at the specified URL,
# and outputs full URL for each file,
# along with the length of the URL.
# define EnumPages function
function EnumPages
{
    param ($URL, $objFolder) 
    # enumerate files in the root folder
    foreach ($file in $objFolder.Files)
    { 
        # evaluate string length
        if ($output.length -ge 260) { $result = "TOO LONG" }
 
        # write output 
        write-host $output, ";" , $output.length, ";" $result, ";"
    }
    # enumerate subfolders 
    foreach ($subfolder in $objFolder.SubFolders)
    {
        EnumPages $URL $subfolder
    } 
}
 
# begin script body
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | Out-Null 
$siteURL = $args[0]
 
# create a new SPSite object 
$site=new-object Microsoft.SharePoint.SPSite($siteURL)
$web = $site.openweb() 

# enumerate files in the root web
foreach ($file in $web.Files)
{
    if ($output.length -ge 260) { $result = "TOO LONG" }
    write-host $output, ";", $output.length, ";" , $result 
}
 
# enumerate folders 
foreach ($folder in $web.Folders)
{
    EnumPages $web.URL $folder
}
  
$site.Dispose(); ##ENFORCED DISPOSAL!!!    

Stop-Transcript

I'm not sure what your other requirement means. You can try opening output file in IE or notepad++/wordpad to properly display output. It just an formatting issue.

Free Windows Admin Tool Kit Click here and download it now
June 19th, 2015 10:28am

Sorry this is the output file now which returns nothing?

Transcript started, output file is C:\powershell\Output.txt
 ;  ; 
 ;  ;  ;
 ;  ;  ;
 ;  ;  ;
 ;  ;  


  • Edited by MJ2012 Friday, June 19, 2015 2:37 PM
June 19th, 2015 10:39am

Sorry, my fault. Removed wrong lines in hurry.

This should work fine:

#mentions output file path
$PAth = "D:\Output.txt"
Start-Transcript -Path $PAth

# This script enumerates all files at the specified URL,
# and outputs full URL for each file,
# along with the length of the URL.
# define EnumPages function
function EnumPages
{
    param ($URL, $objFolder) 
    # enumerate files in the root folder
    foreach ($file in $objFolder.Files)
    {
        $output = $URL + "/" + $objFolder.URL + "/" + $file.Name
 
        # evaluate string length and output URLs with length greater than 260
        if ($output.length -ge 260) 
        { 
            $result = "TOO LONG"
            write-host $output, ";" , $output.length, ";" $result, ";"
        }
        
    }
    # enumerate subfolders
 
    foreach ($subfolder in $objFolder.SubFolders)
    {
        EnumPages $URL $subfolder
    } 
}
 
# begin script body
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | Out-Null 
$siteURL = $args[0]
 
# create a new SPSite object 
$site=new-object Microsoft.SharePoint.SPSite($siteURL)
$web = $site.openweb() 

# enumerate files in the root web
foreach ($file in $web.Files)
{
    $output = $web.URL + "/" + $file.Name 
    # evaluate string length and output URLs with length greater than 260
        if ($output.length -ge 260) 
        { 
            $result = "TOO LONG"
            write-host $output, ";" , $output.length, ";" $result, ";"
        }
}
 
# enumerate folders 
foreach ($folder in $web.Folders)
{
    EnumPages $web.URL $folder
}
  
$site.Dispose(); ##ENFORCED DISPOSAL!!!    

Stop-Transcript

Free Windows Admin Tool Kit Click here and download it now
June 19th, 2015 11:55am

Looking good thanks - sorry one last thing, it now outputs to 1 line at a time are you able to wrap to a new line for each url?
June 19th, 2015 12:45pm

and include the character amount in the output file e.g. http://sitename/document1.doc;265?  it shows on the screen but the output file just has URLs
Free Windows Admin Tool Kit Click here and download it now
June 19th, 2015 12:50pm

#mentions output file path
$PAth = "D:\Output.txt"

# This script enumerates all files at the specified URL,
# and outputs full URL for each file,
# along with the length of the URL.
# define EnumPages function
function EnumPages
{
    param ($URL, $objFolder) 
    # enumerate files in the root folder
    foreach ($file in $objFolder.Files)
    {
        $Output = $URL + "/" + $objFolder.URL + "/" + $file.Name
 
        # evaluate string length and output URLs with length greater than 260
        if ($Output.length -ge 260) 
        { 
            $result = "TOO LONG"
            "$Output `t $($Output.Length) `t $Result" | Out-File -LiteralPath $Path -Append
        }
        
    }
    # enumerate subfolders
 
    foreach ($subfolder in $objFolder.SubFolders)
    {
        EnumPages $URL $subfolder
    } 
}
 
# begin script body
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | Out-Null 
$siteURL = $args[0]
 
# create a new SPSite object 
$site=new-object Microsoft.SharePoint.SPSite($siteURL)
$web = $site.openweb() 

Write-Host "Full URL `t URL Length `t Result" | Out-File -LiteralPath $Path 

# enumerate files in the root web
foreach ($file in $web.Files)
{
    $Output = $web.URL + "/" + $file.Name 
    # evaluate string length and output URLs with length greater than 260
        if ($Output.length -ge 260) 
        { 
            $Result = "TOO LONG"
            "$Output `t $($Output.Length) `t $Result" | Out-File -LiteralPath $Path -Append
        }
}
 
# enumerate folders 
foreach ($folder in $web.Folders)
{
    EnumPages $web.URL $folder
}
  
$site.Dispose(); ##ENFORCED DISPOSAL!!!    

Lets make this a tab separated text file. I adjusted output a little bit so that you wont see anything on screen but all output is redirected to text file.

Here you go:

June 19th, 2015 1:43pm

I get no output now?

Have you tested this on something but maybe drop the -ge number down to 50 so you are able to get results?  thanks for your help with this

Free Windows Admin Tool Kit Click here and download it now
June 19th, 2015 2:36pm

It just doesn't report any problems URLs back now, I know there is a problem document that's full URL is 308 characters long from doing a SQL LEN()
June 19th, 2015 2:37pm

I'm not sure if you changed something. But output should have been generated in file D:\output.txt. I have already tested it one of the sites in my environment.

Or you can use below code to generate output for all urls and their length in D:\output.txt. Once you get output, you can use any spreadsheet software (like MS Excel) to easily filter it:

#mentions output file path
$PAth = "D:\Output.txt"

# This script enumerates all files at the specified URL,
# and outputs full URL for each file,
# along with the length of the URL.
# define EnumPages function
function EnumPages
{
    param ($URL, $objFolder) 
    # enumerate files in the root folder
    foreach ($file in $objFolder.Files)
    {
        $Output = $URL + "/" + $objFolder.URL + "/" + $file.Name
 
        # evaluate string length and output URLs with length greater than 260
        "$Output `t $($Output.Length)" | Out-File -LiteralPath $Path -Append
      
    }
    # enumerate subfolders
 
    foreach ($subfolder in $objFolder.SubFolders)
    {
        EnumPages $URL $subfolder
    } 
}
 
# begin script body
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | Out-Null 
$siteURL = $args[0]
 
# create a new SPSite object 
$site=new-object Microsoft.SharePoint.SPSite("http://projects.bmc.com/PWA/MP-DATA PRIVACY")
$web = $site.openweb() 

Write-Host "Full URL `t URL Length" | Out-File -LiteralPath $Path 

# enumerate files in the root web
foreach ($file in $web.Files)
{
    $Output = $web.URL + "/" + $file.Name 
    # evaluate string length and output URLs with length greater than 260
    "$Output `t $($Output.Length)" | Out-File -LiteralPath $Path -Append
}
 
# enumerate folders 
foreach ($folder in $web.Folders)
{
    EnumPages $web.URL $folder
}
  
$site.Dispose(); ##ENFORCED DISPOSAL!!!		
Free Windows Admin Tool Kit Click here and download it now
June 19th, 2015 2:58pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics