I'm running a SharePoint PnP PowerShell script and only getting partial results—specifically, I'm only seeing some details for the first site in the list. I'm trying to connect to multiple sites using a list of URLs stored in a text file. I've set up the connection and added permissions for a user as site collection admin, but the script doesn't seem to work as expected. I really need help figuring out where I went wrong! Here's the code I'm working with:
```powershell
$filePath = "C:\temp\EEA.txt"
Connect-SPoService -Url https://delta-admin.Sharepoint.com
$SiteURLs=Get-Content -Path $filePath
$TargetUser = "Everyone except external users"
$ReportPath = "C:\temp\PermissionReportEE.csv"
$Results = @()
Foreach($SiteURL in $SiteURLs)
{
Set-SPOUser -Site $SiteURL -LoginName "[email protected]" -IsSiteCollectionAdmin $true
Connect-PnPOnline -Url $SiteURL -ClientID gs008363-0457-400-y667-647484yyy -Interactive
Set-PnpSite -Identity $SiteURL -Owners "[email protected]"
function Get-CustomPermissions {
param($Object, $Type, $Title)
$HasUniquePerms = Get-PnPProperty -ClientObject $Object -Property HasUniqueRoleAssignments
if ($HasUniquePerms) {
$Assignments = Get-PnPProperty -ClientObject $Object -Property RoleAssignments
foreach ($Role in $Assignments) {
$Member = Get-PnPProperty -ClientObject $Role -Property Member
if ($Member.Title -eq $TargetUser) {
$Results += [PSCustomObject]@{
Type = $Type
Location = $Title
User = $Member.Title
SiteURL = $SiteURL
}
$Results | Export-Csv -Path $ReportPath -Append -NoTypeInformation
}
}
}
}
$Web = Get-PnPWeb
$Assignments = Get-PnPProperty -ClientObject $Web -Property RoleAssignments
foreach ($Role in $Assignments) {
$Member = Get-PnPProperty -ClientObject $Role -Property Member
if ($Member.Title -eq $TargetUser) {
$Results += [PSCustomObject]@{
Type = "Web/Site"
Location = $Web.Url
User = $Member.Title
SiteURL = $SiteURL
}
$Results | Export-Csv -Path $ReportPath -Append -NoTypeInformation
}
}
$Lists = Get-PnPList
foreach ($List in $Lists) {
Get-CustomPermissions -Object $List -Type "List/Library" -Title $List.Title
$Items = Get-PnPListItem -List $List -PageSize 500
foreach ($Item in $Items) {
Get-CustomPermissions -Object $Item -Type "Item/File" -Title "$($List.Title) - ItemID: $($Item.Id)"
}
}
}
```
3 Answers
Try running your script line by line to see the results at each step. This can help you pinpoint what’s going wrong. I also suggest optimizing your code; instead of using `$Results +=`, a more efficient way might be to gather results using a different loop structure—it could help with performance.
It looks like you might need to set up an App Registration in Entra as part of your login process. For interactive logins, that step is now required. You can find the details on how to register your application in the PnP PowerShell documentation. Here's a link to get you started: https://pnp.github.io/powershell/articles/registerapplication.html.
Don't forget that the PnP module is case-sensitive when retrieving properties. Double-check the case for your properties to match your SharePoint objects. Also, when calling properties, remember to put them in square brackets!

Yeah, it seems like OP is going for a more secure approach but might not fully realize why that extra step is essential.