Script To Disable Users Or Computers In Active Directory And Move To Specified OU

Script To Disable Users Or Computers And Move To Specified OU.

As a Domain Administrator it is our duty to audit and monitor our complete Active Directory Environment.

Some of the Users or Computers may remain inactive for quite long time. There are lots of options available to identify these inactive lists of Users and Computers.

Once after the identification is over, we need to disable that particular Users or Computers and have to move the same to the specified OU of an Active Directory.

If the list is so long then it is impossible to do it manually and it takes lot of time to finish the task.

In order to avoid that and to finish it through automation here the simple script which I created and using it whenever there is a requirement for it.

By completion of this tasks it will helps us to maintain the proper and updated inventory of our Network environment.

Here will see about the,

Command Line Script to Disable Users or Computers from Text File and Moving It to Specified OU.


There are lot of PowerShell scripts are used and available for performing the above said tasks.

But considering the security most of the organizations will disable access to PowerShell.

Hence I chose command line script here.

Disable Users or Computers from Text File


First will see about the scripts that are required for disabling Users / Computers from Text file or CSV.

@echo off
set ComputerList=path/filename
for /f “delims=” %%a in (‘type “%ComputerList%”‘) do (
echo Processing given data  %%a …
dsquery computer -name “%%a” | dsmod computer -disabled yes
@echo off
FOR /f %%i in (path/filename) do (
echo processing OU data migration
echo SET OFF
echo import-module ActiveDirectory
echo $comp = Import-Csv -Path “D:\Scripts\Disableusers.txt “
echo foreach ($line in $comp){
echo Get-ADComputer $line.CN | Move-ADObject -TargetPath ‘OU=Inactive Users and Computers,DC=DOUBTSCLEAR,DC=com’
echo }

dsquery computer -name %%i | dsmove -newparent OU=Inactive Users and Computers,DC=DOUBTSCLEAR,DC=com

Echo on

Before executing, modify the path specified and script based on your AD setup.

This command will disable all the users or Computers are listed under text file or CSV file.


Moving the Disable Users or Computers to the Specific OU.


Now we have done disabling the required users or computers and we want to move it to the specified OU.

@echo off
FOR /f %%i in (D:\Scripts\Disableusers.txt) do (
  dsquery computer -name %%i | dsmove -newparent OU=Inactive Users and Computers,DC=DOUBTSCLEAR,DC=com

As I said before modify the path specified and scripts based on your environmental setup.

Everything is done, Now just relax and enjoy watching it.


Must Read:  

Active Directory Complete Guide


Active Directory Complete Guide


Read the Complete Article in below Link:


Hope this Tutorial helps you Guys.. !!  if this Article found useful Don’t Forget to Like..!!! Share ..!!! Comment ..!!


Check Also

Active Directory Users and Computers

Active Directory Users And Computers

In this post will see about the basics of Active Directory Users and Computers, what …

Leave a Reply

Your email address will not be published. Required fields are marked *