0

I want to export default owner, member & visitor group users and group name in CSV file using PnP PowerShell.

Can someone assist this?

$Record=Import-Csv -Path ""
$ResultCollection = @()
foreach($row in $Record)
{
    
    $Site =$row.SourceURL
    $Site = $Site -split("/")
    $Name = $Site[4]
    Write-Host "Connecting source site -----> " $row.SourceURL -ForegroundColor Green
    $srcSite = Connect-PnPOnline -Url $row.SourceURL -UseWebLogin
    Write-Host "Retrieving Default group in source" 
    $srcOwner= Get-PnPGroup -AssociatedOwnerGroup
    Write-Host $srcOwner.Title
    $srcMember= Get-PnPGroup -AssociatedMemberGroup
    Write-Host $srcMember.Title
    $srcVisitor= Get-PnPGroup -AssociatedVisitorGroup
    Write-Host $srcVisitor.Title

    $srcOwnerUsers = $srcOwner.Users | Select Email | % { 
    $srcMemberUsers = $srcMember.Users | Select Email | % { 
    $srcVisitorUsers = $srcVisitor.Users | Select Email | % { 
    
        $Result = New-Object PSObject
        $Result | Add-Member -MemberType NoteProperty -Name "srcOwner" -Value $srcOwner.Title
        $Result | Add-Member -MemberType NoteProperty -Name "srcOwnerUsers" -Value $_.Email
        $Result | Add-Member -MemberType NoteProperty -Name "srcMember" -Value $srcMember.Title
        $Result | Add-Member -MemberType NoteProperty -Name "srcMemberUsers" -Value $_.Email
        $Result | Add-Member -MemberType NoteProperty -Name "srcVisitor" -Value $srcVisitor.Title
        $Result | Add-Member -MemberType NoteProperty -Name "srcVisitorUsers" -Value $_.Email
        $ResultCollection += $Result;
    
    }
    
    }
    
    }
    
     
        $ResultCollection | Export-Csv -Path "" -NoTypeInformation 

} 
3
  • I have updated my code above. i am facing issue in exporting all the users from individual group. it is fetching one user only from all the group. Not a multiple users. pls someone look into this.
    – Liz
    Oct 2 '20 at 12:05
  • To isolate the issue, have checked $srcOwner.Users.count? Is it showing 1? You can try adding " -Limit all" swtich, but not sure whether that will help Oct 2 '20 at 12:17
  • I tested again, and it's working for me. First try "$groupM.Users | select Title, email" to see if you have all the users showing up. email might be blank, so look into the Title as well Oct 2 '20 at 12:34
0

Please try below PowerShell.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

function global:Get-SPSite($url)
{
return new-Object Microsoft.SharePoint.SPSite($url)
}

function global:Get-SPWeb($url)
{
    $site= New-Object Microsoft.SharePoint.SPSite($url)
    if($site -ne $null)
        {
           $web=$site.OpenWeb();      
        }
     return $web
}

 $URL="site collection URL"

 $site = Get-SPSite $URL

 #Write the Header to "Tab Separated Text File"
    "Site Name`t  URL `t Group Name `t User Account `t User Name `t E-Mail" | out-file "c:\UsersandGroupsRpt.txt"
     
 #Iterate through all Webs
  foreach ($web in $site.AllWebs)
  {
    #Write the Header to "Tab Separated Text File"
    "$($web.title) `t $($web.URL) `t  `t  `t `t " | out-file "c:\UsersandGroupsRpt.txt" -append
     #Get all Groups and Iterate through   
     foreach ($group in $Web.groups)
     {
            "`t  `t $($Group.Name) `t   `t `t " | out-file "c:\UsersandGroupsRpt.txt" -append
            #Iterate through Each User in the group
                   foreach ($user in $group.users)
                    {
                       #Exclude Built-in User Accounts
                if(($User.LoginName.ToLower() -ne "nt authority\authenticated users") -and ($User.LoginName.ToLower() -ne "sharepoint\system") -and ($User.LoginName.ToLower() -ne "nt authority\local service"))
                {
                            "`t  `t  `t  $($user.LoginName)  `t  $($user.name) `t  $($user.Email)" | out-file "c:\UsersandGroupsRpt.txt" -append
                         }
                    }
     }
   }
write-host "Report Generated at c:\UsersandGroups.txt"

Reference:

https://www.sharepointdiary.com/2013/07/export-sharepoint-users-and-groups-to-excel-using-powershell.html

0

Here is the PnP PowerShell version of the script to get Default Groups' and Users' information from an on-premise site collection for the root and sub webs that have unique permissions. The output is saved into a *CSV file.You can modify it as needed.

Import-Module "C:\Program Files (x86)\SharePointPnPPowerShell2013\Modules\SharePointPnPPowerShell2013" -Verbose

$global:cred = Get-Credential

$SiteURL = "https://WebApp.com/sites/SiteCollName/"
$CSVFilePath="C:\Users\GroupAndUser.csv"


<#================================================================================
Functions:

ProcessWeb($web) - get the defaut groups of a given site
ProcessGroupUser([object] $group, [string]$url, [string]$type) - Process Groups and Users and stores in a global array

#>


Function ProcessWeb([object]$web)  
{  
     #change the context to the web
     $conW=Connect-PnPOnline -Url $web.Url -Credentials $global:cred 
    
     $url=$web.Url

     Write-host "Processing Site: " $web.Title " Url: " $url
     
     # Owner Group
     $groupOwner = Get-PnPGroup -AssociatedOwnerGroup             
     ProcessGroupUser $groupOwner $url "OWNER"
     

     # Member Group     
     $groupM= Get-PnPGroup -AssociatedMemberGroup
     ProcessGroupUser $groupM $url  'MEMBER'
      

     # Visitor Group
     $groupV = Get-PnPGroup -AssociatedVisitorGroup
     ProcessGroupUser $groupV $url 'VISITOR'
    
}  

Function ProcessGroupUser([object] $group, [string]$url, [string]$type)
{
     $users=$group.Users  #Title and Email
     
     if($users.count -eq 0)   {
      
         $groupuser= [PSCustomObject][ordered] @{             
                   GroupName=$group.Title
                   GroupType=$type
                   UserName="NO USER FOUND"
                   UserEmail="NO USER FOUND"
                   SiteUrl= $url               
                    } 

        $global:GroupAndUserData+=$groupuser
     }
     else  {
     
          Write-host "Processing Groups and Users for URL: " $url      
        
            foreach($user in $users) 
              {
                $groupuser= [PSCustomObject][ordered] @{            
                   GroupName=$group.Title
                   GroupType=$type
                   UserName=$user.Title
                   UserEmail=$user.Email
                   SiteUrl= $url               
                        }
                                            
                $global:GroupAndUserData+=$groupuser 
            
               } 
       }     

}

#================================================================================

#Initialize global variable to store groups and users
$global:GroupAndUserData=@()

#Connect to the Site Collection
Connect-PnPOnline -Url $SiteURL -Credentials $global:cred

#Process the root web First
$rootWeb= Get-PnPWeb
ProcessWeb $rootWeb 


#All the Webs that have unique permission
#Getting all sub webs with unique permission flag 
$subWebs =Get-PnPSubWebs -Recurse -Includes HasUniqueRoleAssignments

foreach($subweb in $subWebs)
{
   # Process all sub webs with UNIQUE permission
   if($subweb.HasUniqueRoleAssignments -eq $true)
   {
        ProcessWeb $subweb
   }
   else
   {
    
    Write-host "site" $subweb.Url "is inheriting permission from parent"

   }
}


#================================================================================
#Export the data to CSV
$global:GroupAndUserData | Export-Csv $CSVFilePath -NoTypeInformation
 
Write-host -f Green "Groups Report Generated Successfully!"

1
  • pls check my updated code. I am unable to retrieve all the user from the group
    – Liz
    Oct 2 '20 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.