none
Creating AD Groups from CSV import in powershell RRS feed

  • Question

  • Hi Guys

    This seems like the sort of thing that *should be easy, but i am having no luck at all. I should say I am a scripting newb. :)

    I have a CSV file with a few fields, groupname, grouptype, grouplocation

    the only one i really need is the name, as they will all be security groups and live in the same OU. 

    i've tried a hundred variations of the same thing, and cant get anything but errors. I know it's something simple i'm doing wrong, maybe someone can help?

    import-module activedirectory
    $groups = import-csv H:\!scripts\AAAgroups.csv
    foreach ($group in $groups) {
    new-adgroup -name $groups.GroupName -GroupCategory Security -GroupScope global -Path $Groups.GroupLocation
    }

    That gives me 

    'New-ADGroup : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported.'

    So i tried wrapping some quotes around the variables in the new-adgroup command

    new-adgroup -name "$groups.GroupName" -GroupCategory Security -GroupScope global -Path "$Groups.GroupLocation"

    which gives me "new-adgroup : The object name has bad syntax", i assume because it's trying to use "$groups.groupname" as the name.

    so i need to get my groupname into a string, without quotes. so i tried this

    import-module activedirectory
    $groups = import-csv H:\!scripts\AAAgroups.csv
    $name = out-string -inputobject $groups.GroupName
    $OU = out-string -inputobject $groups.grouplocation
    foreach ($group in $groups) {
    new-adgroup -name $name -GroupCategory Security -GroupScope global -Path $OU
    }

    same thing, bad syntax. So i added quotes around all sorts of things and nothing wants to work. If I run this, then type $name (or $groups.name) - sure enough, i get my list of names. same thing with $ou. I just cant get them to plug into the new-adgroup cmdlet.

    I tried a couple of (much more complicated) scripts around on the net and had various other problems with them too. It seems like it should be as easy as something like what i'm trying, but i just cant get it to work.

    thanks in advance


    • Edited by Kai Tingey Thursday, November 12, 2015 12:10 AM typo
    Thursday, November 12, 2015 12:08 AM

Answers

All replies

  • Hi,

    You're not referencing the current item in the loop. You should be using $group instead of $groups.

    http://ss64.com/ps/foreach.html


    • Marked as answer by Kai Tingey Thursday, November 12, 2015 1:46 AM
    Thursday, November 12, 2015 1:10 AM
  • Thanks for that Mike, i knew it would be something really simple. Doh!

    Thursday, November 12, 2015 1:46 AM
  • Cheers, you're very welcome. Sometimes fresh eyes are all you need.

    Thursday, November 12, 2015 1:47 AM
  • I know this is an old thread, but I'm in the same boat. Can you show me what you changed to get it to create the groups? I also am trying to assign a description to the group. I looked at the link, but a I'm still unsure on all of the terminology so I couldn't figure out the problem. 
    Saturday, March 3, 2018 5:40 PM
  • I know this is an old thread, but I'm in the same boat. Can you show me what you changed to get it to create the groups? I also am trying to assign a description to the group. I looked at the link, but a I'm still unsure on all of the terminology so I couldn't figure out the problem. 

    You need to open your own question because you are asking a new question.  This one is closed and answered and very old.

    Post an example of your code and any full error messages when you open your post.


    \_(ツ)_/

    Saturday, March 3, 2018 6:37 PM