Solved

Powershell script disable users in CSV file and move to OU

Posted on 2013-10-28
3
6,032 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now