Solved

script to check if a list of users' AD account is disabled.

Posted on 2013-01-17
9
5,539 Views
Last Modified: 2013-02-01
Hello,

I have a text file which contains a user list and would like to create a script to check if their AD account is disabled and echo the names if they are.  

What command should I use in my script?

Thanks.
0
Comment
Question by:nav2567
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Assisted Solution

by:Yagya Shree
Yagya Shree earned 125 total points
ID: 38788521
Use below powershell script

Search-ADAccount -accountinactive -usersonly  | where {! ($_.lastlogondate -lt (get-date).addMonths(-6))} | ft Name,lastlogondate
0
 

Author Comment

by:nav2567
ID: 38788907
If my user text file is C:\USERS.TXT.  

Would you help me to create a script to read my USERS.TXT and list out all the "DISABLED" users?
0
 
LVL 5

Assisted Solution

by:coraxal
coraxal earned 125 total points
ID: 38788966
If you don't mind installing ActiveRoles Management Shell for Active Directory (Free PowerShell Commands for Active Directory), then you could do something like this:

http://www.quest.com/powershell/activeroles-server.aspx


$UserCol = Get-Content C:\users.txt

$UserCol | ForEach-Object {
       
        $user = Get-QADUser -SearchRoot "dc=yourdomain,dc=com" -Identity $_ -DontUseDefaultIncludedProperties -IncludedProperties AccountIsDisabled

       if($user.AccountIsDisabled){
             
               Write-Output "User account:  $($user.NTAccountName) is disabled"

        }
}

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:nav2567
ID: 38789306
Thanks.

Would you modify the script to output the disabled account names into c:\disabled-users.txt?

Also, what extension should I use to save this scipt so it will be opened by ActiveRoles Management Shell automatically when I double click on it?
0
 
LVL 54

Assisted Solution

by:McKnife
McKnife earned 250 total points
ID: 38789400
A simple line would do.
for /f %a in (c:\test\users.txt) do net user %a |findstr /C:"Account active               No" && echo %a is deactivated >>c:\test\deactivatedusers.txt

Open in new window


->Please scroll the code rightwards
0
 
LVL 5

Expert Comment

by:coraxal
ID: 38789595
$UserCol = Get-Content C:\users.txt
$DisabledUsers = @()

$UserCol | ForEach-Object {
       
        $user = Get-QADUser -SearchRoot "dc=yourdomain,dc=com" -Identity $_ -DontUseDefaultIncludedProperties -IncludedProperties AccountIsDisabled

       if($user.AccountIsDisabled){
             
               Write-Output "User account:  $($user.NTAccountName) is disabled"
			   $DisabledUsers += $_.NTAccountName

        }
}

$DisabledUsers | Set-Content C:\DisabledUsers.txt

Open in new window



You'll have to save the script with a ".ps1" extension.  So say you save the script in C:\scripts, open up a shell window, cd to C:\scripts, and type the name of the script "myscript.ps1"
0
 

Author Comment

by:nav2567
ID: 38792540
I tried the first PS1 script yesterday and it works.  Thanks.

I also tried the other "simple line" but it did not work.  

I would like to look into one more option which is to use the Windows native command DSQUERY USER.  

Anyone knows how to do this?
0
 
LVL 3

Expert Comment

by:jeorge
ID: 38792633
hi check out this scripts written by Richard L Mueller

http://www.rlmueller.net/Programs/IsUserLocked.txt

Hope it works for you.
0
 
LVL 54

Accepted Solution

by:
McKnife earned 250 total points
ID: 38792818
> I also tried the other "simple line" but it did not work.
You tried it - on your DC? In order for it to work, is has to be executed on the DC. It works on mine (win2008 and 2012). If you need to execute it from a workstation, you need to add /domain, so that would be
for /f %a in (c:\test\users.txt) do net user %a /domain |findstr /C:"Account active               No" && echo %a is deactivated >>c:\test\deactivatedusers.txt

Open in new window


Tested that also, works, too.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
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…

839 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