Quick tip: SharePoint powershell - get items in a list based on custom columns and other hints
Tue, Feb 14, 2012
One-minute read
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 😉