Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

List Disabled Users via Powershell

Posted on 2010-08-25
6
Medium Priority
?
1,445 Views
Last Modified: 2012-05-10
Hi there,

I would like to know how can I list all the disabled users via Powershell in Windows 2008 SP2.

Does anyone know a Cmdlet or a script for it??

Thanks.
0
Comment
Question by:ney_santos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Accepted Solution

by:
flexiwebsw earned 500 total points
ID: 33525190
Try this code
 Save it to a file with the '.ps1' extention then run it with the arguments zshown below:

C:\Disabled.ps1 -domain mydomain -q

make sure your computer allows unsigned scripts to run

to enable this, run powershell as admin, then type in these commands:

Set-ExecutionPolicy

Then when it asks for the policy type in this one:

RemoteSigned

then press enter followeed by Y then enter

you should now be able to execute unsigned scripts, but if you use the pre-made one attached then you will have to right click on the download and select 'UnBlock'
param(
$domain=$env:userdomain,
[switch]$query,
[switch]$whatif,
[switch]$help,
[switch]$examples,
[switch]$min,
[switch]$full
) #end param
# Begin Functions
function funHelp()
{
$descriptionText= `
@"
NAME: LocateDisabledUsers.ps1 
DESCRIPTION:
Locates disabled users a local or remote domain by
supplying the netbios name of the domain. 
The script can query multiple domains by accepting 
more than one value for the -domain parameter. The 
script also supports using -whatif to prototype the 
command prior to execution
PARAMETERS: 
-domain the domain or domains to query for locked 
out users. Note: this is the netbios domain name.
Does not accept fully qualified domain name. For
example: nwtraders is correct, nwtraders.com is 
not.
-query executes the query
-whatif prototypes the command.
-help prints help description and parameters file
-examples prints only help examples of syntax
-full prints complete help information
-min prints minimal help. Modifies -help
"@ #end descriptionText
$examplesText= `
@"
SYNTAX:
LocateDisabledUsers.ps1
Displays an error missing parameter, and calls help
LocateDisabledUsers.ps1 -query
Queries disabled user accounts. The domain queried is
the local logged on users domain from the machine 
that launched the script 
LocateDisabledUsers.ps1 -domain nwtraders, contoso -query
Queries disabled user accounts in the nwtraders domain and
in the contoso domain. The script is executed locally 
LocateDisabledUsers.ps1 -query -domain nwtraders -whatif
Displays what if: Perform operation locate disabled 
users from the nwtraders domain.The query will execute 
from the localhost computer
LocateDisabledUsers.ps1 -help
Prints the help topic for the script
LocateDisabledUsers.ps1 -help -full
Prints full help topic for the script
LocateDisabledUsers.ps1 -help -examples
Prints only the examples for the script
LocateDisabledUsers.ps1 -examples
Prints only the examples for the script
"@ #end examplesText
$remarks = `
"
REMARKS
For more information, type: $($MyInvocation.ScriptName) -help -full
" #end remarks
if($examples) { $examplesText ; $remarks ; exit }
if($full) { $descriptionText; $examplesText ; exit } 
if($min) { $descriptionText ; exit }
$descriptionText; $remarks 
exit
} #end funHelp function
function funline (
$strIN,
$char = "=",
$sColor = "Yellow",
$uColor = "darkYellow",
[switch]$help
)
{
if($help)
{
$local:helpText = `
@"
Funline accepts inputs: -strIN for input string and -char for seperator
-sColor for the string color, and -uColor for the underline color. Only 
the -strIn is required. The others have the following default values:
-char: =, -sColor: Yellow, -uColor: darkYellow
Example:
funline -strIN "Hello world"
funline -strIn "Morgen welt" -char "-" -sColor "blue" -uColor "yellow"
funline -help
"@
$local:helpText
break
} #end funline help
$strLine= $char * $strIn.length
Write-Host -ForegroundColor $sColor $strIN 
Write-Host -ForegroundColor $uColor $strLine
} #end funLine function
Function funWhatIf()
{
foreach($sDomain in $Domain)
{
"what if: Perform operation locate disabled users from the $sDomain domain"
}
exit
} #end funWhatIf
Function funQuery()
{
Foreach($sDomain in $domain)
{
$strOutput = Get-WmiObject -Class win32_useraccount -filter `
"domain = ""$sDomain"" AND disabled = 'true'"
$count = ($strOutput | Measure-Object).count
If($count -eq 0)
{ 
funline -scolor green -ucolor darkyellow -strIN `
"There are no disabled accounts in the $sDomain" 
} #end if
ELSE
{
funline -scolor red -ucolor darkyellow -strIN `
"$count disabled in the $sDomain domain -- List follows:" 
format-table -property name, sid -AutoSize -inputobject $strOutput
} #end else
} #end foreach
exit
} #end funquery
# Entry Point
if($help) { funhelp }
if($examples) { funhelp }
if($full) { funhelp }
if($whatif) { funWhatIf }
if(!$query) { "missing parameter" ; funhelp }
if($query) { funQuery }

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 33525353
If you use and import the ActiveDirectory module of R2 then:

Search-ADAccount -AccountDisabled
0
 
LVL 13

Assisted Solution

by:soostibi
soostibi earned 500 total points
ID: 33525370
Or, more precisely:

Search-ADAccount -AccountDisabled -usersonly
0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 57

Expert Comment

by:Mike Kline
ID: 33525489
The Quest AD cmdlets can also help  http://www.quest.com/powershell/activeroles-server.aspx
Get-QADUser -SL 0 -disabled | FT displayname,sammaccountname
Thanks
Mike
0
 

Author Comment

by:ney_santos
ID: 33525864
Thanks flexiwebsw, your script is perfect.

Soostibi... How can I import the R2 module on Win 2k8 SP2? Can I download it and install?

Mkline71... I can't install anyother thirty party modules on this enviroment, but thanks anyway.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33526256
You can add the AD DS Remote Server Administration Tools feature with Server Manager and then in the PowerShell window run:
 import-module activedirectory
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
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 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…
Suggested Courses

721 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