Powershell tip - find all checked out files
Wed, Feb 15, 2012
One-minute read
I needed to quickly find all files in a particular web that were checked out. I immediately went to Powershell, and after hacking about for a few minutes did what I should have done first - Google’d it. Doing this revealed that Gary had already done the bulk of what I needed to do. However, I made a couple of changes, namely that I only wanted it to traverse a specific web, but I needed it to traverse any subwebs in that web and lists.
function GetCheckedOutFiles($web)
{
Write-Host "Processing Web: $($web.Url)..."
foreach ($list in ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
Write-Host "`tProcessing List: $($list.RootFolder.ServerRelativeUrl)..."
foreach ($item in $list.CheckedOutFiles) {
if (!$item.Url.EndsWith(".aspx")) { continue }
$hash = @{
"URL"=$web.Site.MakeFullUrl("$($web.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
"CheckedOutBy"=$item.CheckedOutBy;
"CheckedOutByEmail"=$item.CheckedOutByEmail
}
New-Object PSObject -Property $hash
}
foreach ($item in $list.Items) {
if ($item.File.CheckOutStatus -ne "None") {
if (($list.CheckedOutFiles | where {$_.ListItemId -eq $item.ID}) -ne $null) { continue }
$hash = @{
"URL"=$web.Site.MakeFullUrl("$($web.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
"CheckedOutBy"=$item.File.CheckedOutByUser;
"CheckedOutByEmail"=$item.File.CheckedOutByUser.Email
}
New-Object PSObject -Property $hash
}
}
}
foreach($subWeb in $web.Webs)
{
GetCheckedOutFiles($subweb)
}
$web.Dispose()
}
$web = get-spweb $args[0]
GetCheckedOutFiles($web)
Save it as GetCheckedOutFiles.ps1 and you would then call it with
./GetCheckedOutFiles http://urltoweb | Format-Table url | out-file output.txt -width 500
(or do as Gary does and save it as cmdlet for easy reuse.)