PowerShell script to add users to groups based on user's DN RRS feed

  • Question

  • I need to create a PowerShell script to add users to groups based on User's DN. 

    I have a CSV file contains user's DN like:

    CN=test User1,OU=FULLTIME,DC=example,DC=com
    CN=test User2,OU=PARTTIME,DC=example,DC=com

    Basically, I need to create a PowerShell script to read this file, if find user's DN contains OU=FULLTIME put user into the fulltime group. if find user's DN contains OU=PARTTIME put user into the parttime group. 

    Any help would be greatly appreciated.


    Tuesday, January 23, 2018 9:48 PM

All replies

  • The groups you mentioned, are you referring to AD groups?
    Tuesday, January 23, 2018 9:55 PM
  • claim csv file -> for each item in CSV file read first 8 char after ou= -> if fulltime add to group ->if parttime add to group 

    EDIT:but pull from a CSV in this case would cause problems, because CN=test user1 and the OU are different items in a CSV.

    good luck young hemp knight.

    • Edited by The Grim Tuesday, January 23, 2018 9:59 PM
    Tuesday, January 23, 2018 9:56 PM
  • The full distinguished names in the CSV file must be in quotes, since the values have embedded commas.

    Also, best if the CSV has a header line.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, January 23, 2018 10:28 PM
  • yes, these are AD global groups
    Tuesday, January 23, 2018 10:28 PM
  • What does your CSV look like.  What you have posted is not a CSV.

    To match a certain string:

    if($line -match 'fulltime'){
        $groupname = 'FullTime'

    You can use a switch statement for a list of matches.

    help about_switch

    The kind of group is unimportant. 


    • Edited by jrv Tuesday, January 23, 2018 10:55 PM
    Tuesday, January 23, 2018 10:54 PM
  • here is a sample of the CSV file

    "CN=test User1,OU=FULLTIME,DC=example,DC=com"
    "CN=test User2,OU=PARTTIME,DC=example,DC=com"

    Tuesday, January 23, 2018 11:56 PM
  • Import-Csv <yourfile> |
               if($_Name -match 'fulltime'){
                     # add to fulltime group

    It is as simple as that or use a switch.


    Wednesday, January 24, 2018 12:04 AM
  •  if $line -match 'fulltime' $groupname = 'FullTime'

    Wednesday, January 24, 2018 6:28 PM
  •  if $line -match 'fulltime' $groupname = 'FullTime'

    This is PowerShell and not VB. Your post is not helpful.


    Wednesday, January 24, 2018 6:47 PM