PowerShell - Add and Remove users from Active Directory Groups

 


PowerShell script to add and remove users from active directory groups on-prem

#import the ActiveDirectory Module

Import-Module ActiveDirectory

 

$users = @(

'Emily.Jones@contoso.com'

'Max.Wilson@contoso.com'

'Olivia.Davis@contoso.com'

'Ethan.Lee@contoso.com'

'Ava.Thompson@contoso.com'

'Lucas.Garcia@contoso.com'

'Mia.Patel@contoso.com'

'Benjamin.Taylor@contoso.com'

'Isabella.Nguyen@contoso.com'

'Alexander.Johnson@contoso.com'

)

 

foreach($u in $users){

    #PowerShellRemoveE3

    Remove-ADGroupMember -Identity 'Office365_F3' -Members (Get-ADUser -Filter "userPrincipalName -eq '$u'" | Select -expand Name) -confirm:$false

    #PowerShellAddF3

    Add-ADGroupMember -Identity 'Office365-E3' -Members (Get-ADUser -Filter "userPrincipalName -eq '$u'" | Select -expand Name)

}


Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.