简体   繁体   中英

How to get a list of all documents from SharePoint using PowerShell?

I want to retrieve all documents from SharePoint using PowerShell, but at the moment I can only retrieve documents for a particular site.

This is the code I am using:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Parameters
$SiteURL="https://xxx.sharepoint.com/sites/CRMDevelopment"
$LibraryName="Documents"
$ReportOutput = "C:\users\xxx\downloads\VersionHistory.csv"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the web & Library
    $Web=$Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($LibraryName)
    $Ctx.ExecuteQuery()

    #Get All Files of from the document library - Excluding Folders
    $Query =  New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>"
    $ListItems=$List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    $VersionHistoryData = @()
    #Iterate throgh each version of file
    Foreach ($Item in $ListItems)
    {
        ##more code goes here.
    }
}

Demo to iterate all site collections by PnP PowerShell and SharePoint online management shell, if you want to access the library in all site collections, the account need access to all site collections(admin not has permissions to all site collections by default).

#region Variables 
$AdminName = "admin@xxx.onmicrosoft.com" 
$Password = "password"
#endregion Variables

#region Credentials 
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force 
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $(convertto-securestring $Password -asplaintext -force)
#endregion Credentials
#Config Parameters
$AdminSiteURL="https://xxx-admin.sharepoint.com"
#$ReportOutput = "C:\users\xxx\downloads\VersionHistory.csv"

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –Credential $credentials

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
Write-Host "Total Number of Site collections Found:"$SiteCollections.count -f Yellow

#Loop through each site collection and retrieve details
Foreach ($Site in $SiteCollections)
{
    #Test for specific site
#if($Site.URL -eq "https://xxx.sharepoint.com/sites/lee"){
    Write-Host "Processing Site Collection :"$Site.URL -f Yellow

    Connect-PnPOnline -Url $Site.URL -credentials $credentials
    $items=Get-PnPListItem -List Documents -Query "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>"
    Foreach ($Item in $items)
    {
        Write-Host $Item.FieldValues["FileRef"]
    }
#}
}
Write-Host "--"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM