Solved

Powershell script disable users in CSV file and move to OU

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

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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 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 …

773 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