Bulk Check-In All Checked Out Files in SharePoint Online using PowerShell

In a SharePoint document library, if the “Require documents to be checked out to edit” option is configured, users are forced to checked-out the required file to make changes. Once a user checked out a file to edit, the file gets locked with exclusive edit access to only the checked-out user. If other users need to make changes on the same document, the file should have been checked-in back by the checked-out user.

Over the period, the checked-out files count will gradually increase if users forgot to check-in back the files. In this case, other users can’t be able to make changes in those files and they can only open the files in read-only mode. As a SharePoint Administrator, you need to inform the checked-out users to check-in back the file.

In this post, we will explore how to find all the checked-out files and check in back the files using PnP PowerShell. Before you start, ensure that you have already installed the latest PnP PowerShell module. Run the following command if you have not already installed it.

Install-Module -Name "PnP.PowerShell"

Bulk Check-In All Checked Out Files in SharePoint Online Site Library

This PowerShell script gets all the checked-out documents in the given SPO site document library and check-in the documents one by one. Finally, the script exports the file details (such as file name, URL, and checked-out user name) and their check-in status report to a CSV file.

$Result = @() #Result array
 
#Provide SPO Site URL   
$SiteUrl = "https://contoso.sharepoint.com/sites/sitename"
 
#Provide List/Document Library name 
$ListName = "Shared Documents"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
 
#CAML Query to filter only Checked-out files
$CAMLQuery = "
<View Scope='RecursiveAll'>
    <Query>
        <Where>
            <IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull>
        </Where>
    </Query>
</View>"
  
#Query All Checked out files
$ListItems = Get-PnPListItem -List $ListName -Query $CAMLQuery  -PageSize 1000  
$i = 0;
$TotalFiles = $ListItems.Count

#Iterate checked-out files one by one and check-in
ForEach($ListItem in $ListItems)
{
$i++;
Write-Progress -activity "Processing $($ListItem.FieldValues.FileLeafRef) " -status "$i out of $TotalFiles files completed"
$CheckInStatus = '';
try
{
#Check-In the file and set check-in comment
Set-PnPFileCheckedIn -Url $ListItem.FieldValues.FileRef -CheckinType MajorCheckIn -Comment "Auto check-in by Adminstrator"
$CheckInStatus ='Success'
}
catch
{
$CheckInStatus = = "Failed: $_"
}

#Add the Checked out file and check-in status to the Result array
$Result += New-Object PSObject -property $([ordered]@{ 
FileName = $ListItem.FieldValues.FileLeafRef
FileUrl        = $ListItem.FieldValues.FileRef
CheckedOutUser    = $ListItem.FieldValues.CheckoutUser.LookupValue
CheckInStatus    = $CheckInStatus
})
     
}
 
#Export the check-in status report to a CSV file
$Result | Export-CSV "C:\Temp\Check-In-Status-Report.CSV" -NoTypeInformation -Encoding UTF8 
Advertisement