Sign up to access this lesson
Click here to sign up and get access to this lesson!

Saving Progress...
In this lecture, we are going to be learning how we can automatically move disabled users to our Disabled Users OU. This is kind of a little cleanup script that we are going to create and run. It can come in handy when you have several disabled users.
We open Active Directory Users and Computers and we see we have three disabled users.

If you have hundreds of disabled users in your Active Directory Domain, and they are not in the Disabled Users OU, we can write a script that will automatically move them there.
To create the script open Windows PowerShell ISE by clicking on the bottom left on the Windows icon and type powershell. Click on Windows PowerShell ISE.

The Windows PowerShell ISE opens up and looks like the following:

Let’s type the following code to list all disabled users:
# Import the AD Module
Import-Module ActiveDirectory
# List all disabled AD users
Search-ADAccount -AccountDisabled | Select-Object Name, DistinguishedName
Click the green Play icon and we should see the output at the bottom pane.

From the output, we can see we have disabled users in different Containers and OUs.
First, make sure Advanced Features is enabled in Active Directory Users and Computers.

Then, we select the Disabled OU and right-click and select Properties.

Now from the Properties window click on the Attribute Editor tab and double-click on the distinguishedName line to see the location.
Copy the value as it will be needed for our script.

Now what we are going to do is move all these disabled user accounts. Let’s add more lines of code to the script.
# Import the AD Module
Import-Module ActiveDirectory
# List all disabled AD users
Search-ADAccount -AccountDisabled | Select-Object Name, DistinguishedName
# Move all disabled AD users to disabled users OU
Search-ADAccount -AccountDisabled | Move-ADObject -TargetPath "OU=Disabled Users,OU=instructorpaul,DC=instructorpaul,DC=com"
Now, we click the run script icon (green Play button) and the script will get executed.

Open Active Directory Users and Computers to verify the disabled users have been moved to the Disabled Users OU.
Click Refresh. We now see all the disabled users under the OU.

Another thing we can do is, for example, we have a user that has been placed in the Disabled Users OU but the account is not disabled.
If we move an enabled user like Ridley Quin to the Disabled Users OU but forgot to disable the user account we can also make the script disable the user accounts that are inside the Disabled Users OU.
Let’s move the user to the Disabled Users OU by clicking on it and dragging it to Disabled Users and dropping it there.

Let’s add the following to our script:
# Import the AD Module
Import-Module ActiveDirectory
# List all disabled AD users
#Search-ADAccount -AccountDisabled | Select-Object Name, DistinguishedName
# Move all disabled AD users to disabled users OU
#Search-ADAccount -AccountDisabled | Move-ADObject -TargetPath "OU=Disabled Users,OU=instructorpaul,DC=instructorpaul,DC=com"
# Disable all users in the disabled users OU
Get-ADUser -Filter {Enabled -eq $True} -SearchBase "OU=Disabled Users,OU=instructorpaul,DC=instructorpaul,DC=com" | Select-Object Name
Click the Run script button.
From the output, we can see we are selecting the account Ridley Quin because it is in the Disabled Users OU and its Enabled.

Now let’s update the script to perform the action.
# Import the AD Module
Import-Module ActiveDirectory
# List all disabled AD users
Search-ADAccount -AccountDisabled | Select-Object Name, DistinguishedName
# Move all disabled AD users to disabled users OU
#Search-ADAccount -AccountDisabled | Move-ADObject -TargetPath "OU=Disabled Users,OU=instructorpaul,DC=instructorpaul,DC=com"
Sign up to access the rest of this lesson
You must either log in or sign up to access this lesson.
CURRICULUM
Course Introduction • 1min
0 / 1 lessons complete
Getting Started with Active Directory Domain Services • 52min
0 / 6 lessons complete
Section Overview
Free lesson
Text | 1 min
Installing the ADDS (Active Directory Domain Services) Server Role
Video | 7 min
Promote the Server to a Domain Controller
Video | 7 min
Forests, Trees and Domains
Video | 2 min
Windows Domain Quiz
Quiz | 5 Questions
Lab: Installing the Active Directory Domain Services
Lab | 30 min
Introduction to Active Directory Users & Computers • 1hr 23min
0 / 10 lessons complete
Section Overview
Free lesson
Text | 2 min
Organizational Units (OUs) and Containers
Video | 5 min
Creating, managing and deleting OUs
Video | 5 min
Creating User Accounts with Active Directory
Video | 8 min
Searching for Objects in Active Directory
Video | 6 min
Resetting User Passwords and Unclocking Accounts in Active Directory
Video | 6 min
Understanding Groups and Memberships
Video | 11 min
Disabling and Deleting User Accounts
Video | 6 min
Active Directory Quiz
Quiz | 5 Questions
Lab: Active Directory Users and Computers
Lab | 30 min
Adding a Second Domain Controller • 1hr 30min
0 / 7 lessons complete
Notice of change in Lab Names
Text | 2 min
Section Overview
Free lesson
Video | 2 min
Promoting Our Second Domain Controller
Video | 13 min
Flexible Single Master Operation (FSMO) Roles Overview
Free lesson
Video | 4 min
Transferring FSMO (Flexible Single Master Operations) Roles
Video | 5 min
Adding a Second Domain Controller Quiz
Quiz | 5 Questions
Lab: Adding a Second Domain Controller
Lab | 60 min
Active Directory Backups • 1hr 24min
0 / 5 lessons complete
Creating an Active Directory / System State Backup
Video | 4 min
Restoring an Active Directory Backup
Video | 7 min
Creating and Restoring Active Directory Snapshots
Video | 8 min
Active Directory Backups Quiz
Quiz | 5 Questions
Lab: Active Directory Backups
Lab | 60 min
How to Administrate Active Directory with Windows PowerShell • 1hr 58min
0 / 7 lessons complete
Enabling Script Execution for PowerShell
Video | 6 min
Listing AD Users with PowerShell
Video | 14 min
Creating AD Users with PowerShell
Free lesson
Video | 10 min
Creating User Accounts from a CSV (Comma Separated Value) File
Video | 15 min
Move All Disable Users to Disabled Users OU with PowerShell
Video | 8 min
AD PowerShell Quiz
Quiz | 5 Questions
Lab: ADUC PowerShell Automation
Lab | 60 min
Administrating AD SS (Active Directory Sites and Services) • 1hr 3min
0 / 5 lessons complete
Active Directory Sites and Services - Section Overview
Free lesson
Video | 8 min
Configuring Our ITFROUTE01 Server to act as a Router
Video | 11 min
Configuring Active Directory Sites and Services
Video | 9 min
Administrating AD SS Quiz
Quiz | 5 Questions
Lab: Configure ADDS Between Two Subnets
Lab | 30 min
Active Directory Trusts • 54min
0 / 5 lessons complete
Window Trusts Explained
Free lesson
Video | 6 min
Configuring DNS Settings for Active Directory Trusts
Video | 6 min
Establish a Two-way Active Directory Trust
Video | 7 min
Active Directory Trusts Quiz
Quiz | 5 Questions
Lab: Establish a two-way AD Trust
Lab | 30 min
Modifying the Active Directory Schema • 43min
0 / 3 lessons complete
Add Custom Attributes to Active Directory Users
Video | 8 min
Active Directory Schema Quiz
Quiz | 5 Questions
Lab: Extending the Active Directory Schema
Lab | 30 min
Course Conclusion • 1min
0 / 1 lessons complete