PowerShell Script To Get A List of All Checked - Out Files

Objective

This purpose of this document is to mention the steps to get a list of all the checked out files in a web application. The reusable script for this job is also attached with this document. This script offers,

  • To select the a web application to operate on
  • To generate a report in CSV format, which will be helpful for excel reporting.

Business Case

S. No.Business Case
1Before we go ahead and start doing migration we need to extract many report and insights about the current SharePoint environment. One of those need is to get a list of checked out files in your SharePoint. This script will allow administrator to do this job easily.

Targeted Audience

  • SharePoint Application Developers
  • SharePoint Administrator
  • SharePoint Architect

Offerings

  • One reusable PowerShell script is provided which needs to be run to get a list of Checked out files.
  • Generate a report in excel format which includes information like

    • Site Collection Title
    • Site Collection URL
    • Web Title
    • Web URL
    • Library Name
    • File Name
    • File URL
    • Last Modified
    • Checked-out By
    • Checked-out By Email Address
    • Primary Admin
    • Primary admin Email
    • Secondary Admin
    • Secondary Admin Email

Technical Details

Below are the technical details for this PowerShell script,

  1. Execution

    Prerequisite:

    Login to SharePoint Server as Farm Administrator and copy the required files as per your requirement.

    Run:

    • Run the PowerShell Script as “Run as Administrator“.
    • Browse the folder path where you have kept this PowerShell script file and execute a command.

      command

PowerShell Script

  1. ##########################################################################################################################  
  2. ######## V 1.0  
  3. ######## PowerShell Script to get a list of checked out files in your SharePoint Environment  
  4. ################################################################################################################################  
  5.  
  6. #check to see if the PowerShell Snapin is added  
  7. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {  
  8.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
  9. }  
  10.  
  11. ## SharePoint DLL   
  12. [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")   
  13. $global:currentPhysicalPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path  
  14.    
  15. Function Get-SPWebApplication()  
  16. {    
  17.   Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )  
  18.   return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)  
  19. }  
  20.    
  21. Function global:Get-SPSite()  
  22. {  
  23.   Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL )  
  24.    
  25.    if($SiteCollURL -ne '')  
  26.     {  
  27.         return new-Object Microsoft.SharePoint.SPSite($SiteCollURL)  
  28.     }  
  29. }  
  30.     
  31. Function global:Get-SPWeb()  
  32. {  
  33.     Param( [Parameter(Mandatory=$true)] [string]$SiteURL )  
  34.     $site = Get-SPSite($SiteURL)  
  35.     if($site -ne $null)  
  36.     {  
  37.         $web=$site.OpenWeb();  
  38.     }  
  39.    return $web  
  40. }  
  41. #EndRegion  
  42.    
  43.  Function GetCheckedOutFiles([string]$WebAppURL)  
  44.  {   
  45.     try  
  46.     {  
  47.         $results = @()  
  48.          
  49.         #Get the Web Application  
  50.         $WebApp=Get-SPWebApplication($WebAppURL)  
  51.   
  52.         #Arry to Skip System Lists and Libraries  
  53.         $SystemLists =@("Converted Forms""Master Page Gallery""Customized Reports""Form Templates",   
  54.                  "List Template Gallery""Theme Gallery""Reporting Templates",  "Solution Gallery",  
  55.                  "Style Library""Web Part Gallery","Site Assets""wfpub","Site Pages")  
  56.    
  57.         #Loop through each site collection  
  58.         foreach($Site in $WebApp.Sites)  
  59.         {  
  60.             #Loop through each site in the site collection  
  61.             foreach($Web in $Site.AllWebs)  
  62.             {  
  63.                 #Loop through each document library  
  64.                 foreach ($List in $Web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary))  
  65.                 {  
  66.                     #Get only Document Libraries & Exclude Hidden System libraries  
  67.                     if (($List.Hidden -eq $false) -and ($SystemLists -notcontains $List.Title) )  
  68.                     {  
  69.                         #Loop through eadh Item  
  70.                         foreach ($ListItem in $List.Items)  
  71.                         {  
  72.                             if( ($ListItem.File.CheckOutStatus -ne "None") -and ($ListItem.File.CheckedOutByUser -ne $null))  
  73.                             {  
  74.                                 $sitecollectionUrl =  "<SiteCollection relativeURL=" + $Site.RootWeb.ServerRelativeURL + "></SiteCollection>"  
  75.                                 #Create an object to hold storage data  
  76.                                 $resultsData = New-Object PSObject  
  77.                                 $resultsData | Add-Member -type NoteProperty -name "SiteCollection Title" -value $Site.RootWeb.Title -Force    
  78.                                 $resultsData | Add-Member -type NoteProperty -name "SiteCollection URL" -value $sitecollectionUrl -Force              
  79.                                 $resultsData | Add-Member -type NoteProperty -name "Web Title" -value $Web.Title -Force  
  80.                                 $resultsData | Add-Member -type NoteProperty -name "Web URL" -value $Web.url -Force  
  81.                                 $resultsData | Add-Member -type NoteProperty -name "Library Name" -value $List.Title -Force  
  82.                                 $resultsData | Add-Member -type NoteProperty -name "File Name" -value $ListItem.Name -Force  
  83.                                 $resultsData | Add-Member -type NoteProperty -name "File URL" -value $Web.Site.MakeFullUrl(“$($Web.ServerRelativeUrl.TrimEnd(‘/’))/$($ListItem.Url)”)  -Force  
  84.                                 $resultsData | Add-Member -type NoteProperty -name "Last Modified" -value $ListItem['Modified'].ToString() -Force  
  85.                                 $resultsData | Add-Member -type NoteProperty -name "Checked-Out By" -value $ListItem.File.CheckedOutByUser -Force  
  86.                                 $resultsData | Add-Member -type NoteProperty -name "Checked-Out By User Email" -value $ListItem.File.CheckedOutBy.Email -Force  
  87.                                 $resultsData | Add-Member -type NoteProperty -name "Primary Administrator" -value $Site.Owner -Force  
  88.                                 $resultsData | Add-Member -type NoteProperty -name "Primary Administrator Email" -value $Site.Owner.Email -Force  
  89.                                 $resultsData | Add-Member -type NoteProperty -name "Secondary Administrator" -value $Site.SecondaryContact -Force  
  90.                                 $resultsData | Add-Member -type NoteProperty -name "Secondary Administrator Email" -value $Site.SecondaryContact.Email -Force  
  91.                                 $results += $resultsData   
  92.                             }  
  93.                         }  
  94.                     }  
  95.                 }  
  96.                 $Web.Dispose()           
  97.             }  
  98.             $Site.Dispose()           
  99.         }  
  100.         $results | export-csv -Path $currentPhysicalPath/ListAllCheckedOutFiles.csv -notypeinformation -Force  
  101.          
  102.         #Send message to output console  
  103.         write-host "Checked out Files Report Generated Successfully!"  
  104.     }  
  105.     catch [System.Exception]   
  106.     {   
  107.         write-host -f red $_.Exception.ToString()   
  108.     }   
  109. }  
  110.  
  111. # Function Call  
  112. $WebApp = Read-Host "Enter the web application URL to work on:"  
  113. GetCheckedOutFiles $WebApp  

 


Similar Articles