Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Recursively checks for files in a document library that are checked out to a particular user (or checked out at all).
#Requires -Module SharePointPnPPowerShellOnline
$ErrorActionPreference = 'Stop'
$SPUrl = 'https://yourtenant.sharepoint.com/sites/yourgroup'
# Recursively calls Get-PnpFolderItem for a given Document Library
function Get-PnpFolderItemRecursively($FolderSiteRelativeUrl) {
$Items = @(Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl)
foreach ($Item in $Items) {
# Strip the Site URL off the item path, because Get-PnpFolderItem wants it
# to be relative to the site, not an absolute path.
$ItemPath = $Item.ServerRelativeUrl -replace "^$(([Uri]$Item.Context.Url).AbsolutePath)/",''
# If this is a directory, recurse into this function.
# Otherwise, write the item out to the pipeline.
if ($Item -is [Microsoft.SharePoint.Client.Folder]) {
Get-PnpFolderItemRecursively $ItemPath
}
else {
Write-Output $Item
}
}
}
# Filters the pipeline to only include files that are checked out.
# If -Email parameter is included, then it will be further filtered to
# only files checked out to that user.
function Where-PnpIsCheckedOut([String]$Email) {
begin {
$CheckedOutItems = @()
}
process {
# Is this file checked out at all?
if ($_.CheckOutType -ne 'None') {
if ($Email) {
# CheckedOutByUser is not loaded by default.
# Queue it up for load.
# Will not actually make a trip to the server until ExecuteQuery later.
$_.Context.Load($_.CheckedOutByUser)
}
$CheckedOutItems += $_
}
}
end {
foreach ($CheckedOutItem in $CheckedOutItems) {
if ($Email) {
# When -Email is specified, we need to load CheckedOutByUser and
# compare it to the parameter value
if ($CheckedOutItem.Context.HasPendingRequest) {
$CheckedOutItem.Context.ExecuteQuery()
}
if ($CheckedOutItem.CheckedOutByUser.Email -match $Email) {
Write-Output $CheckedOutItem
}
}
else {
# When no -Email is specified, all checked out items are returned.
Write-Output $CheckedOutItem
}
}
}
}
try {
Connect-PnPOnline -Url $SPUrl -Credentials (Get-Credential)
Get-PnpFolderItemRecursively 'Shared Documents' |
Where-PnpIsCheckedOut 'someone@yourtenant.com'
}
catch {
Write-Error "$_"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.