How to audit SharePoint using powershell
Sometimes you need to know what information you have got in your SharePoint solution. I have been asked this numerous times since GDPR was introduced.
PowerShellGet-SPWeb http://your-sharepoint-site | Select -ExpandProperty Lists | Where-Object { $_.GetType().Name -eq "SPDocumentLibrary" -and -not $_.Hidden } | Select -ExpandProperty Items | Group-Object { $_.ParentList.ParentWeb.Url + "/" + $_.ParentList.Title } | Select-Object Name, Count | Format-Table -AutoSize
This PowerShell command is designed to work with SharePoint, and its main purpose is to list the items in SharePoint document libraries that are not hidden. Let’s break it down step by step:
- Get-SPWeb http://your-sharepoint-site:
- This cmdlet retrieves a specific SharePoint web (a site) based on the given URL.
- Select -ExpandProperty Lists:
- This takes the SharePoint web retrieved from the first command and expands its “Lists” property. Essentially, it fetches all the lists (and libraries, since in SharePoint a document library is technically a type of list) present on the given SharePoint web.
- Where-Object { $.GetType().Name -eq “SPDocumentLibrary” -and -not $.Hidden }:
- This filters the results from the previous command. It only selects those objects that are of type “SPDocumentLibrary” (meaning they’re document libraries) and are not hidden.
- Select -ExpandProperty Items:
- This takes each of the filtered document libraries and expands their “Items” property, meaning it fetches all the individual items (like documents) within each library.
- Group-Object { $.ParentList.ParentWeb.Url + “/” + $.ParentList.Title }:
- This groups the resulting items by their parent list’s web URL and title. The purpose of this is likely to aggregate the items based on which library (and its location) they come from.
- Select-Object Name, Count:
- From the grouped results, this command selects the name of the group (which is the library’s web URL and title from the previous command) and the count of items within that group.
- Format-Table -AutoSize:
- Finally, this formats the results into a table, adjusting the column sizes to fit the content (due to
-AutoSize
).
- Finally, this formats the results into a table, adjusting the column sizes to fit the content (due to
In summary, the command fetches all items from non-hidden document libraries in a given SharePoint web, groups them by their parent library’s location and title, and then displays a table showing how many items are in each of these libraries.
1 Response
[…] Remember, you can embed PowerShell commands into sharepoint. […]