Share via


Using PowerShell to clean up SharePoint document library files with no versions

Versioning of SharePoint document libraries allows it to become a great repository for files.  You can check items in and out and maintain the history of the file automatically.  A powerful library tool in SharePoint document libraries is the ability to open the document library with Explorer.  If you have the Windows Desktop Experience installed (default on client OSs, manual on Server), you can open an Explorer Window that will allow you to drag and drop files directly into the document library.

Unfortunately, with this power, comes responsibility.  When versioning is enabled on the document library and performing drag & drop additions in this way, the items are not checked in.  Instead, you must check them in manually before they can be seen and used by anyone else who has permissions to that document library.  While usually this is performed manually, there are cases when you may wish to have some code do this for you.

Imagine a user dragging and dropping entire directory trees of documents into a library from their file system.  Then the user leaves the company and now the administrator is faced with hundreds of files listed in the "Manage files which have no checked in version".  What a mess!

So after some research, I wrote a little piece of PowerShell script that would recursively enumerate all the files in a document library and check them in if they were listed as checked out and had no version count. 

Prior to running this code as an administrator you must take ownership of all the files listed in the in the "Manage files which have no checked in version" section of the library settings page first.  If you do not do this, the files will not be checked in.  

When playing around with this script, it would be wise to comment out the $item.File.CheckIn("Checked in By Administrator")line of code to make sure you are actually getting the files you wish to check-in.  You can also manipulate the library by changing the values of $site, $root and $folder to values that need.  The real work is done once you call the function Set-CheckInFolderItems $folder This will recursively traverse the library specified by $folder and check in any files that have no versions.  After running this code, the list of files in the "Manage files which have no checked in version" page should be empty for that library.

The PowerShell script is below.  Note that the line of code Write-Host "Check in File: "$item.Name" Version count " $item.File.Versions.Count -foregroundcolor Green should be all on one line.  I had to reduuce the font so that it would display properly.  I've also attached a zip file with the code in it as well.  Enjoy!

 

[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")

$site = New-Object Microsoft.SharePoint.SPSite("https://SP2010")

$root = $site.allwebs[0]

$folder = $root.GetFolder("My Document Library")

#============================================================

# Function Set-CheckInFolderItems is a recursive function

# that will CheckIn all items in a list recursively

#============================================================

function Set-CheckInFolderItems([Microsoft.SharePoint.SPFolder]$folder)

{

    # Create query object

    $query = New-Object Microsoft.SharePoint.SPQuery

     $query.Folder = $folder

 

    # Get SPWeb object

    $web = $folder.ParentWeb

 

    # Get SPList

    $list = $web.Lists[$folder.ParentListId]

    # Get a collection of items in the specified $folder

    $itemCollection = $list.GetItems($query)

    # If the folder is the root of the list, display information

    if ($folder.ParentListID -ne $folder.ParentFolder.ParentListID)

    {

        Write-Host("Recursively checking in all files in " + $folder.Name)

    }

    # Iterate through each item in the $folder 

    foreach ($item in $itemCollection)

    {

        # If the item is a folder

        if ($item.Folder -ne $null)

        {

            # Write the Subfolder information

            Write-Host("Folder: " + $item.Name + " Parent Folder: " + $folder.Name)

            # Call the Get-Items function recursively for the found sub-solder

            Set-CheckInFolderItems $item.Folder

        }

        # If the item is not a folder

        if ($item.Folder -eq $null)

        {

            if ($item.File.CheckOutType -ne "None")

            {

                if ($item.File.Versions.Count -eq 0)

                {

                    # Check in the file

Write-Host "Check in File: "$item.Name" Version count " $item.File.Versions.Count -foregroundcolor Green

                    $item.File.CheckIn("Checked in By Administrator")

                }

            }

        }

    }

    $web.dispose()

    $web = $null

}

 

Set-CheckInFolderItems $folder

 

Recursive File Check In.zip

Comments

  • Anonymous
    March 07, 2014
    ok

  • Anonymous
    August 04, 2014
    Paul getting below error, pls advise You cannot call a method on a null-valued expression. At C:tempRecursiveFilecheckin.ps1:22 char:37

  •     $itemCollection = $list.GetItems <<<< ($query)    + CategoryInfo          : InvalidOperation: (GetItems:String) [], RuntimeException    + FullyQualifiedErrorId : InvokeMethodOnNull
  • Anonymous
    August 04, 2014
    Hi Sierra!  Without having detailed information of your system to debug the script, It's hard to advise on the error you are getting.  Three years ago, on the versions mentioned, this did work on the scenario described.  It appears you need some detailed support on this issue.  Please call Premier Support at 800-936-3100 for that assistance.  Sorry I cannot be of additional assistance on this.

  • Anonymous
    July 05, 2016
    How do you do this with CSOM?