Skip to content

Instantly share code, notes, and snippets.

@josheinstein
Last active February 1, 2018 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josheinstein/3ace0c9f8e25d07583ceb57d13f71b2e to your computer and use it in GitHub Desktop.
Save josheinstein/3ace0c9f8e25d07583ceb57d13f71b2e to your computer and use it in GitHub Desktop.
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