Find All Checked Out Files and Check-In them Back using PowerShell

I'm Sharing one of the PowerShell script I used in SharePoint 2010 migration. Since its very difficult to check-in back all the checked-out files after migration, its a best practice to check-in all checked out files prior.

Find All Checked-Out files and Check in back in a SharePoint Library using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$WebURL="http://sharepoint.crescent.com"
$LibraryURL="/Shared%20Documents" #Relative URL

#Get Objects
$Web = Get-SPWeb $WebURL
$Folder = $web.GetFolder($LibraryURL)

#Function to find all checked out files in a SharePoint library
Function CheckIn-CheckedOutFiles($Folder)
{

    $Folder.Files | Where { $_.CheckOutStatus -ne "None" }  | ForEach-Object {
    write-host ($_.Name,$_.URL,$_.CheckedOutBy)

    #To Check in
    $_.Checkin("Checked in by Administrator")
    }

   #Process all sub folders
   $Folder.SubFolders | ForEach-Object {
   CheckIn-CheckedOutFiles $_
   }
}

#Call the function to find checkedout files
CheckIn-CheckedOutFiles $Folder 

While this works fine, lets improve the performance by filtering checked out documents using CAML query:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  
#Variables
$WebURL="http://intranet.crescent.com"
$ListName="Documents"
 
#Get Objects
$Web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)
 
If($List -ne $Null)
{
    #Define CAML query to filter all checked out files
    $Query = New-Object Microsoft.SharePoint.SPQuery
    $Query.Query = "<Where>
                      <IsNotNull>
                        <FieldRef Name='CheckoutUser' />
                      </IsNotNull>
                    </Where>"
    $Query.ViewAttributes = 'Scope="Recursive"'
     
    $ListItems = $List.GetItems($Query)
    
    Write-host "Total Number of Checked Out Files Found:"$ListItems.count

    #Loop through each checked out File
    ForEach ($Item in $ListItems) 
    {
        Write-Host "'$($Item.Url)' is Checked out by: $($Item["CheckoutUser"])"

        #Check in
        #$Item.File.CheckIn("Checked in By Administrator!")
        #Write-Host -f Green "File Checked In!"
    }
}
else
{
    Write-Host -f Yellow "List '$ListName' Does not Exist!"
} 

PowerShell to Find All Checked Out Files and Check-in from a Web Application
This PowerShell script will Scans, Generates Report and check-in all checked out files. Run this script with Farm Administrator privileges.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function CheckIn-AllCheckedOutFiles()
{  
    #Define 'Web Application URL' as Mandatory Parameter
    Param( 
    [Parameter(Mandatory=$true)] [string]$WebAppURL,
    [Parameter(Mandatory=$true)] [string]$ReportOutput 
    )
 
    #Get the Web Application
    $WebApp = Get-SPWebApplication $WebAppURL

    #Write the CSV Header - Tab Separated
    "Site Collection Name `t Site Name`t Library `t File Name `t File URL `t  Last Modified `t Checked-Out By" | Out-file $ReportOutput

    #Arry to Skip System Lists and Libraries
    $SystemLists =@("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates", "List Template Gallery", "Theme Gallery", 
           "Reporting Templates", "Solution Gallery", "Style Library", "Web Part Gallery","Site Assets", "wfpub")
 
    #Loop through each site collection
    ForEach($Site in $WebApp.Sites)
    {
        #Loop through each site in the site collection
        ForEach($Web in $Site.AllWebs)
        {
            Write-host "Processing Site:" $web.Url
            #Loop through each document library
            Foreach ($List in $Web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary))
            {
                #Get only Document Libraries & Exclude Hidden System libraries
                if ( ($List.Hidden -eq $false) -and ($SystemLists -notcontains $List.Title) )
                {
                    #Loop through each Item
                    foreach ($ListItem in $List.Items) 
                    {
                        If( ($ListItem.File.CheckOutStatus -ne "None") -and ($ListItem.File.CheckedOutByUser -ne $null))
                        {
                            #Log the data to a CSV file
                            "$($Site.RootWeb.Title) `t $($Web.Title) `t $($List.Title) `t $($ListItem.Name) `t $($Web.Url)/$($ListItem.Url) `t  $($ListItem['Modified'].ToString()) `t  $($ListItem.File.CheckedOutByUser)" | Out-File $ReportOutput -Append
                            Write-host -f Yellow "Found a Checked out file at: $($Web.Url)/$($ListItem.Url)"
      
                            #Check in the file
                            $ListItem.File.Checkin("Checked in by Administrator")
                        }
                    }
                }
            }
        }
    }
 
    #Send message to output console
    write-host -f Green "Checked out Files Report Generated Successfully!"
}

#Call the Function to Get Checked-Out Files and check them in
CheckIn-AllCheckedOutFiles -WebAppURL "http://intranet.crescent.com" -ReportOutput "C:\Temp\CheckedOutFiles.txt"

Its also possible to retrieve all checked out files via Search! Here is how: Create a Managed Property by adding the existing crawled property: ows_CheckoutUser(Text), then you can use that managed property in your search!

Bulk Check-In all checked-out files in Site collection:
If you want to check-in all checked-out files by you, use "Content and Structure" link under site settings. (/_Layouts/sitemanager.aspx)
Bulk Check-in Files in SharePoint
You have the option "Checked Out to Me" which lists all files checked-out to you. Just select the desired files and click Check-in from actions menu.
Find All Checked Out Files and Check-In them Back using PowerShell Find All Checked Out Files and Check-In them Back using PowerShell Reviewed by Salaudeen Rajack on February 20, 2013 Rating: 5

5 comments:

  1. Hi
    thanks for the information but it helps to checkout all files while doing migration from 2007 to 2010.
    my situation is in Sp 2010 user had left the organization and we have only one document which was checked out bye that user.can you explain this scenario for me with clear examples because i don't have scripting knowledge.

    ReplyDelete
    Replies
    1. Surya,

      Sure, just change this line in the script. From:
      if( ($ListItem.File.CheckOutStatus -ne "None") -and ($ListItem.File.CheckedOutByUser -ne $null))

      To:
      if( ($ListItem.File.CheckOutStatus -ne "None") -and ($ListItem.File.CheckedOutByUser.LoginName -eq "Domain\UserName" ))

      Delete
  2. It appears to run but doesn't. It creates the text file and creates the headers for it. And it seems to be processing something but it never finds any checked out files and I know I have a ton of them. Not sure what I am missing

    ReplyDelete
    Replies
    1. Make sure you are running this script from FARM Admin Credentials, which should be granted with "FULL Control" User Policy to your target web application.

      Delete
    2. Getting the same problem. Tried it with both a farm admin account (my own) and the actual farm installation account, and both have the same result. Script appears to run for a few minutes then outputs a report with only the headers.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.