Get All Instances of a SharePoint Field in a Site Collection

Paste the below text into PowerShell or PowerShell ISE

You could even add the ability to output the array to CSV if you like.





#region INFO

########################
####   INFORMATION   ###
########################

<#
.DESCRIPTION
    This script will use an input parameter for a SharePoint field,
    using the internal field name, and display all the locations where it is instanced.
    It will even give you the URL to the property
.NOTES
    Author    : Eric Jochens
    Tags      : SharePoint
#>

#endregion INFO


#region PARAMETERS
[CmdletBinding()]
Param
(
    [Parameter(Mandatory=$true,HelpMessage="Enter the SharePoint Field Internal Name")][String]$FieldInternalName,
    [Parameter(Mandatory=$true,HelpMessage="Enter the SharePoint Field Display Name")][String]$FieldDisplayName,
    [Parameter(Mandatory=$true,HelpMessage="Enter the URL for the site collection we will search for fields")][String]$SiteCollectionUrl
)
#endregion PARAMETERS


#region SNAPINS

    Add-PSSnapin "Microsoft.SharePoint.PowerShell" -EA 0

#endregion SNAPINS


#region SCRIPT

####################
#####  Script  #####
####################

#System Variables
$StartTime = Get-Date
Write-Host ("Start Time:     " + $StartTime)
$DateStr = $StartTime.ToString("yyyy-MM-dd_HHmm")
Try
{
    $site = Get-SPSite -Identity $SiteCollectionUrl
    If($site){
        $arrayFields = @()
        Write-Host "Found site collection: " $site.Url -ForegroundColor Cyan
        $webs = $site.AllWebs
        ForEach($web in $webs){
            $webTitle = $web.Title
            $lists = $web.Lists
            ForEach($list in $lists){
                $listTitle = $list.Title
                ForEach ($field in ($list.Fields | Where-Object{$_.internalName -eq $FieldInternalName -or $_.Title -eq $FieldDisplayName} ))
                {
                    $listGUID = [uri]::EscapeDataString("{" + $list.ID.Guid + "}")
                    [String]$fieldUrl = $web.Url + "/_layouts/15/FldEdit.aspx?List=" + $listGUID + "&Field=" + $field.internalName
                    $objField = New-Object System.Object
                    $objField | Add-Member -MemberType NoteProperty -Name "SiteURL" -Value $site.Url
                    $objField | Add-Member -MemberType NoteProperty -Name "WebTitle" -Value $webTitle
                    $objField | Add-Member -MemberType NoteProperty -Name "ListTitle" -Value $listTitle
                    $objField | Add-Member -MemberType NoteProperty -Name "FieldDisplayName" -Value $field.Title
                    $objField | Add-Member -MemberType NoteProperty -Name "FieldInternalName" -Value $field.internalName
                    $objField | Add-Member -MemberType NoteProperty -Name "FieldType" -Value $field.Type
                    $objField | Add-Member -MemberType NoteProperty -Name "FieldURL" -Value $fieldUrl
                    $arrayFields += $objField
                }#fields
            }#lists
            $web.Dispose()
        }#webs
    }#site
    $arrayFields | Format-Table -Property WebTitle,ListTitle,FieldDisplayName,FieldInternalName,FieldType,FieldURL -AutoSize
}# try
Catch [system.exception]
{
    While($_.Exception.Message){Write-Error $_.Exception.Message}
    While($_.Exception.InnerException){Write-Error $_.Exception.InnerException}
    Break
}
Finally
{
$site.Dispose()
    $EndTime = Get-Date
$TimeToComplete = $EndTime - $StartTime
Write-Host ("Script Complete.")
Write-Host ("End Time       : " + $EndTime)
Write-Host ("Total Elapsed  : " + $TimeToComplete.Minutes + " min " + $TimeToComplete.Seconds + " sec")
    [System.GC]::Collect()
}

#endregion SCRIPT

Comments

Popular posts from this blog

SharePoint Designer 2013 Approval Workflow with Comments

SharePoint Search - Content Processing Pipeline Failed to Process the Item

Change SharePoint server hostname and Web Application Names