In a SharePoint document library, if the “Require documents to be checked out to edit” option is enabled, users need to first checked-out the required file to make changes. If other users need to work on the same document, the checked-out file should have been check-in back. Over a period of time, the checked-out files count will gradually increase if the users failed or forgot to check-in back the files.
In this post, we will explore how to find all the checked-out files in a SharePoint Online Site library 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"
Find and Export All Checked Out Files in SharePoint Online Site library to CSV
The below PnP PowerShell script exports all the checked-out files and user details (name and email address) who checked out the file.
Once you get the list of checked-out files and user details, you can inform the users to Check-in back the files to unlock the edit access for other users. Otherwise, you can use PowerShell script to Check in the files. For more details, see this post: Check-In All Checked Out Files in Bulk using PnP PowerShell.
$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
#Iterate checked-out files and add to the Result array
ForEach($ListItem in $ListItems)
{
#Add the Checked out File details to the Result array
$Result += New-Object PSObject -property $([ordered]@{
FileName = $ListItem.FieldValues.FileLeafRef
FileUrl = $ListItem.FieldValues.FileRef
CheckedOutUser = $ListItem.FieldValues.CheckoutUser.LookupValue
CheckedOutUserEmail = $ListItem.FieldValues.CheckoutUser.Email
})
}
#Export the result to a CSV file
$Result | Export-CSV "C:\Temp\All-Checked-Out-Files.CSV" -NoTypeInformation -Encoding UTF8
The error message indicates the PnP context is not created for the given site. Can you confirm that the Connect to PnP Online command (See the below command) is successfully executed?
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
Also, confirm that are you using more than one Connect-PnPOnline connection.