SharePoint Online: Manage Files Which Have No Checked in Version using PowerShell

When users don’t provide value for mandatory metadata field values when uploading files (in classic experience) or when users forget to check in documents while the “Require Check Out” option under versioning settings of any library is set to “Yes”, then those files are called “Files Which Have No Checked-in Version”. The file(s) which are uploaded will be stored on SharePoint but will be checked out and visible only to the uploader! It often happens when users upload using File explorer view!

Manage Files Which Have No Checked In Version in SharePoint Online:

As a site administrator, You can take ownership of files that have no checked-in version. To take control,

  • Go to the “Manage files which have no checked-in version” link under Library Settings, 
  • Select the documents and click on the “Take Ownership of Selection” button and confirm the prompt. 
  • Once you took ownership, the document gets removed from this page and starts appearing in the list where they are uploaded (only to You, until you check-in them!) As a site owner, you can set the metadata properties if needed, check-in and make these documents visible for all users. sharepoint online manage files which have no checked in version powershell

PowerShell to Take Ownership and Check-In All Files Which Have No Checked In Version in SharePoint Online:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/sites/Marketing/"
$ListName ="Documents"

Try{
    #Get Credentials to connect
    $Cred= Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get Checked out Files with no checked-in version
    $List = $Ctx.Web.lists.GetByTitle($ListName)
    $CheckedOutFiles = $List.GetCheckedOutFiles()
    $Ctx.Load($List)
    $Ctx.Load($CheckedOutFiles)
    $Ctx.ExecuteQuery()

    #Loop through each checked-out file
    ForEach($File in $CheckedOutFiles)
    {
        #Take Ownership of the File
        $CheckedOutByUser=$File.CheckedOutBy
        $Ctx.Load($CheckedOutByUser)
        $File.TakeOverCheckOut()
        $Ctx.ExecuteQuery()

        #Get the File and Check-in
        $CheckInFileURL = [String]::Concat($List.ParentWebUrl, $File.ServerRelativePath.DecodedUrl.Replace($List.ParentWebUrl, [string]::Empty))
        $CheckInFile = $Ctx.web.GetFileByServerRelativeUrl($CheckInFileURL)
        $Ctx.Load($CheckInFile)
        $Ctx.ExecuteQuery()

        #Check in the file
        $CheckInFile.CheckIn("Checked-in from PowerShell",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
        $Ctx.ExecuteQuery()

        Write-Host -f Yellow "'$($CheckInFile.name)' at $($CheckInFile.ServerRelativeUrl) was Checked Out by: $($CheckedOutByUser.Email)"
        Write-host -f Green "Checked In The File Successfully!"         
    }
}
Catch {
        write-host -f Red "Error:" $_.Exception.Message
}

Can I take ownership & Check-in all files from a specific user? Sure:

#Get the User
$User = $Web.EnsureUser("Salaudeen@crescent.com");

#Check if the File is checked out to the specific user
If($File.CheckedOutById -eq $User.Id)
{
 #Take ownership and check-in
}

PnP PowerShell to check-in All Checked out Files with No Checked-In Version

This PowerShell script checks in all files checked out to a particular user.

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$UserID = "i:0#.f|membership|salaudeen@crescent.onmicrosoft.com"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext

#Get the List
$List = Get-PnPList $ListName
#Get the User
$CheckedoutByUser = Get-PnPUser -Identity $UserID

#Get All Checked-Out Files
$CheckedOutFiles = $List.GetCheckedOutFiles()
$Ctx.Load($CheckedOutFiles)
$Ctx.ExecuteQuery()

#Check-in All Files Checked out to the User
$CheckedOutFiles | ForEach-Object {
    If($_.CheckedOutById -eq $CheckedoutByUser.Id)
    {
        #Take over the Checked-Out File
        $_.TakeOverCheckOut()
        $Ctx.ExecuteQuery()

        #Check-in the file
        $CheckInFileURL = [String]::Concat($List.ParentWebUrl, $_.ServerRelativePath.DecodedUrl.Replace($List.ParentWebUrl, [string]::Empty))
        Set-PnPFileCheckedIn -Url $CheckInFileURL -CheckinType MajorCheckIn -Comment "Checked-In by the Script at $(Get-Date)"    
        Write-Host "Check-in the File: $CheckInFileURL"    
    }
}

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

10 thoughts on “SharePoint Online: Manage Files Which Have No Checked in Version using PowerShell

  • Getting an error when trying to pull the checked out files

    $CheckedOutFiles += $DocLib.GetCheckedOutFiles()
    An error occurred while enumerating through a collection: The collection has not been initialized. It has not been
    requested or the request has not been executed. It may need to be explicitly requested..

    Reply
  • I have 300 sites to do this on, all in the same “Shared Documents” library, how can I import CSV of sites and loop through each site and apply the PnP script?

    Reply
  • The only way to report on GetCheckedOutFiles in a large list/library is to scrape the page or check it every X days? thx!

    Reply
  • Hello,

    Tried using the PnP script. Worked well for small libraries but gives me the view threshold error for large document libraries.
    Is there a way to work around that?

    Reply
    • Im having the same problem

      Any chance the script can be updated to take this into account?

      Many thanks

      Reply
    • Unfortunately, The “GetCheckedOutFiles” method strictly complies with the 5000 threshold limit as of today.

      Reply
  • Hello! Amazing article, but is there a way to do all this via sharepoint-pnp?

    Reply
  • Great article!
    But, we have a couple of questions.

    1) We’re getting this error after running through the script once
    “Error Adding Custom Action! Exception calling “ExecuteQuery” with “0” argument(s): “File Not Found.””

    2) Where would we add the “Specific User” code?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *