locked
How to move bulk AD Groups between OU's RRS feed

  • Question

  • Hi Everyone,

    I would like to move over 50 AD Groups between OU's and stamp the notes box with a comment. 

    I am having trouble with the move to start of, this is the script I found:

    $Move=import-csv D:\Temp\Em_Groups.csv
    Import-module ActiveDirectory
    foreach ($Group in $Groups){
        get-adgroup $Group|move-adobject -targetpath "OU=Cleanup,OU=Disabled Accounts,DC=blue,DC=net"
    }
    

    The script is not working (not moving the groups to new OU).

    Any ideas please?

    M


    Maelito

    Thursday, May 26, 2016 3:30 PM

Answers

  • In that case this will do what you're after:

    Import-Csv .\groupList.csv | ForEach-Object {
    
        Set-ADGroup -Identity $_.GroupName -Replace @{info='Your comment goes here'}
    
        Get-ADGroup -Identity $_.GroupName |
            Move-ADObject -TargetPath 'OU=Cleanup,OU=Disabled Accounts,DC=blue,DC=net'
    
    }

    I generally prefer ForEach-Object loops over foreach loops, as I find them more intuitive and easier to work with.

    I also just noticed that your original script was storing the input in $Move rather than the $Groups variable you were using in your loop. Since $Groups has nothing in it, the loop didn't have anything to do.


    • Proposed as answer by Elaine Jing Monday, May 30, 2016 9:04 AM
    • Marked as answer by Elaine Jing Wednesday, June 1, 2016 7:22 AM
    Thursday, May 26, 2016 4:11 PM

All replies

  • Hi,

    You're not referencing the header when using $Group. Assuming the header of your input CSV is GroupName, you should use $Group.GroupName.


    Thursday, May 26, 2016 3:41 PM
  • Hi Mike,

    Thanks for always being online! 

    I don't follow you on this one?

    What am I doing wrong here and how can I tag the group with a comment in "notes"?

    M


    Maelito

    Thursday, May 26, 2016 3:59 PM
  • =]

    What does the first few rows of your input CSV look like? Knowing that will help make this easier to explain.


    Thursday, May 26, 2016 4:03 PM
  • Ah!

    GroupName
    G-Reports-FC
    G-Reports-RO
    G-Canda RO
    G-Pricing Team-RO
    G-Brasil-RO
    


    Maelito

    Thursday, May 26, 2016 4:05 PM
  • In that case this will do what you're after:

    Import-Csv .\groupList.csv | ForEach-Object {
    
        Set-ADGroup -Identity $_.GroupName -Replace @{info='Your comment goes here'}
    
        Get-ADGroup -Identity $_.GroupName |
            Move-ADObject -TargetPath 'OU=Cleanup,OU=Disabled Accounts,DC=blue,DC=net'
    
    }

    I generally prefer ForEach-Object loops over foreach loops, as I find them more intuitive and easier to work with.

    I also just noticed that your original script was storing the input in $Move rather than the $Groups variable you were using in your loop. Since $Groups has nothing in it, the loop didn't have anything to do.


    • Proposed as answer by Elaine Jing Monday, May 30, 2016 9:04 AM
    • Marked as answer by Elaine Jing Wednesday, June 1, 2016 7:22 AM
    Thursday, May 26, 2016 4:11 PM
  • Also you are importing the CSV into the $Move variable but then using the $Groups variable which based on what you have posted is empty.

    I think you want

    foreach ($Group in $Move.GroupName) {

    Thursday, May 26, 2016 4:25 PM
  • Awesome that worked brilliantly.

    Yes, I do remember you saying that you prefer "foreach-Object" rather the foreach.

    Would this also help in reporting as in Output/export?

    M


    Maelito

    Thursday, May 26, 2016 4:33 PM
  • Would this also help in reporting as in Output/export?
    Sure, as you're dealing with a pipeline and you can then just pipe any results you're after into Export-Csv or Out-File.

    Thursday, May 26, 2016 4:58 PM