Forum Discussion
luvsql
Jan 10, 2022Steel Contributor
Need Assistance Modifying powershell script for libraries over 5,000 records
Microsoft support assisted me with getting this script setup so that I could get a bug fixed (which they are now saying is by design) that is preventing files from being moved. For some reason, whic...
luvsql
Jan 10, 2022Steel Contributor
Here is the script:
#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://tenantname.sharepoint.com/subsite/"
$LibraryName="Documents"
Try{
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
#Get the list
$List = $Web.Lists.GetByTitle($LibraryName)
#Prepare the query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull>
</Where>
</Query>
<RowLimit Paged='TRUE'>2000</RowLimit>
</View>"
#Batch Process items: sharepoint online powershell bulk check in
Do {
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Get All Checked out files
ForEach($Item in $ListItems)
{
#Get the Checked out File data
$File = $Ctx.Web.GetFileByServerRelativeUrl($Item["FileRef"])
$Ctx.Load($File)
$CheckedOutByUser = $File.CheckedOutByUser
$Ctx.Load($CheckedOutByUser)
$Ctx.ExecuteQuery()
Write-Host -f Yellow "Found a Checked out File '$($File.Name)' at $($Web.url)$($Item['FileRef']), Checked Out By: $($CheckedOutByUser.LoginName)"
#Check in the document
$File.CheckIn("Checked-in By Administrator through PowerShell!", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$Ctx.ExecuteQuery()
Write-Host -f Green "File '$($File.Name)' Checked-In Successfully!"
}
}
While($Query.ListItemCollectionPosition -ne $Null)
}
Catch {
write-host -f Red "Error Check In Files!" $_.Exception.Message
}
#Read more: https://www.sharepointdiary.com/2017/07/sharepoint-online-powershell-to-bulk-check-in-all-documents.html#ixzz7F8GUYZg7
#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://tenantname.sharepoint.com/subsite/"
$LibraryName="Documents"
Try{
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
#Get the list
$List = $Web.Lists.GetByTitle($LibraryName)
#Prepare the query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull>
</Where>
</Query>
<RowLimit Paged='TRUE'>2000</RowLimit>
</View>"
#Batch Process items: sharepoint online powershell bulk check in
Do {
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Get All Checked out files
ForEach($Item in $ListItems)
{
#Get the Checked out File data
$File = $Ctx.Web.GetFileByServerRelativeUrl($Item["FileRef"])
$Ctx.Load($File)
$CheckedOutByUser = $File.CheckedOutByUser
$Ctx.Load($CheckedOutByUser)
$Ctx.ExecuteQuery()
Write-Host -f Yellow "Found a Checked out File '$($File.Name)' at $($Web.url)$($Item['FileRef']), Checked Out By: $($CheckedOutByUser.LoginName)"
#Check in the document
$File.CheckIn("Checked-in By Administrator through PowerShell!", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$Ctx.ExecuteQuery()
Write-Host -f Green "File '$($File.Name)' Checked-In Successfully!"
}
}
While($Query.ListItemCollectionPosition -ne $Null)
}
Catch {
write-host -f Red "Error Check In Files!" $_.Exception.Message
}
#Read more: https://www.sharepointdiary.com/2017/07/sharepoint-online-powershell-to-bulk-check-in-all-documents.html#ixzz7F8GUYZg7