Avatar of CiscoAzn
CiscoAzn
Flag for United States of America asked on

Is there a script to move all disabled AD accounts over to a specified OU?

I have multiple sites/OU and each one have disabled accounts that i would want to automatically move to a different OU. Is there a Powershell script for this?
PowershellActive DirectoryWindows Server 2008

Avatar of undefined
Last Comment
CiscoAzn

8/22/2022 - Mon
becraig

Here is an easy one, includes the creation of the OU in the event you do not yet have the destination OU created:
#Import AD Module
Import-Module ActiveDirectory

#Check for existence of OU and create if not present
[string] $Path = 'OU=StaleComputers,DC=domain,DC=com'
try
{
	if (!([adsi]::Exists("LDAP://$Path")))
	{
		#Create OU since it does not yet exist
		NEW-ADOrganizationalUnit “StaleComputers” –path “DC=domain, DC=com”
	}
	else { Write-Debug "OU Already Exists:  $Path" }
}
catch [Exception]    {
	return $_.Exception.Message
}


#now we proceed to check for computers
Get-ADComputer -Filter { Enabled -eq $false } | Move-ADObject -TargetPath $Path -WhatIf

Open in new window



If you already have the OU then this will work:


 $Path = 'OU=StaleComputers,DC=domain,DC=com'
Get-ADComputer -Filter { Enabled -eq $false } | Move-ADObject -TargetPath $Path -WhatIf

Open in new window


Remove the whatif to actually run.
CiscoAzn

ASKER
What about pointing to a specific OU for the source of all the sites with the disabled accounts and the destination would the $Path. I don't want to run it for the entire domain.
ASKER CERTIFIED SOLUTION
becraig

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CiscoAzn

ASKER
I did a test and found out why it didn't work. Had to change from ADComputer to ADUser so this works perfectly thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
becraig

No prob, yeah I'd actually done this for computers on another question and just copy and pasted.
becraig

Did you need anything else on this question ?

If so let me know and I will be happy to help.
Satyendra Tiwari

Hi Cisco,
You can download the complete script from this given resource and I hope, it will help you to resolve your purpose for moving all disabled AD accounts to specific OU : https://gallery.technet.microsoft.com/scriptcenter/Move-and-disable-inactive-b1cf86c3
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
becraig

This question was indicated as answered in comment 40458167
Satyendra Tiwari

This question was indicated as answered in comment 40458167

Ok, i did not see that.
CiscoAzn

ASKER
becraig I want to run this as a scheduled task and not able to. Do you have a way to do this? What is the Action that needs to be input to have this working?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck