Solved

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

Posted on 2013-01-17
9
5,070 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Resolve DNS query failed errors for Exchange
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

770 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