2

I am using this script to export groups and users in a SP group. I am having trouble exporting to csv, how can I do it?

$siteUrl = "SiteCollection"
$web = Get-SPWeb $siteUrl


 $site = Get-SPSite $URL

 if (Get-SPWeb($url).HasUniqueRoleAssignments -eq $true)
 {
    $Web=Get-SPWeb($url)
 }
 else
 {
    $web= $site.RootWeb
 }

 #Get all Groups and Iterate through   
 foreach ($group in $Web.sitegroups)
 {
    write-host " Group Name: "$group.name "`n---------------------------`n"
        #Iterate through Each User in the group
               foreach ($user in $group.users)
                {
                    write-host $user.name  "`t" $user.LoginName  "`t"  $user.Email  | FT
                }
 write-host "=================================="  #Group Separator
 }

1 Answer 1

1
  • Define a variable as $output = @()

  • and add

foreach ($user in $group.users)
                {
                   $output += $user.name  "`t" $user.LoginName  "`t"  $user.Email  | FT
                }
  • Then export
$Output| export-csv -Path c:\folder\export.csv -NoTypeInformation

Alternative to use Out-file to can append new data

Surround foreach with $() | Out-file c:\root\export1.txt -Append

$(foreach ($group in $Web.sitegroups)
 {
    write-host " Group Name: "$group.name "`n---------------------------`n"
        #Iterate through Each User in the group
               foreach ($user in $group.users)
                {
                 $group.name 
                }
 })| Out-file c:\root\export1.txt -Append
1
  • Bit of a PS novice here. Where do I put/format in my script :Define a variable as $output = @() Commented Aug 11, 2016 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.