powershell saving data exporting to a file

I have this command which gives me the output I need but having problems exporting the contents to a file. Can anyone help all the contents are printed to a screen


[PS] C:\temp>foreach ($group in Get-DistributionGroup -OrganizationalUnit "Microsoft.com/master/users") { get-dist
ributiongroupmember $group | sort displayname | ft @{expression={$_.displayname};Label="$group"},alias, RecipientTypeDet
ails, Identity } export-csv c:\temp\alldata.csv -append

Open in new window



This just prints to the screen. I tried select and do not get the first name.
techdriveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan McFaddenSystems EngineerCommented:
You are missing a pipe right before the export-csv command.

Dan
0
techdriveAuthor Commented:
Wish it was that easy

At line:1 char:245
+ ... ls, Identity } | export-csv c:\temp\allstuff.csv
                    ~
an empty pipe element is not allowed.
   + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
   + FullyQualifiedErrorId : EmptyPipeElement
0
Dan McFaddenSystems EngineerCommented:
OK, I'm not a big fan of one-liners like this.

I suggest that it is done as a script.  See below:

$groups = Get-DistributionGroup -OrganizationalUnit "Microsoft.com/master/users" -ResultSize Unlimited
$output = @()

foreach ($group in $groups)
{
    Write-Host "working on group : " $group
    $dgm = get-distributiongroupmember $group | select DisplayName,Alias,RecipientTypeDetails,Identity | sort displayname
        foreach ($m in $dgm)
        {
            $temp = New-Object PSObject
            Write-Host $dgm.Alias
            $temp | Add-Member -MemberType NoteProperty -Name  "Group" -Value $m.DisplayName
            $temp | Add-Member -MemberType NoteProperty -Name  "Alias" -Value $m.Alias
            $temp | Add-Member -MemberType NoteProperty -Name  "RecipientTypeDetails" -Value $m.RecipientTypeDetails
            $temp | Add-Member -MemberType NoteProperty -Name  "Identity" -Value $m.Identity
            $output += $temp
        }
}

$Output | Export-Csv alldata.csv -NoTypeInformation -UseCulture -Encoding UTF8

Open in new window


Dan
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

techdriveAuthor Commented:
The script looks good but functions different.
0
Dan McFaddenSystems EngineerCommented:
Of course, you are trying to grab info from 3 different arrays, query additional info based on the outer arrays.  It's a bit too much for a 1 liner.

Also, the command looped thru the $group and performed a Get-DistributionGroupMember call without storing the info you collected.  Best case you would only get the info from the last group in your list.  It would go to screen, but there isn't anything to drop into the export-csv command.

Is it giving you all the necessary info?

Dan
0
techdriveAuthor Commented:
I wanted something like this at minimum



groupname
      member1
      member2
      member3

groupname
       member1
       member2
0
techdriveAuthor Commented:
Hey nevermind I found something to fix the issue but thanks

-AutoSize  | Out-File c:\temp\alldata.txt -append}
0
Amit KumarCommented:
Hi, please try this one:

$saveto = "D:\Data\Members.txt"

Get-DistributionGroup -OrganizationalUnit "Microsoft.com/master/users" -ResultSize Unlimited | sort name | ForEach-Object {

	"`r`n$($_.Name)`r`n=============" | Add-Content $saveto
	Get-DistributionGroupMember $_ | sort Name | ForEach-Object {
		If($_.RecipientType -eq "UserMailbox")
			{
				"," + $_.Name + " (" + $_.PrimarySMTPAddress + ")" | Add-Content $saveto
			}
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
techdriveAuthor Commented:
Amit thanks it works perfect. One final question if I wanted to add additional fields to the recipient types can you share the syntax, thanks
0
Amit KumarCommented:
Great to know, hope you can accept the solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.