title: How to get a list of all documents from SharePoint using PowerShell?
body: <p>I want to retrieve all documents from SharePoint using PowerShell, but at the moment I can only retrieve documents for a particular site.</p>
  
  <p>This is the code I am using:</p>
  
  <pre><code>#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 &amp; 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 = "&lt;View Scope='RecursiveAll'&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='FSObjType' /&gt;&lt;Value Type='Integer'&gt;0&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;OrderBy&gt;&lt;FieldRef Name='ID' /&gt;&lt;/OrderBy&gt;&lt;/Query&gt;&lt;/View&gt;"
      $ListItems=$List.GetItems($Query)
      $Ctx.Load($ListItems)
      $Ctx.ExecuteQuery()
  
      $VersionHistoryData = @()
      #Iterate throgh each version of file
      Foreach ($Item in $ListItems)
      {
          ##more code goes here.
      }
  }
  </code></pre>