Jerry Seinfield
asked on
Powershell script to automatically populate a security group in AD for all users with a regular mailbox
Hello Experts,
I need some help with an script that I am writing. Basically, I need to populate a security group in AD with all users from my domain with a regular mailbox assigned.
The script must not populate all mail enabled objects, because this would add shared mailboxes, DLs and so on. I just need regular users with their mailbox to be added to a security group
I wrote the following
Import-Module ActiveDirectory
$user = Get-ADUser -filter *
$Group = "TenantCompanyName"
ForEach ($samAccountName in $user)
{
Add-ADGroupMember $Group -members $samAccountName
}
I would like to replace the filter * with something like below
Get-ADUser -Filter {EmailAddress -like "*"}
Get-ADUser -Filter {mail -like "*"}
-or-
Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")}
For example, add JohnDoe@mydomain.com to the group above and so on
Again, this script is supposed to add all users from the domain with a valid email address to a security group. Shared mailboxes, DLs, and any other mail enabled object should be excluded
Any help is highly appreciated
I need some help with an script that I am writing. Basically, I need to populate a security group in AD with all users from my domain with a regular mailbox assigned.
The script must not populate all mail enabled objects, because this would add shared mailboxes, DLs and so on. I just need regular users with their mailbox to be added to a security group
I wrote the following
Import-Module ActiveDirectory
$user = Get-ADUser -filter *
$Group = "TenantCompanyName"
ForEach ($samAccountName in $user)
{
Add-ADGroupMember $Group -members $samAccountName
}
I would like to replace the filter * with something like below
Get-ADUser -Filter {EmailAddress -like "*"}
Get-ADUser -Filter {mail -like "*"}
-or-
Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")}
For example, add JohnDoe@mydomain.com to the group above and so on
Again, this script is supposed to add all users from the domain with a valid email address to a security group. Shared mailboxes, DLs, and any other mail enabled object should be excluded
Any help is highly appreciated
Why dont you simply use the Exchange cmdlets? Get-Recipient in particular can easily be used to return only user mailboxes:
Get-Recipient -RecipientTypeDetails UserMailbox
ASKER
sure, and how you would adapt that to my script? how would the script look?
Anyone else?
Anyone else?
IMO, you must have the RSAT tools installed (Install-WindowsFeature RSAT-ADDS) on the Exchange server and run the following script from the Exchange management shell...
Let us know.
Import-Module ActiveDirectory
$USER = Get-Recipient -RecipientTypeDetails UserMailbox
$GROUP = "TenantCompanyName"
$USER | ForEach-Object { Add-ADGroupMember $GROUP -Members $_.Alias }
Let us know.
ASKER
Thanks Todd,
Can you or someone else please test my script in your lab domain?
Import-Module ActiveDirectory
# script to populate all regular users with a valid email address
# to a Security Group in AD
$user = Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")}
$Group = "Tenant-EntAppsALL"
ForEach ($samAccountName in $user)
{
Add-ADGroupMember $Group -members $samAccountName
}
Can you or someone else please test my script in your lab domain?
Import-Module ActiveDirectory
# script to populate all regular users with a valid email address
# to a Security Group in AD
$user = Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")}
$Group = "Tenant-EntAppsALL"
ForEach ($samAccountName in $user)
{
Add-ADGroupMember $Group -members $samAccountName
}
This should do it...
Import-Module ActiveDirectory
$USER = Get-ADObject -Filter {(Mail -like "*") -and (ObjectClass -eq "User")} -Properties *
$GROUP = "Tenant-EntAppsALL"
ForEach ($samAccountName in $USER) { Add-ADGroupMember $GROUP -Members $samAccountName }
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER