Skip to content

Instantly share code, notes, and snippets.

@tigattack
Last active April 24, 2021 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tigattack/ff27eb93550c588140eefc8e4295c75d to your computer and use it in GitHub Desktop.
Save tigattack/ff27eb93550c588140eefc8e4295c75d to your computer and use it in GitHub Desktop.
List all files in a folder and it's subfolders in SharePoint Online
If (Get-Module -ListAvailable -Name 'SharePointPnPPowerShellOnline') {
Import-Module SharePointPnPPowerShellOnline
}
Else {
$PnPPInstallStatus = 'False'
}
Function Get-PnPFolderContents {
param (
$Credential = (Get-Credential),
$Domain = (Read-Host -Prompt 'Enter the primary domain name in your 365 tenant (no spaces or TLDs)'),
$Library = (Read-Host -Prompt 'Enter the name of the SharePoint library you''re working in'),
$Folder = (Read-Host -Prompt 'Enter the name of the folder you''re working in (e.g. My folder)')
)
If ($PnPPInstallStatus -eq 'False') {
$InstallPnPP = (Read-Host -Prompt 'Would you like to install PnP PowerShell? You cannot continue without installing this module. (Y/N)')
If ($InstallPnPP -eq 'Y') {
Install-Module SharePointPnPPowerShellOnline
}
If ($InstallPnPP -eq 'N') {
Write-Warning 'Cannot continue without PnP PowerShell. Exiting.'
Break
}
}
$siteURL = "https://$Domain.sharepoint.com/sites/Documents/$Library"
Connect-PnPOnline -Url $siteURL -Credentials $Credential
$FolderURL = "Documents/"+$Folder
$folders = Get-PnPFolderItem "$FolderURL" -ItemType Folder
Write-Output "Contents of $Library/$FolderURL':"
ForEach($folder in $folders){
$newFolderURL = $FolderURL+"/"+$folder.Name
Get-PnPFolderItem -FolderSiteRelativeUrl $newFolderURL -ItemType File
}
}
@ytrezq
Copy link

ytrezq commented Apr 23, 2021

Doesn’t work for large folders. Like those with 100000 files in the same directory.

@tigattack
Copy link
Author

I'm not surprised, this was a quick and dirty solution to a problem at my old workplace. The majority of ths script could be hugely improved.

I no longer have access to a SharePoint instance to test with, but feel free to suggest a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment