Getting an Inventory of All SharePoint Documents Using Windows PowerShell

Posted on Posted in Scripts, SharePoint 2010

I got an email today asking if I had anything that would generate a report detailing all the documents throughout an entire SharePoint Farm. As this wasn’t the first time I’ve been asked this same question I decided that I’d just go ahead and post the script for generating such a report.

The script is really quite straightforward – it simply iterates through all Web Applications, Site Collections, Webs, Lists, and finally, List Items. I skip any List that is not a Document Library (as well as the Central Admin site) and then build a hash table containing all the data I want to capture. I then convert that hash table to an object which is written to the pipeline.

All of this is placed in a function which I can call and then pipe the output to something like the Out-GridView cmdlet or the Export-Csv cmdlet. I also wrote the script so that it works with either SharePoint 2007 or SharePoint 2010 so that I don’t have to maintain two versions (I could have used cmdlets such as Get-SPWebApplication, Get-SPSite, and Get-SPWeb but there was little benefit to doing so and the script would be limited to SharePoint 2010).

One word of caution – in a large Farm this script should be run off hours or at least on a back facing server (not your WFE) – it’s going to generate a lot of traffic to your database.

function Get-DocInventory() {
    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    foreach ($spService in $farm.Services) {
        if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {
            continue;
        }

        foreach ($webApp in $spService.WebApplications) {
            if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }

            foreach ($site in $webApp.Sites) {
                foreach ($web in $site.AllWebs) {
                    foreach ($list in $web.Lists) {
                        if ($list.BaseType -ne "DocumentLibrary") {
                            continue
                        }
                        foreach ($item in $list.Items) {
                            $data = @{
                                "Web Application" = $webApp.ToString()
                                "Site" = $site.Url
                                "Web" = $web.Url
                                "list" = $list.Title
                                "Item ID" = $item.ID
                                "Item URL" = $item.Url
                                "Item Title" = $item.Title
                                "Item Created" = $item["Created"]
                                "Item Modified" = $item["Modified"]
                                "File Size" = $item.File.Length/1KB
                            }
                            New-Object PSObject -Property $data
                        }
                    }
                    $web.Dispose();
                }
                $site.Dispose()
            }
        }
    }
}
Get-DocInventory | Out-GridView
#Get-DocInventory | Export-Csv -NoTypeInformation -Path c:\inventory.csv

116 thoughts on “Getting an Inventory of All SharePoint Documents Using Windows PowerShell

    1. Hi Gary… I’m still quite new to the world of PowerShell but getting there…

      Is it possible to break out from the version count how many Major and Minor versions there are?

  1. Hi – thanks for posting the inventory script. When I run it, I am able to get a CSV file with some info and then powershell errors out.

    “Get-DocInventory : Exception has been thrown by the target of an invocation.
    At C:\getdocs.ps1:41 char:17
    + Get-DocInventory <<<< | Export-Csv -NoTypeInformation -Path c:\inventory.csv
    + CategoryInfo : NotSpecified: (:) [Get-DocInventory], TargetInvocationExcepti
    + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,Get-DocInventory

    Any thoughts as to what I might be doing wrong?

  2. Not sure what the issue is – you might try adding some Write-Host statements in the script to see where exactly it is failing (or just use the $Error variable to dig into the stack trace if you know how to do that).

  3. In this case I don’t think code or powershell is the right way to go due to the overhead and speed (lack thereof) of sharepoint api at grabbing a dataset like this. I much prefere (and use) this direct SQL query instead:

    SELECT
    Webs.Title AS ‘Web’,
    AllLists.tp_Title AS ‘List Name’,
    AllDocs.DirName AS ‘URL’,
    AllDocs.LeafName AS ‘Name’,
    AllDocs.ExtensionForFile AS ‘File Type’,
    AllDocStreams.Size AS ‘File Size’,
    AllDocs.TimeCreated
    FROM AllDocs
    JOIN AllLists
    ON AllLists.tp_ID = AllDocs.ListId
    JOIN AllDocStreams
    ON AllDocStreams.Id = AllDocs.Id
    JOIN Webs
    ON Webs.Id = AllLists.tp_WebId
    ORDER BY Webs.Title

    ^takes literally a thousand times less overall processing time than sharepoint api.

    You can also filter results easily using something like this:
    WHERE Webs.Title NOT LIKE ‘%SiteName%’
    AND AllDocs.TimeCreated > ‘2010-05-10 00:00:00.000’

    1. I paste this and i am getting error. if there anything i am missing do i need to make any changes before i can use this.

      please help thanks.
      yp

    2. You posted this code back in 2010. Is it possible to also find the DOCID and author. I can’t find them in any of the tables.

      SELECT
      Webs.Title AS ‘Web’,
      AllLists.tp_Title AS ‘List Name’,
      AllDocs.DirName AS ‘URL’,
      AllDocs.LeafName AS ‘Name’,
      AllDocs.ExtensionForFile AS ‘File Type’,
      AllDocStreams.Size AS ‘File Size’,
      AllDocs.TimeCreated
      FROM AllDocs
      JOIN AllLists
      ON AllLists.tp_ID = AllDocs.ListId
      JOIN AllDocStreams
      ON AllDocStreams.Id = AllDocs.Id
      JOIN Webs
      ON Webs.Id = AllLists.tp_WebId
      ORDER BY Webs.Title

  4. Yes, hitting the database directly is always going to be faster but it’s also not supported and puts you in an unsupported state with Microsoft. The supported route is to use the API – it’s slower but…

  5. I have two questions:

    1. Does this script work on 2010 as well? It worked fine for me on 2007.
    2. I need to use a similar approach to get the following properties from each list (not list item):
    a. Content types configured on the list, and the scope of the content type (list, web or site)
    b. Columns configured on the list, and again the scope (list or content type, and if content type, which one)

    How do I go about finding out what properties are available on a list in PowerShell?

    1. Hey David, we are getting errors when we tried to run this script on MOSS 2007 version. Error is because of -Property .
      Pleae need your help.

      Thanks
      Manasa

  6. Yes, this will work in both 2007 and 2010. To get the properties you can either use the SDK or, if you have an instance of a list (an SPList object), you can pass that object to the Get-Member cmdlet to see all the methods and properties that are available.

  7. I find it absurd that Microsoft dosn’t give you an easy way to determine the number of documents in a document library, not to mention listing the items out by folder. I’ve wasted a few days trying to find what I missed, it turns out that I haven’t missed anything – it dosn’t exist!!!!! Nice! I’ll keep working on this, while Bill gives my money to charities.

    1. Oh, and if you want folders, just do this:
      $web = Get-SPWeb http://demo
      $list = $web.GetList(“Documents”)
      Write-Host “Total Items: $($list.ItemCount)”
      $list.Folders | % {
      $hash = @{“Url”=$_.Url;”Count”=$_.Folder.ItemCount}
      New-Object PSObject -Property $hash
      }

      1. Gary,
        Is it possible to list out the folders and subfolders for each document in the library? I found that if I add “Folder” = $list.Folders it will display all folders. I’m more interested in just the folder for the document on the current row of the csv file that is exported.

  8. Is there a way to retrieve the file extention as well? I’d like to also see what type of file (how many of each) I’m dealing with.

    Thanks

      1. Hi Gary, thanks for this script. It’s awesome.

        I’m running the site collection script and I’m having trouble where to add this file extension with within the script. I also want to group the file extension (.i.e Group-Object extension | Sort-Object Count) but I’m not having any luck. any suggestions.

        function Get-DocumentInventory([string]$siteUrl) {
        $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
        foreach ($web in $site.AllWebs) {
        foreach ($list in $web.Lists) {
        if ($list.BaseType -ne “DocumentLibrary”) {
        continue
        }
        foreach ($item in $list.Items) {

        $data = @{
        “Web Application” = $webApp.ToString()
        “Item Modified” = $item[“Modified”]
        “Item Created” = $item[“Created”]
        “File Size” = $item.File.Length/1KB
        “Web” = $web.Url
        “Site” = $site.Url
        “Item URL” = $item.Url
        extension: [System.IO.Path]::GetExtension($item.File.Name)
        Group-Object extension | Sort-Object Count

        }
        New-Object PSObject -Property $data
        }
        }
        $web.Dispose();
        }
        $site.Dispose()
        }
        Get-DocInventory site ULR| Out-GridView
        Get-DocInventory | Export-Csv -NoTypeInformation -Path c:\PowerShell\LOGS\Report_SC_Docs.csv

      1. When I add this snip to retrieve the Author, the value is blank for all documents in the report.
        Why would the value be blank?

    1. function Get-DocumentInventory([string]$siteUrl) {
      $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
      foreach ($web in $site.AllWebs) {
      foreach ($list in $web.Lists) {
      if ($list.BaseType -ne “DocumentLibrary”) {
      continue
      }
      foreach ($item in $list.Items) {
      $data = @{ … }
      New-Object PSObject -Property $data
      }
      }
      $web.Dispose();
      }
      $site.Dispose()
      }

  9. Hey Gary,

    Just a quick tip that may improve performance…

    To get a filtered set of lists that are only of type DocumentLibrary, you can use this:

    $lists = $web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary)

    Cheers,
    Ben

    1. Thanks Ben. I always forget about that method. Note that you can also just do this:
      $lists = $web.GetListsOfType(“DocumentLibrary”)
      PowerShell’s adaptive type system will convert the string to the proper enum type.

      1. I know this is an old post but how can i use this to only return items from document libraries? Also, if i just wanted to return items from lists?

        $lists = $web.GetListsOfType(“DocumentLibrary”)

  10. Gary,
    I tried the the example you gave for running against a Site Collection but I’m doing something wrong.
    What do I need to correct?
    Thanks. Terry

    function Get-DocumentInventory([string]$siteUrl) {
    $site = New-Object Microsoft.SharePoint.SPSite $siteUrl = “http://sharepoint.aa.com/sites/SCname”
    foreach ($web in $site.AllWebs) {
    foreach ($list in $web.Lists) {
    if ($list.BaseType -ne “DocumentLibrary”) {
    continue
    }
    foreach ($item in $list.Items) {

    $data = @{
    “Web Application” = $webApp.ToString()
    “Item Modified” = $item[“Modified”]
    “Item Created” = $item[“Created”]
    “File Size” = $item.File.Length/1KB
    “Web” = $web.Url
    “Site” = $site.Url
    “Item URL” = $item.Url
    }
    New-Object PSObject -Property $data
    }
    }
    $web.Dispose();
    }
    $site.Dispose()
    }

    Get-DocInventory | Out-GridView
    Get-DocInventory | Export-Csv -NoTypeInformation -Path c:\PowerShell\LOGS\Report_SC_Docs.csv

    1. Change this line:
      $site = New-Object Microsoft.SharePoint.SPSite $siteUrl = “http://sharepoint.aa.com/sites/SCname”
      To this:
      $site = New-Object Microsoft.SharePoint.SPSite $siteUrl

      Then pass in the site URL:
      Get-DocInventory “http://sharepoint.aa.com/sites/SCname” | Out-GridView

  11. Hey Gary,

    When I run the code I get this error:

    Get-DocInventory : Exception has been thrown by the target of an invocation.
    At line:39 char:17
    + Get-DocInventory <<<< | Out-GridView
    + CategoryInfo : NotSpecified: (:) [Get-DocInventory], TargetInvocationException
    + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,Get-DocInventory

    What am I doing wrong?

  12. Is there a way to break out the file name from the URL? We still need the URL but not in the same column as Filename.

    Thanks!

  13. Hi again Gary, it was a copy & paste issue as you said…I got that error cleared up. However, now when I run the script, nothing at all happens, no errors, nothing. Any ideas?

      1. Hi Gary, I tried to run the following:

        function Get-DocInventory([string]$siteUrl)
        {
        $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
        foreach ($web in $site.AllWebs) {
        foreach ($list in $web.Lists) {
        if ($list.BaseType -ne “DocumentLibrary”) {continue}
        foreach ($item in $list.Items) {
        $data = @{
        “Web Application” = $webApp.ToString()
        “Site” = $site.Url
        “Web” = $web.Url
        “list” = $list.Title
        “Item ID” = $item.ID
        “Item URL” = $item.Url
        “Item Title” = $item.Title
        “Item Created” = $item[“Created”]
        “Item Modified” = $item[“Modified”]
        “File Size” = $item.File.Length/1KB
        }
        New-Object = PSObject -Property $data
        }
        }
        $web.Dispose();
        }
        $site.Dispose()
        }

        .\Get-DocInventory1 “http://mysitespigit.barcapint.com/sites/spcrawler/default.aspx” | Export-Csv -NoTypeInformation -Path D:\Wenzhe\SPtest.csv

        It created an empty SPtest.csv file, do you have any idea what goes wrong?

        My second question is that… for your original script, I am quite confused about where to run it., it seems to me that it should be run on server farm, but i don’t know where it is. Can you explain more?

        Many thanks!!

        1. You’re loading the script wrong. You need to load it into memory and then call the function.
          . .\Get-DocInventory1.ps1
          Get-DocInventory “http://mysitespigit.barcapint.com/sites/spcrawler/default.aspx” | Export-Csv -NoTypeInformation -Path D:\Wenzhe\SPtest.csv

          You need to run the script from one of your SharePoint servers.

  14. This is a great script 🙂 Helps a lot. But as i am new to powershell: Is it possible to get the content if a list? So e.g. i have a List with the same name accross the Site Collection and in every site i have a list with the same name. And in this lists i have a column which is called “Topic” could i extract that data also with powershell?

    Also i wanted to search only a certain List with the name “persons” so i changed “if ($list.BaseType -ne “DocumentLibrary”) ” to “if ($list.Title -eq “persons”)” but it doen’t work 🙁 What is wrong ?

  15. Gary –

    Have you tried to an export on all returned values from this script? I have several hundreds of subsites that we want to dispose of; I need to collect ALL documents from their libraries and migrate to a single library under the Site Collection.

    1. Should be pretty easy – just a matter of calling the OpenBinary() method on the SPFile object and saving it to the appropriate path using the [System.IO.File]::WriteAllBytes() method.

  16. Hi,

    If I would like to filter out file extensions before running the script, i.e only quering for documents with .docx extension, is that something that is possible and how would the code look like and where do i place it?

    Many Thanks

    1. It would involve use CAML to query items from a list rather than iterating through every item (though you could still iterate over all items and just check the file extension but that’s not as efficient).

  17. Hi Gary,

    First of all, I would like to thank you for this article. I looked for various methods to getting all documents list and I think this is the best way to realize what I want.

    I faced up a problem, an exception has been thrown about file exporting, I am copying error details. Someone also encounter this problem but nobody tells how to solve it. May be I miss some steps or do incorrectly because I’m new at powershell admin (I started to learn and use it today :)). After the copied your script, I saved it on a text file then save name as osman.ps1 then invoked it from SPpowershell script.

    PS C:\Users\Administrator> c:\osman.ps1
    Get-DocInventory : Exception has been thrown by the target of an invocation.
    At C:\osman.ps1:42 char:17
    + Get-DocInventory <<< c:\osman.ps1
    Get-DocInventory : Exception has been thrown by the target of an invocation.
    At C:\osman.ps1:42 char:17
    + Get-DocInventory <<<< | Export-Csv -NoTypeInformation -Path c:\inventory.csv
    + CategoryInfo : NotSpecified: (:) [Get-DocInventory], TargetInvo
    cationException
    + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,Get-
    DocInventory

    1. Unfortunately that error is just a wrapper so I can’t say exactly what is going on without more information. After you get the error try to outout the $Error automatic variable to see if you can get more details:
      $Error[0] | select *

      0 is the last error that occurs so it should give you what you need (every once in a while I need to check a higher index because of things that happen behind the scenes in powershell which add to this collection but it’s pretty rare).

      1. I had exactly the same message and I believe it’s a permissions issue. I tried running the script as the farm admin and had no problems.

      2. I am seeing the same error. I am logged in as the farm admin. When I look at $Error, I get:
        PS C:\Users\sa_spsdev\Documents\powershell> $error
        Get-DocInventory : Exception has been thrown by the target of an invocation.
        At C:\Users\sa_spsdev\Documents\powershell\get-docinventory.ps1:44 char:17
        + Get-DocInventory <<<< | Export-Csv -NoTypeInformation -Path d:\temp\inventor
        y.csv
        + CategoryInfo : NotSpecified: (:) [Get-DocInventory], TargetInvo
        cationException
        + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,Get-
        DocInventory

        What powershell settings need to be in place for the script to work?

        1. You need to look at the inner exception to see what’s really going with that error. As long as you have rights to run the cmdlets (so you’re a shell admin) and you have rights to the site collection you’re inspecting, then the code should work.

  18. Hey there,

    This is a tremendous resource and I’ve had some great results. Thank you for your work and follow up throughout this thread.

    I’m looking for a full listing of all major and minor versions, including comments that have been appended to each version.

    We have the Author (I saw that parameter listed above), and I assume this is capturing who the last editor was.

    thanks again,

      1. Hey Gary,
        First of all thanks for your great Script und your precious help in your Blog! Is there a way to get the Version size information via Powershell, since it is not a member of the SPListItemVersion object?
        Thanks in advance,

        Mortimer

  19. Hi,
    To start off with I like your site and have made use of some of the information with great success.

    My question is, is it possible to find out who and when a file in a Document Library has been accessed (read only) if Auditing isn’t turned on. i.e. is there a property that logs the history of a file, like what you get from auditing, even though auditing is not turned on.

    Regards,

    Chris

    1. There’s no way out of the box to do that. You’d have to create a custom handler for the specific document type (like I did with the PDF handler on one of my recent posts) and log the information that way.

  20. This is nice. So I’m wondering how we could modify this so we can download the document to a file share. I have a requirement to copy a boatload of documents to a filesystem. I guess I’ll check the SPListItem class and see if there is a download method.

  21. I’m going to show my ignorance here, but whats the moderation status?

    I tried googling the term in assication to SP2010 files but noting really came up appart from normal approval status.

  22. Hi Gary,
    After executing these below command I am getting below error . I am using SharePoint 2010 Management shell . I would require all the available file name along with their size in a SharePoint Site Collection .Could you please help me on this .

    Add-PsSnapin Microsoft.Sharepoint.Powershell
    function Get-DocumentInventory([string]$siteUrl) {
    $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
    Get-DocInventory “http://sharepoint.share.com” | Out-GridView
    foreach ($web in $site.AllWebs) {
    foreach ($list in $web.Lists) {
    if ($list.BaseType -ne “DocumentLibrary”) {
    continue
    }
    foreach ($item in $list.Items) {

    $data = @{
    “Web Application” = $webApp.ToString()
    “Item Modified” = $item[“Modified”]
    “Item Created” = $item[“Created”]
    “File Size” = $item.File.Length/1KB
    “Web” = $web.Url
    “Site” = $site.Url
    “Item URL” = $item.Url
    }
    New-Object PSObject -Property $data
    }
    }
    $web.Dispose();
    }
    $site.Dispose()
    }

    Get-DocInventory | Out-GridView
    Get-DocInventory | Export-Csv -NoTypeInformation -Path c:\Report Docs.csv

    The term ‘Get-DocInventory’ is not recognized as the name of a cmdlet, function, script file, or operabl
    e program. Check the spelling of the name, or if a path was included, verify that the path is correct an
    d try again.
    At C:\Temp\report.ps1:28 char:17
    + Get-DocInventory <<<< | Out-GridView
    + CategoryInfo : ObjectNotFound: (Get-DocInventory:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    The term 'Get-DocInventory' is not recognized as the name of a cmdlet, function, script file, or operabl
    e program. Check the spelling of the name, or if a path was included, verify that the path is correct an
    d try again.
    At C:\Temp\report.ps1:29 char:17
    + Get-DocInventory <<<< | Export-Csv -NoTypeInformation -Path c:\Report Docs.csv
    + CategoryInfo : ObjectNotFound: (Get-DocInventory:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  23. Hi guys, the script is working very well, I was wondering if there is a way to twick this script to get a list of all orphan items and only, means document that do not have a parent document librarry and are not linked anywhere in the site, pages that doesn’t belong to any web site, sub site that do not have a prent site ect

  24. Hi Gary
    I need to get a documents count either from DocLibA or DocLibB or Both.
    can you let me know how the “or” function will work in this scenario.

    regards,
    Nik

  25. I am brand new to powershell. I have found an article that talks about setting the execution scope for the user. I have copied your script above into getdocinventory.ps1 .
    When I start powershell, and then type the path to the ps1 file, I get the message:
    The term “Out-GridView” is not recognized as a cmdlet, …

    From reading on the web, I thought that Out-GridView was a part of PowerShell. So what might I be doing wrong?

    I tried looking to see what version of PowerShell we have installed. It looks to me like it is saying v2.0.50727

    Should I be able to get Out-GridView here? If not, then what do I need to do?

    1. You probably have PowerShell v1 installed (that version number is the .net version number (in the console view the properties of the $host variable to see what version you’re running – just type “$host [ENTER]”). You need v2 or better for the cmdlet (if you’re running 2007/2010 then stick with v2).

  26. Thank you for the pointer to version 2 of PowerShell.

    Does SharePoint maintain in the metadata of the documents the concept of “last accessed date”?

    We are trying to determine files which are no longer being read, to make them candidates for review before removal…

  27. I’m trying to inventory how much custom code (from code blocks) we have on our 2007 Farm. What to start by identifying everything that looks like a “Page” and the library won’t always be obvious. Can Powershell search actual documents. Also, any way see page syntax code with SQL?

    1. You could use PowerShell to pull down the aspx file and then use a regex to look within that file for embeded code blocks. Going directly against the database won’t help you if the page is unghosted as the file contents will reside on the file system so you’ll definitely want to stick with using the object model.

      1. Thank You. So far I’ve got a script that write-hosts the full URL of all my aspx pages. By pull down do you mean save locally.. or can my regex run in line within my powershell script. Or would a better option be to ge the UNC path to the aspx page and then use another script with regex? Any links or code samples would be appreciated.

  28. Hi Gary. Thank you for your efforts. Is there a way to modify this so I can identify lists and libraries with >5,000 items?

    Thanks!

  29. I could use some help.
    I have tried to update a copy of your original script to cover several of the things you mentioned before, as well as set up the script to take the farm name as an argument. The script starts out:

    function Get-DocInventory([string]$farmUrl) {
    [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
    $farm = New-Object Microsoft.SharePoint.SPFarm $farmUrl
    :
    other code from above
    :
    $farm.Dispose()
    }

    Get-DocInventory http://mysite | Export-Csv -NoTypeInformation -Path d:\temp\inventory.csv

    When I run my this script file, I get this as my output:
    New-Object : Cannot find type [Microsoft.SharePoint.SPFarm]: make sure the asse
    mbly containing this type is loaded.
    At C:\Users\spsadmin2\Documents\PowerShell\Get-DocInventory.ps1:8 char:23
    + $farm = New-Object <<<< Microsoft.SharePoint.SPFarm $farmUrl
    + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentExcepti
    on
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewOb
    jectCommand

    You cannot call a method on a null-valued expression.
    At C:\Users\spsadmin2\Documents\PowerShell\Get-DocInventory.ps1:55 char:16
    + $farm.Dispose <<<< ()
    + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeEx
    ception
    + FullyQualifiedErrorId : InvokeMethodOnNull

    I know the second error is because of the first. I just don't know why the assembly is not being loaded.
    This is SP 2010 SP 1, powershell v2.0. I am on as a farm admin, and opened the sp 2010 management shell as an admin.

    Is there anything I can check to see what is going wrong?

    1. There’s no URL for the farm and you don’t need to dispose the SPFarm object. If you’re on SP2010 just set the $farm variable using this: $farm = Get-SPFarm

      1. So, I am trying to adapt your script to MOSS SP 2007 , which uses powershell v1. Is there a way to get the script to run for a farm in that situation?

        1. The script is written to work on 2007. I never tested on V1 but I think it should work – I recommend you upgrade to V2 though anyways as it will make your life much easier.

  30. Hi Gary,

    Thanks for sharing the script and answering questions.

    It may seem stupid…but what do you mean by saying “If you’re on SP 2010”?
    i got confused on where to run this script.. on SharePoint farm? But where is it?

    Many thanks!

    1. You need to be a SharePoint farm administration and run the script on the server hosting the farm. If you’re not sure what I mean by that then you likely don’t have the permissions you require and should get with someone in your IT department.

  31. Hi Gary,

    Thanks for the excellent post. I have a requirement of extracting the unique URLs generated by the Document ID service of SP 2010 (NOT item ID) from every site in SharePoint. Is there a simple way to achieve this using PS?
    Many thanks – Sabs.

    1. Hello Gary,

      I have a requirement to list the inventory of list items which are in Pending state with in a site collection. Could you let me know how to find this.

      Thanks

  32. Good afternoon! We finally got powershell v2 installed on my farm so I ran your script (modified with several of the additional columns you mentioned in reply to others).
    I had 2 interesting events – twice during the run I got the following error:
    The following exception was thrown when trying to enumerate the collection: “Server Out Of Memory
    There is no memory on the server to run your program. Please contact your administrator with this problem.”.
    At C:\Documents and Settings\sa_sps\My Documents\WindowsPowerShell\inventory.ps1:18 char:32
    + foreach <<<< ($item in $list.Items) {
    + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : ExceptionInGetEnumerator

    Our server has 4 GB of memory (it is a 32 bit server) so I wondered if you had an idea what might cause a couple of "no memory on the server" type exception.

  33. Hi Gary,
    Very good article on PowerShell Script for all the documents in the Farm. Can you Have any Idea how to limit the usage of the user in a site collection to upload documents in any documents.
    thanks for the article.

  34. hello! I have an SP 2010 farm. I have been asked to get a count of the library items per site collection across the entire farm.

    When I run the above script, I get all sorts of things that appear to be much more than just library items.
    Is there a way that I can take the output that this creates and filter out just the library items? I don’t need to include general site master pages, list items, announcements, etc. – just the document libraries.

    Thank you for your helpful blogs!

  35. This is a great script. I tried to get it to run on a specific place in the site collection (something like Get-DocumentInventory “http://sharepoint.domain.com/sitecollection/folder/subfolder/subfolder”)

    Any way to specify this somewhere in the code so it doesn’t run on the whole site collection and just the specific folder and subfolders recursively?

  36. Hi,

    Great Article., I’ve a query: Is it possible to find all the Custom list in site collection using PowerShell scripts?

    Thanks,
    Pandiarajan

  37. Hi Gary,

    to investigate impact of lowering the listview treshold on our farm, we need to be able to get the number of items per folder (which can be nested). Output should then be something like:
    site a
    => folder 1 – 1 (the subfolder)
    => folder 1.1 – 3 (1 file & the 2 subfolders)
    => folder 1.1.1 – 10 (10 files)
    => folder 1.1.2 – 5 (5 files)
    => folder 2 – 1 (1 file)

    and so on..

    I have been playing around based upon the post above, but can’t get the desired output. Can you help me into the right direction?

    Thx

  38. Hi Gary,

    Not sure if I will use your code, but wanted to say thanks man. It takes a lot of effort to share code like this and then provide answers to everyone’s comments. It is very nice of you. Thank you.

    Paul.

  39. Thank you so much for this. Even in 2016 with SP 2013 (On Prem) this code works!

    Great to use this for comparing migrated content (i.e. if it existed in MOSS 2007, was it properly transferred to SP 2010 / SP 2013)

    Thanks again Gary.

  40. Got it to work perfectly, but it pulls the data from all site collections. Is it possible to run against just a particular subsite? I tried adding the site after the Get-DocInventory, but it still pulled all of the site collections. Any help would be greatly appreciated.

    Thank you,
    Brandon

  41. Hello Gary,

    Thanks a ton for this tremendous resource… its perfect.

    Just want to know if we can include content database name and size for each of the sites? i am very new to Power shell.

    Thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *

*