powershellactive-directoryactive-directory-group

Adding groups to AD user with a ps1 script


With following script I'd like to add a group to an AD user, but it doesn't do it, and I get no error.

param($Userid,$AdditionalGroup)

# Get user
$User = Get-ADUser `
    -Filter "SamAccountName -eq $Userid"

# Add comment
Add-ADGroupMember `
    -Identity $AdditionalGroup `
    -Members $User

Solution

  • Filtering like that didn't work for me (and generated an error), however adding ' before and after $Userid did the trick.

    param($Userid,$AdditionalGroup)
    
    # Get user
    $User = Get-ADUser `
        -Filter "SamAccountName -eq '$Userid'"
    
    # Add comment
    Add-ADGroupMember `
        -Identity $AdditionalGroup `
        -Members $User