Solved

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

Posted on 2016-11-08
4
74 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 85

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 85

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Here's a look at newsworthy articles and community happenings during the last month.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

636 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