Solved

Powershell script disable users in CSV file and move to OU

Posted on 2013-10-28
3
6,151 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
3 Comments
 
LVL 53

Accepted Solution

by:
Will Szymkowski earned 500 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 21

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now