Friday, April 24, 2015

POWERSHELL SCRIPT TO IDENTIFY DOCUMENTS WITH LONG PATH NAMES

I wrote this PowerShell script to identify all documents in a site that exceed 256 characters in length.  See http://technet.microsoft.com/en-gb/library/ff919564(v=office.14).aspx for more information.
Param([String] $Site = $(Throw 'Error, no site URL specified.'))

<#
---------------------------------------------------------------------
PowerShell script to report on all files and folders within a site
that exceed 256 characters.  For more information, see the link
below.

File    : Get-LongPaths.PS1

Revision history:
-----------------
1.0 Initial version

Usage:
------
Run Get-LongPaths.PS1 <Site URL>
Example: Get-LongPaths.PS1 <a href="http://intranet/hr">http://intranet/hr</a>

---------------------------------------------------------------------
#>

# -----------------------
# Add required snap-in(s)
# -----------------------

Add-PSSnapin Microsoft.SharePoint.PowerShell -ea 0

# -----------------------
# Define global variables
# -----------------------

$Date = Get-Date -Format yyyy-MM-dd_h-mm
$Logging = "True"
$LogFile = "C:\Get-LongPaths $Date.log"

If ($Site.EndsWith("/"))
{
   $Site = $Site.Substring(0,$Site.Length-1)
}

# ------------------------------------------------------------------
# Function to report on folders that exceed 256 characters in length
# ------------------------------------------------------------------

Function Get-LongPaths([String]$Site)
{
   $Web = Get-SPWeb -Identity $Site

   ForEach ($List in $Web.Lists)
   {
      If ($List.BaseType -Eq "DocumentLibrary")
      {
         ForEach ($Item in $List.Items)
         {
            $FullURL = $Site + "/" + $Item.Url
            If ($FullURL.length -gt "256")
            {
               Write-Host $FullURL "-" $FullURL.Length
               If ($Logging -eq $True)
               {
                  $FullURL | Out-File -Append $LogFile
               }
            }
         }
      }
   }
}

Get-LongPaths $Site

No comments:

Post a Comment