Solved

Need to move selected user with a specific attribute to an OU from their existing OU

Posted on 2016-11-08
4
69 Views
Last Modified: 2016-11-10
My requirement is to move few users from an OU to a different OU. All users are currently under "SAMPLE" OU and all of them have an attribute "xyz" set with a value '3'.

ex: user "Test" is located in OU=Sample,DC=Contoso,DC=com. He's got an attribute named "xyz" set with value '3'. I would like to move this user to OU=Sample1,DC=Contoso,DC=com

My requirement is to move user accounts with attribute name "xyz" of value 3 from an OU for ex: OU=Sample,DC=COntoso,DC=com to OU=Sapmple1,DC=Contoso,DC=com

Additionally, all those accounts which are supposed to be moved are disabled and i need to enable them as well.

Let me know if any other information is required

Thanks in advance
0
Comment
Question by:A D
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41878860
In test mode; remove the -WhatIf in line 5 to run it for real.
$Attribute = 'xyz'
$Value = 3
$SourceOU = "OU=Sample,DC=Contoso,DC=com"
$TargetOU = "OU=Sample1,DC=Contoso,DC=com"
Get-ADUser -SearchBase $SourceOU -Filter "$Attribute -eq '$Value'" | Move-ADObject -TargetPath $TargetOU -WhatIf

Open in new window

0
 

Author Comment

by:A D
ID: 41878867
Thanks oBdA. I'll test the script. Maybe I should have mentioned to move all users from "SAMPLE" OU to "SAMPLE1" only if they are disabled and have an attribute "xyz" set with value "3" and later enable them as well.

Can you add "enable" part to the script?
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41878885
Two "-WhatIf"s now, line 6 and 7. Note that line 7 will now not be executed, because the WhatIf in line 6 doesn't pass anything on. You can just comment out line 6 to verify that it's going to move the correct accounts.
$Attribute = 'Comment'
$Value = 3
$SourceOU = "OU=Sample,DC=Contoso,DC=com" 
 $TargetOU = "OU=Sample1,DC=Contoso,DC=com" 
Get-ADUser -SearchBase $SourceOU -Filter "($Attribute -eq '$Value') -and (Enabled -eq 'False')" |
	Set-ADUser -Enabled $True -PassThru -WhatIf |
	Move-ADObject -TargetPath $TargetOU -WhatIf

Open in new window

0
 

Author Closing Comment

by:A D
ID: 41880306
Thanks oBdA. This worked perfectly.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question