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! 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 which 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://crescenttech.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("[email protected]");

#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|[email protected]"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • May 18, 2021 at 5:47 PM

    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
  • March 3, 2021 at 6:21 PM

    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
  • May 14, 2020 at 3:15 PM

    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
    • November 27, 2020 at 10:12 AM

      Im having the same problem

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

      Many thanks

      Reply
    • November 30, 2020 at 6:12 AM

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

      Reply
  • April 21, 2020 at 5:26 PM

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

    Reply
  • April 1, 2020 at 3:00 PM

    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