Help
Help

Reputation: 181

PowerShell 2 Command To List Only Local Disabled User Accounts

I am restricted to PowerShell version 2, I have crafted a command that lists out all of the accounts in the disabled state:

Get-WmiObject Win32_UserAccount | where{$_.Disabled -eq "True"} | select Name, Disabled

This works well, however, it also lists DC user accounts, I am only trying to query local users.

Is there any way to query only local user accounts in the disabled state?

Upvotes: 0

Views: 3246

Answers (2)

postanote
postanote

Reputation: 16096

Old school Windows Tools still work, even from PowerShell regardless of version.

wmic useraccount get Name,Disabled

or

Start-Process powershell -ArgumentList '-NoExit', '-NoProfile', '-Command  &{ "wmic useraccount get Name,Disabled" }'
# Results
<#
Disabled  Name
TRUE      Administrator
...
#>

Update for proof relative to your comment...

...it seems to give me an Invalid GET Expression error message on PowerShell version 2

...

C:\>powershell -version 2.0 -nologo -noprofile
PS C:\> (Get-WmiObject -Class Win32_OperatingSystem).Caption
Microsoft Windows 10 Pro
PS C:\> $PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.9151
BuildVersion                   6.1.7600.16385
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1


PS C:\> wmic useraccount get Name
Name
Administrator
...

PS C:\> wmic useraccount get Disabled
Disabled
TRUE
..

PS C:\> wmic useraccount get "Name,Disabled"
Disabled  Name
TRUE      Administrator
...

Upvotes: 2

Daniel
Daniel

Reputation: 5114

Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount=True and Disabled=True" | Select-Object Name, Disabled

Upvotes: 4

Related Questions