I'm using the PowerShell script below to create views on a SharePoint Online document library with subfolders. The script creates the view, but does not display the files contained in the subfolder. The script creates a view that 'shows all items without folders'.
The Excel file in the screenshot is the only file in the subfolder 0002TestFolder. Unfortunately, I'm not very familiar with PowerShell and don't know what the script requires to create the view correctly.
#Config Variables
$SiteURL = "https://XXXX.sharepoint.com/sites/TestTeamSite"
$ListName = "Documents"
$ViewName = "0002TestFolder"
$ViewFields = @("DocIcon", "Name", "Modified", "Modified By")
$ViewColumns = "DocIcon", "Name", "Modified", "Modified By"
$Query = "<OrderBy><FieldRef Name='Title' /></OrderBy><Where><Leq><FieldRef Name='Modified' /><Value Type='DateTime'><Today/></Value></Leq></Where>"
#Get Credentials to connect
#$Cred = Get-Credential
Try {
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#sharepoint online pnp powershell create view
Add-PnPView -List $ListName -Title $ViewName -ViewType Html -Fields $ViewFields -Query $Query -ErrorAction Stop
Set-PnPView -List $ListName -Identity $ViewName -Values @{Scope=[Microsoft.SharePoint.Client.ViewScope]::Recursive}
Write-host "View '$ViewName' Created Successfully!" -f Green
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Subfolder View:
All Documents View:

