2

My first question on this forum so hopefully I am doing this right. I have written a script based off of a bit of code I found on this site but it is not producing what I need and I am not sure why. The original code is as follows:

$Mailbox=get-Mailbox xxxx@company.com
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter

This produced the output of a list of distribution lists the user was a member of with 4 columns, Name, DisplayName, GroupType, PrimarySmtpAddress. This works great. I added to this to build a tool where you enter in the users email address but now the output looks like all the properties of the Distribution Lists the user is a member of. Here is my complete code:

Import-Module MSOnline
$LiveCred = Get-Credential 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/PowerShell  -Credential $LiveCred -Authentication Basic -AllowRedirection 
Import-PSSession -allowclobber $Session

Connect-MsolService -Credential $LiveCred
cd c:\scripts

Write-Host "This tool displays the Distrbution lists a user is a member of"
$User = Read-Host -Prompt 'email address you would like to find the distribution lists of'

$Mailbox=get-Mailbox "$User"
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter
Mike_D
  • 45
  • 1
  • 8

1 Answers1

0

I assume you want only those four properties. Change the last line in your code to something like below:

Get-DistributionGroup -ResultSize Unlimited -Filter $Filter | Select-Object Name,DisplayName,GroupType,PrimarySmtpAddress
Aman Sharma
  • 1,930
  • 1
  • 17
  • 31
  • That worked! One followup question would be that I would like to also output this information to a file. Thanks. – Mike_D May 09 '16 at 13:14
  • I am glad it helped. Store this information in a variable and then use `Out-File` cmdlet to output to the file. Refer this SO post: [Write output to a text file in PowerShell](http://stackoverflow.com/questions/18469104/write-output-to-a-text-file-in-powershell) – Aman Sharma May 09 '16 at 13:50