r/PowerShell • u/Routine-Yogurt3430 • Apr 15 '26
How to remove thousands of folders checked out by a user
I created a SharePoint Document Center and forgot to disable the "require check out" option. A user uploaded thousands of documents with nested sub-folders. Other users could not see any files in the DC and it was then I saw the issue and disabled the file check out option. Unfortunately the system won't let me as admin delete the folder structure she created as it says the files are open (checked out).
To speed things up I created a new root folder and had her upload her files to that one. Now other users can see the sub-folder data and all is good.
Unfortunately I still have the original folder with the checked out files. The user doesn't have time to check each file in, and I don't have time to take ownership of each file. Is there a PowerShell script that might do this? I have seen a few scripts referencing "Connect-PnPOnline" but my system doesn't recognize it. Has it been replaced with an updated command?
**UPDATE**
So I have tried the script listed below to accomplish this and though it doesn't throw any errors, it returns to a powershell prompt without displaying anything on the screen, nor does it check in the files. I have tried adjusting the PageSize variable as there are thousands of files in this DC.
Any thoughts on what I can do to get this working?
#############################################################
CLS
$SiteURL = "https://XXXXXXXXX.sharepoint.com/sites/global-dl-legal"
$LibraryName ="Documents"
Write-host "Processing $($SiteURL) ..."
Connect-PnPOnline -Url $SiteURL -ClientId 12345 -Interactive
$List = Get-PnPList $LibraryName
# Get Context
$clientContext = Get-PnPContext
foreach ($File in Get-PnPListItem -List $LibraryName -PageSize 500) {
if ($File.FieldValues.CheckoutUser -ne $null) {
try {
write-host "File $($File.FieldValues.FileRef) is Checked out to $($File.FieldValues.CheckoutUser.LookupValue). Performing UndoCheckOut ..." -NoNewline
# -Url: relative path of the file
# -AsListItem: Retrieves the file and returns it as a ListItem object
$ListItem = Get-PnPFile -Url $File.FieldValues.FileRef -AsListItem
# Get the File object
$targetFile = $ListItem.File
$clientContext.Load($targetFile)
$clientContext.ExecuteQuery()
# The file should be checked out for this operation
$targetFile.UndoCheckOut()
$clientContext.ExecuteQuery()
write-host "Done!" -ForegroundColor Green
}
catch {
write-host "Error: $($Error[0])" -ForegroundColor Red
}
}
}
Disconnect-PnPOnline
1
u/LowKeyMikeT Apr 15 '26
DMS-Shuttle can do bulk check ins. Worth the license cost IMO
2
u/Fit_Indication_2529 Apr 16 '26
Connect-PnPOnline has not been replaced. It is still current, but it comes from the PnP.PowerShell module. Current guidance is to install PnP.PowerShell, and recent docs say it uses modern auth and that PowerShell 7.4+ is required. The PnP docs also note that -ClientId is required for some auth flows starting September 9, 2024.
3
u/Modify- Apr 15 '26 edited Apr 15 '26
Im not a fan of write-host. You should be using: Write-output, Write-warning or Write-error.
Secondly, the try catch only works while the error is a terminating one. If not, the catch block does nothing. I have not worked with these methods so I can't say for sure if they produce that kind of error.
For debugging, start with the first item in the loop. You can do that by doing: foreach ($thing in $things[0]){}
Then check if each command/variable has the expected value. Go one by one until you have found the "bug" in the script. Hope this helps a bit!
1
u/alanjmcf Apr 15 '26
I haven’t read through the script in detail, but what happens if you run it in a debugger. VS code, or even ISE of PnP works for you in PSv5.
Does it get a bunch of files returned from Get-PnPListItem? For testing I’d sign that to a variable before passing it to foreach.
1
u/BlackV Apr 15 '26
There are multiple problems with that script, you should run through a debug or manually line be line (fix the for loop first)
Have you tried that?
1
u/BlackV Apr 15 '26
p.s. formatting
it'll format it properly OR
Inline code block using backticks
`Single code line`inside normal textSee here for more detail
Thanks