Forum Discussion
kcelmer
Dec 16, 2025Brass Contributor
Get-PnPListItem retrieving no files from SharePoint folder with files
Hello!
Can anyone assist with this script? I'm fairly new to PowerShell and it's likely something simple I don't know, yet.
# Install PnP PowerShell module if not already installed
# Install-Module PnP.PowerShell -Force
# Define SharePoint site URL, folder path, and the sensitivity label to apply
$SiteUrl = "https://XXXXXXXXXX.sharepoint.com/"
$FolderPath = "/sites/IT/Shared%20Documents/General/Azure%20SSO" # Example: "/Documents/ConfidentialData"
$SensitivityLabelId = "7e01211f-294a-4f47-9efd-aebe45f12d17" # The GUID of your sensitivity label
# Connect to SharePoint Online
Connect-PnPOnline -Url $SiteUrl -ClientID "XXXXXXXXXXXXXXXXXXXXX" # This will open a browser for authentication
# Get all files in the specified folder
Write-Host "Getting Files..."
$Files = Get-PnPListItem -List "Documents" -FolderServerRelativeUrl $FolderPath | Where-Object { $_.FileSystemObjectType -eq "File" }
Write-Host "Files found $Files"
# Loop through each file and apply the sensitivity label
foreach ($File in $Files) {
Write-Host "Applying label to: $($File.FieldValues.FileRef)"
Set-PnPSensitivityLabel -ListItem $File -SensitivityLabelId $SensitivityLabelId
}
Write-Host "Sensitivity label application complete."
The line
$Files = Get-PnPListItem -List "Documents" -FolderServerRelativeUrl $FolderPath | Where-Object { $_.FileSystemObjectType -eq "File" }
retrieves no files so it just skips the following loop and ends with no errors.
I added the line Write-Host "Files found $Files" to verify that it was not finding anything.
No RepliesBe the first to reply