This is a collection of snippets of PowerShell that I found helpful when trying to access SharePoint data via PowerShell. Another great resource is probably Shane Young.
NOTE: Pretty sure all commands return security trimmed results, so make sure the user you are running as has appropriate permissions, otherwise some results may be omitted.
Create CSV of all Site Collections and their last modified date:
Get-SPSite -Limit All | select Url, LastContentModifiedDate | Export-Csv SiteCollections.csv
Create CSV of all Sites and Subsites and their last modified date:
Get-SPSite -Limit All | Get-SPWeb -Limit All | select Url, LastItemModifiedDate | Export-Csv SubSites.csv
Create CSV of all lists within a site, its last modified date, and whether or not the list was hidden:
$url = "http://srvr-shareptpor/mysite"
$web = Get-SPWeb $url
$filename = "Lists-" + $web.title + ".csv"
$web.lists | Select Title, LastItemModifiedDate, Hidden | Export-Csv $filename
$filename
Show members of SharePoint group with user login and email.
Get-SPWeb 'https://sharepoint.example.com' | Select -ExpandProperty SiteGroups | Where {$_.Name -EQ "Group Name"} | Select -ExpandProperty Users | Select Name, userlogin, Email
See all methods and properties of a thing
$something | Get-Member
See items in a list:
$url = "http://sharepoint.example.com/mysite"
$web = Get-SPWeb $url
$list = $web.lists["Documents"]
$list.GetItems() | Select Name, Id
Move an item in to a document set retaining versions:
$url = "http://sharepoint.example.com/mysite"
$web = Get-SPWeb $url
$list = $web.lists["Documents"]
$item = $list.GetItemById(2)
$target = $list.GetItemById(1)
$file = $web.GetFile($item.Url)
$file.MoveTo($target.Url + "/" + $file.name)
# Tricks along the way
$web.lists | Select Title # to see lists on that site
$list.GetItems() | Where-Object -Property "Name" -like -Value "A*" | Select Name, Id # To see a subset of list items
Dig in to details about a specific list item:
$url = "http://sharepoint.example.com/mysite"
$web = Get-SPWeb $url
$list = $web.lists["Documents"]
$items = $list.GetItems()
$item = $items.GetItemById(5)
$userObj = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $item["Person"])
$userObj.User
Show contents of a folder within a doclib:
$url = "http://sharepoint.example.com/mysite"
$web = Get-SPWeb $url
$list = $web.lists["Documents"]
$list.Folders | Where-Object -Property "Name" -like -Value "30005*" | Select Name, Id
$folder = $list.Folders.GetItemById(9154) # Gets folder as SPListItem type
$folder.Folder # Folder as SPFolder type
$folder.Folder.Files | Select Name
$folder.Folder.SubFolders | Select Name
Count number of items below a single folder in a doclib (run as .ps1)
$url = 'http://sharepoint.example.com/mysite'
$web = Get-SPWeb $url
$list = $web.lists['testlibrary']
$folder = $list.Folders.GetItemById(10).Folder
Function countFolderItems( $folder ) {
$folderItemsCount += $folder.Files.Count
if ( $folder.SubFolders.Count -ne 0 ) {
Write-Host "$($folder.SubFolders.Count) subfolder(s) found in $($folder.Name)"
$subFolders = $folder.SubFolders
ForEach ( $subFolder in $subFolders ) {
$folderItemsCount += countSubFolderItems ( $subFolder )
}
}
return $folderItemsCount
}
Function countSubFolderItems( $subFolder ) {
return countFolderItems ( $subFolder )
}
$totalItems = countFolderItems ( $folder )
Write-Host "Found $totalItems total files (not including folders) inside $($folder.Name)"
Write-Host ""
Count number of items in a doclib
$url = 'http://sharepoint.example.com/mysite'
$web = Get-SPWeb $url
$web.Lists | Select Title
$list = $web.lists['Documents']
$list.Items.Count