Solved

Powershell script disable users in CSV file and move to OU

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
A hard and fast method for reducing Active Directory Administrators members.
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…
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…

729 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