Move Groups - Powershell

Hi Guys,

We have groups with a -m in the name and a -r in the name. e.g:


All the R groups with a corresponding M, I want to move these to an OU called deprovsion, so far I have written this script, please help me complete it:

Get-QADGroup -SizeLimit 0 -SearchRoot "OU=Departmental Share Security,OU=Resource Groups,OU=Groups,DC=yu,DC=DS,DC=rt,DC=ac,DC=uk" | ?{$_.Name -match "-M$"} | % {
$S = $_
$D = $S.Name -replace "-M$","-R"

 if ( Get-QADGroup $D )

       #move to OU OU=Deprovision,OU=Departmental Share Security,OU=Resource Groups,OU=Groups,DC=yu,DC=DS,DC=rt,DC=ac,DC=uk



thank you in advance
KaySenior Systems AdministratorAsked:
Who is Participating?
Chris DentConnect With a Mentor PowerShell DeveloperCommented:
ADUC GUI is the AD Users and Computers management console. It's  a bit of a pointless comment really.

Move-QADObject is the CmdLet you want if you've managed to identify the groups you're most interested in.

I'd probably do:
Get-QADGroup -Name "*-M" -SizeLimit 0 -SearchRoot "OU=Departmental Share Security,OU=Resource Groups,OU=Groups,DC=yu,DC=DS,DC=rt,DC=ac,DC=uk" -SearchScope OneLevel |
  Where-Object { Get-QADGroup -Name ($_.Name -replace "-M$","-R") } |
  ForEach-Object {
    $_ | Move-QADObject -NewParentContainer "OU=Deprovision,OU=Departmental Share Security,OU=Resource Groups,OU=Groups,DC=yu,DC=DS,DC=rt,DC=ac,DC=uk" -WhatIf

Open in new window

Your original Where-Object filter is wrapped back into Get-QADGroup. The check for a matching -R group is performed in the Where-Object filter, it won't error if it fails to find one.

The search is limited to only the "Departmental Share Security" OU, using OneLevel means it won't find anything in a sub-OU (and therefore try and move things twice).

Finally, all matching groups are moved using Move-QADObject.

WhatIf is included in the Move command to allow you to test it, the code won't do anything other than chat about changes as it stands.


Will SzymkowskiSenior Solution ArchitectCommented:
I have found it much easier to move groups using the ADUC GUI, rather then scripting this procedure.

Not helpful in regards to your script but might be more effective.

KaySenior Systems AdministratorAuthor Commented:
what is ADUC GUI, where do i find it?
AmitIT ArchitectCommented:
ADUC means Active Directory Users and Computers snap-in. You can login to one of the DC. Goto Start>Run>dsa.msc

This will open ADUC for you. Now right click on the domain name>click find>type group name>click find>right click the group name>select Move>Browse to destination US Path>Click Ok.

Let me know, if you still have any doubt or query.
All Courses

From novice to tech pro — start learning today.