Wednesday, February 11, 2015

SharePoint PowerShell: Recursively checkin files in a folder

This is a script that will try to parse a certain folder in a SharePoint 2010 site and recursively check for files that are checked out.
If found, the files would be checked in,

This is useful in situations where you are unable to move files and folders because someone had the files checked out.



 $ver = $host | select version   
 if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}   
 Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue   
 Function OverrideCheckIn($folder)  
 {   
   Write-Host "+ " $folder.Name  
   foreach($file in $folder.Files)  
   {  
  if ($file.CheckOutStatus -ne "None" )  
  {  
  Write-Host "$($file.Name) is Checked out To: $($file.CheckedOutBy)"  
     $file.CheckIn("Checked In By Administrator")  
     Write-Host "$($file.Name) Checked In" -ForeGroundColor Green  
  }  
   }  
   # Use recursion to loop through all subfolders.  
   foreach ($subFolder in $folder.SubFolders)  
   {  
     OverrideCheckIn($subfolder)  
   }  
  }  
 $TargetWeb = "http://sharepoint/site1"   
 $TargetLibrary = "Shared Documents"   
 $Folder = "Week1/Day 1"  
 $web = Get-SPWeb $TargetWeb   
 $list = $web.Lists[$TargetLibrary]  
 #Get the folder reference based on the url AND use the Files property  
 $folder = $list.ParentWeb.GetFolder($web.Url + "/" + $list.RootFolder.Url + "/" + $Folder)  
 OverrideCheckIn($folder)  
 $web.Dispose()  
 ​  

No comments:

Related Posts Plugin for WordPress, Blogger...