Last active
April 24, 2021 14:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
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
Doesn’t work for large folders. Like those with 100000 files in the same directory.