Archive for the ‘Powershell’ Category

Powershell tip – find all checked out files

Wednesday, February 15th, 2012

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.)

Quick tip: SharePoint powershell – get items in a list based on custom columns and other hints

Tuesday, February 14th, 2012

This may be handy when trying to find specific items in a list based on values of various fields:

$web = Get-SPWeb http://yourweb
$list = $web.Lists["Your Library Name"]

// this is the bit - get items of a particular content type
// ? is shorthand for where, and $_ is the item in the pipeline
$listItems = $list.Items | ?{$_.ContentType.Name -eq "Content Type Name"}

// or items based on a custom column - if using -like then the wildcard is *
$listItems = $list.Items | ?{$_["InternalFieldName"] -like "*this*"

// you could join them up using -and
$listItems = $list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*"

// or iterate the loop and print them out
foreach($item in $listItems) { Write-Host $item.Name, $item["InternalFieldName"] }

or more directly

$list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*" | foreach { $_.Name, $_["InternalFieldName"]

// or count them
$listItems.Count

or

$list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*" | foreach {$count++}
$count

Powershell can be infuriating – but when you find the syntax, it can be pretty helpful.

P.S For a bonus tip, next time you’re in Powershell, hit F7 ;)