Phillip fyhn
Phillip fyhn

Reputation: 41

Import users to groups using CSV

Hi im fairly new at the whole scripting scene, and im trying to make a script that will import users, i have listet in a csv file into a AD group. What i would like to know is, how the format should go. My csv file contrains " Group|Name|SamAccountName|Path|Enabled|Password" I would like to know how this line is configured

"add-adgroupmember -identity $user.Group -member"

Upvotes: 0

Views: 748

Answers (1)

Eldo.Ob
Eldo.Ob

Reputation: 794

Here, that should work

but before using the script you definetly should get to know something about:

Add-ADGroupMember

Add-ADPrincipalGroupMembership

And you should be shure about your CSV and the delimiters. With the wrong cmdlet or using of a cmdlet you can produce a lot of troubles in an ActiveDirectory :)

Import-Csv -Delimiter ";" -Path "P:\\ath\to\your\file.csv" | % {

  $group = get-ADGroup -Filter { Name -like $_.Group } -Server dom-company.com
  $user = Get-ADUser -Filter { SamAccountName -eq $_.SamAccountName } -Server dom-company.com
  if($group.Name -eq $_.Group -and $user.SamAccountName -eq $_.SamAccountName){
    Add-ADPrincipalGroupMembership -Identity $user -MemberOf $group
  }

}

Upvotes: 1

Related Questions