Powershell 365 - Remove email from distribution group - powershell

$Email = "#"
Get-DistributionGroup | where { (Get-DistributionGroupMember $_.Name | foreach {$_.PrimarySmtpAddress}) -contains "$Email"}
Results: Name, DisplayName, Group Type, PrimarySMTPAddress
I need to remove the user's email address from the distribution group.
I know it will be a foreach command.
Does anyone know how to run that command?

You can use the If statement within a For loop that determines if the email address exists in the distribution group.
If this condition is satisfied, that's when you want to run the Remove-DistributionGroupMember cmdlet:
$Email = "#"
Get-DistributionGroup |
ForEach-Object {
If ((Get-DistributionGroupMember -Identity $_.Name).PrimarySmtpAddress -contains $Email) {
Remove-DistributionGroupMember -Identity $_.Name -Member $Email -WhatIf}
}
}

Related

list users that are not part of a few distribution lists PowerShell Exchange online

I'm looking to find users whos primary smtp is our domain.com that are not part of certain office365 distribution lists we have. For example Dist1, Dist2, Dist3, Dist4. I'm not very good with PowerShell but I found this script and I'm hoping someone can help me adjust this.
This script pulls the group membership of all groups.
Get-Mailbox | Where-Object {$_.PrimarySMTPAddress -like "*domain.com"} | ForEach-Object {
$user = Get-User -Identity $_.DistinguishedName
$groups = Get-Group | Where-Object {$_.Members -contains $User}
$_ | Select-Object DisplayName, Alias, PrimarySMTPAddress,
#{Name = 'Groups' ; Expression = {$groups.Name -join '; '}}
} | Export-Csv -Path 'X:\O365UserGroups.csv' -NoTypeInformation
I have made a script that checks if users whos primary smtp is your domain.com and if they are members of distributionsgroup 1 or 2. If the user is not a member of a distributionsgroup it will output this. You can add export to csv etc easily yourself.
$Users = Get-Mailbox | ? {$_.PrimarySmtpAddress -like "*domain.com"} #Enter your domain
$DistributionGroups = #("dist1","dist2") #Enter names of your distributiongroups
foreach($DistributionGroup in $DistributionGroups)
{
$DistributionGroupMembers = Get-DistributionGroupMember $DistributionGroup
foreach($User in $Users)
{
foreach($DistributionGroupMember in $DistributionGroupMembers)
{
if($DistributionGroupMember.PrimarySmtpAddress -ne $User.PrimarySmtpAddress)
{
Write-Host "$($User.PrimarySmtpAddress) missing in $DistributionGroup" #Export output here fx.
}
}
}
}

Export Email Address and Mobile Number from Office 365 Distribution Group

I can't seem to find a way to export an email address and a phone number from a specific DG.
Hey guys,
I'm trying to get an email and a phone number from each member in a DG. I can't get the phone number.
I used any words I could think of
Get-DistributionGroupMember -Identity $DG -ResultSize Unlimited | Select PrimarySMTPAddress, Mobile, MobilePhone,
Phone, PhoneNumber, MobileNumber
I also tried
Get-MsolGroupMember -GroupObjectId | select DisplayName, MobileNumber
Nothing yields results. If I try the same comment with a simple Get-MsolUser it WILL work. Why?
It's this field in the Exchange admin:
Any ideas?
Thanks :)
AFAIK the Exchange cmdlet Get-DistributionGroupMember returns an array of ReducedRecipient member objects where not all AD user properties are stored.
In order to get the user properties you need, you will also have to use either the Get-ADUser or the Get-MsolUser cmdlet.
The code below uses Get-ADUser (untested)
$result = Get-DistributionGroupMember -Identity $DG -ResultSize Unlimited |
Where-Object { $_.RecipientType -eq 'UserMailbox' } |
ForEach-Object {
$user = Get-ADUser -Identity $_.SamAcountName -Properties MobilePhone, HomePhone, OfficePhone
[PsCustomObject]#{
'DistributionGroup' = $DG
'DisplayName' = $_.DisplayName
'PrimarySmtpAddress' = $_.PrimarySmtpAddress
'MobilePhone' = $user.MobilePhone
'HomePhone' = $user.HomePhone
'OfficePhone' = $user.OfficePhone
'City' = $_.City
'Country' = $_.CountryOrRegion
}
}
# output on screen
$result
# output to CSV
$result | Export-Csv -Path ('D:\DistributionGroupMembers_{0}.csv' -f $DG) -NoTypeInformation -Encoding UTF8
In order to use the Get-ADUser cmdlet you need to Import-Module ActiveDirectory.

Filtering AD Group by User Properties ; Returning improperly

Disclaimer: I am not good with powershell, this in mainly butchered code. I apologize if this is done poorly or is a stupid question.
I am trying to filter the ACTIVE users in my company by their company (ET) and whether or not they are in a certain group.
So the filter for ACTIVE users in the company "ET" is working properly, the output of this script gives me every active users with that parameter; it does not filter it further down into only users in a certain group.
$users = Get-ADUser -filter {(Enabled -eq $True) -and (Company -eq "ET")}
-SearchBase 'DC=CSOKI,DC=Local' |select -exp samaccountname
$group = "O365-E3-Full"
$members = Get-ADGroupMember -Identity $group -Recursive | Select -
ExpandProperty samaccountname
ForEach ($user in $users) {
If ($members -contains $user) {
Write-output $(name) | out-file ".\TEST.txt"
} Else {
Write-Host "$user does not exist in the group"
}}
Expected:
Output ACTIVE users in company ET that are in group O365-E3-FULL and write-host users that are not(unnecessary, I just want the filter).
Actual:
Write-hosts every ACTIVE user in company ET and ignores the group filter.
In getting your list of users you are collecting the account name for the users with:
| Select -exp samaccountname
Then in getting group members you are getting the Name with:
| Select -ExpandProperty Name
You need to be selecting SamAccountName in both of your Gets
Sorry, pretty quick knock together
# Create empty array
$answer = New-Object System.Collections.ArrayList
# If is in group then add to array
If ($members -contains $user) {
$answer.Add($user) > $null
} Else {
Write-Host $user "does not exist in the group"
}
# Output the array to the text file
Write-output $answer | out-file ".\TEST.txt"

Display Distribution Lists and Members for Intermedia HostPilot PowerShell

I am still learning my way around PowerShell and I'm trying to get some data out of Intermedia using their Intermedia HostPilot PowerShell tool.
First I start out by adding all the Distribution Group information to my $Groups array:
$Groups = Get-DistributionGroup
I am able to get the DisplayName and EmailAddress of those in Distribution Groups, however I can't tell which user is in which group:
for ($i=0; $i -lt $Groups.length; $i++)
{ Get-DistributionGroupMember -Identity $Groups[$i].DistinguishedName |
Select DisplayName, EmailAddress }
I found the script below online (https://www.morgantechspace.com/2015/06/powershell-export-distribution-list-members-to-csv.html) which was helpful but I still don't see the members of the group in my csv file, just a list of the distribution groups:
$Groups = Get-DistributionGroup
$Groups | ForEach-Object {
$group = $_.GUID
$members = ''
Get-DistributionGroupMember $group | ForEach-Object {
If($members) {
$members=$members + ";" + $_.GUID
} Else {
$members=$_.GUID
}
}
New-Object -TypeName PSObject -Property #{
GroupName = $group
Members = $members
}
} | Export-CSV "C:\\Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8
Ideally I would like to have an additional column that displays the Distribution Group for each user. Something like this:
DistributionGroup DisplayName EmailAddress
accounting Rob Smith rob.smith#yahoo.com
accounting John Quincy john.quincy#yahoo.com
This is one variation I tried:
for ($i=0; $i -lt $Groups.length; $i++)
{ Get-DistributionGroupMember -Identity $Groups[$i].DistinguishedName |
Select DisplayName, EmailAddress, $Groups[$i].DisplayName }
This just gives me a heading with the name of the first distribution group, like this:
DisplayName EmailAddress Accounting
Any tips are welcome. Thanks!
I really don't know what the Intemedia HostPilot powershell commands are but in plain powershell you could go with something like that :
$DGroups = Get-ADGroup -Filter {(GroupCategory -eq "Distribution")} | select Name
Foreach ($Group in $DGroups)
{
write-host""
write-host "Members of the >>> "$Group.Name" <<< are:"
write-host "--------"
$Users = Get-ADGroupMember $Group.Name | Select samaccountname | sort Samaccountname
Foreach ($user in $users)
{
Get-ADUser -Identity $user.samaccountname -Properties * | select Name, Samaccountname, mail, displayname
}
}
I am posting it as an answer as the code in the comments is not displayed really well.
The $Result object matches the desired format example you gave and is ready to be output to the console, piped to Out-GridView, or exported to CSV.
$DGroups = Get-DistributionGroup
$Result = #()
foreach ( $DGroup in $DGroups ) {
$DGroup | Get-DistributionGroupMember | Select-Object DisplayName, EmailAddress | ForEach-Object {
$Result += [PSCustomObject]#{
DistributionGroup = $DGroup.DisplayName
DisplayName = $_.DisplayName
EmailAddress = $_.EmailAddress
}
}
}
Hope this helps.

Powershell trying to copy users from one AD group to another

I am trying to copy from one ad group to another. However I am having trouble with piping the data and feeding the add-group command with array items please help
Write-Host "You have selected the following group: $SourceGroup"
$SourceGroup = Read-Host
Write-Host "Enter the name of the new group you want the membership of $SourceGroup copied to"
$DestinationGroup = Read-Host
$P1 = Get-ADGroup $SourceGroup -Property member | Select member | Format-Table -HideTableHeaders
$P2 = Foreach ($Member in $P1) { Get-ADUser $Member -Property SamAccountName | Select SamAccountName }
Foreach ($Member in $P1) { Add-ADGroupMember -Identity $DestinationGroup -Member $Member }
You break it by using Format-Table. Formatting is for display, you should not format anything you expect to work with in the shell. By all means format it at the end once you're done.
Anyway, you should be able to simplify it.
Get-ADGroup $SourceGroup | Get-ADGroupMember | ForEach-Object {
Add-ADGroupMember $DestinationGroup -Member $_.DistinguishedName
}
Chris