?
Solved

Powershell script disable users in CSV file and move to OU

Posted on 2013-10-28
3
Medium Priority
?
6,862 Views
Last Modified: 2015-10-09
Hi All

I am after a powershell script using the microsoft cmdlets that will disable users whos samaccountnames are in a csv file and also move them to a specified OU

Thanks
0
Comment
Question by:ncomper
[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
3 Comments
 
LVL 53

Accepted Solution

by:
Will Szymkowski earned 2000 total points
ID: 39605554
You can use the following powershell command below to accomplish this...

CSV file needs to be constructed like below...

sAMAccountName           DN                                          TargetOU
jsmith                          "OU=test,DC=domain,DC=com"    "OU=NewOU,DC=domain,DC=com"
mdavis                         "OU=test,DC=domain,DC=com"    "OU=NewOU,DC=domain,DC=com"
So on...

Script below...

Import-Module activedirectory
$UserList = Import-Csv "c:\yourCSVhere.csv"
foreach ($User in $UserList) {
$User.sAMAccountName
$User.DN
$User.TargetOU
Set-ADUser -Identity $User.sAMAccountName -Enabled $false
Pause -s 30
Move-ADObject -Identity $User.DN -TargetPath $User.TargetOU
}

Open in new window


Will.
1
 
LVL 5

Author Closing Comment

by:ncomper
ID: 39617373
Thanks Will
0
 
LVL 22

Expert Comment

by:dan_blagut
ID: 41031856
Hello
A little variation for a lazzy admin. If you don't want to have a complex input file and the target OU is the same for all users:

Import-Module activedirectory
$UserList = Import-Csv "e:\script\deact.txt"
foreach ($User in $UserList) {
$User.sAMAccountName
Set-ADUser -Identity $User.sAMAccountName -Enabled $false
$local=get-aduser -Identity $User.sAMAccountName
Move-ADObject -Identity $local.DistinguishedName -TargetPath "OU DistinguishedName"
}
 

In fact Move-ADObject can't use samaccountname, only DistinguishedName is ok, but we can obtain DistinguishedName from samaccountname.

PS Will thank's for the script.

Dan
0

Featured Post

WatchGuard's M Series Appliances - Miecom Approved

WatchGuard's newest M series appliances were put to the test by Miercom.  We had great results and outperformed all of our competitors in both stateless and stateful traffic throghput scenarios! Ready to see how your UTM appliance stacked up? Download the Miercom Report!

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

770 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