EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell User List
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell User List

PowerShell User List

Definition of PowerShell User List

PowerShell User list is a way to retrieve the users from the local windows machines or the active directory users using the specific cmdlets like Get-LocalUser for the local users on windows OS and Get-ADUsers for the active directory users to retrieve the user details like Distinguished Name (DN), GUID, Security Identifier (SID), Security Account Manager (SAM) or name and can be exported to the CSV or the text file.

Syntax

Syntax of PowerShell User List are given below:

Get-LocalUser syntax:

Get-LocalUser
[[-Name] <String[]>] [<CommonParameters>] Get-LocalUser
[[-SID] <SecurityIdentifier[]>] [<CommonParameters>]

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Get-ADUser syntax:

Get-ADUser
[-AuthType <ADAuthType>] [-Credential <PSCredential>] -Filter <String>
[-Properties <String[]>] [-ResultPageSize <Int32>] [-ResultSetSize <Int32>] [-SearchBase <String>] [-SearchScope <ADSearchScope>] [-Server <String>] [<CommonParameters>]

Get-ADUser
[-AuthType <ADAuthType>] [-Credential <PSCredential>] [-Identity] <ADUser>
[-Partition <String>] [-Properties <String[]>] [-Server <String>] [<CommonParameters>]

Get-ADUser
[-AuthType <ADAuthType>] [-Credential <PSCredential>] -LDAPFilter <String>
[-Properties <String[]>] [-ResultPageSize <Int32>] [-ResultSetSize <Int32>] [-SearchBase <String>] [-SearchScope <ADSearchScope>] [-Server <String>] [<CommonParameters>]

From the Get-ADUser syntax, you can use any set of combinations and similarly for the Get-LocalUser account. You can’t use the -Filter and -Identity property together in Get-ADUser cmdlet and -Name and -SID property in Get-LocalUser cmdlet.

We can also use the WMI or CIMInstance class Win32_UserAccount  to retrieve the local user details from the local or the remote computers. In addition “Net User” command in cmd is also helpful to retrieve the local user list.

How to List Users in PowerShell?

There are various methods to list the users in the PowerShell by using the Native commands like Get-LocalUser which retrieves the local user account details from the local computer or the remote computers or the Get-ADUser which retrieves the users from the Active Directory domain.

Get-LocalUser command was introduced in PowerShell 5.1 and it is part of Microsoft.PowerShell.LocalAccounts module. In the earlier PowerShell version, to retrieve the list of users you either need to download the local accounts module or you need to use the cmd command like Net User (which still works) or the WMI method class Win32_UserAccount.

If you are remoting to the older PowerShell version machines then the Get-LocalUser command won’t work there. When you run the local user commands on the domain controller, it will provide you the active directory users because DC doesn’t have the local users.

Examples of PowerShell User List

Following are the examples are given below:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,294 ratings)

Example #1: Getting Local User Accounts List Using Cmd

We can use the “Net User” cmd command to retrieve the user list from the cmd or the PowerShell.

net user

Output:

PowerShell User List-2.1

  • To get the local users list from the remote computer use Invoke-Command in PowerShell,

Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock { net user}

Output:

PowerShell User List-2.2

The above output is from the remote computer LabMachine2k16. 

Example #2: Retrieving the Local Users Using the Wmi Method

We can use the WMI method or the CIM instance method class Win32_UserAccount to retrieve the user list.

gwmi win32_UserAccount | Select Name, FullName, Caption, Domain, SID | ft -AutoSize

Output:

PowerShell User List-2.3

  • To get the local user details on the remote computer, you need to add the -ComputerName

Invoke-Command -ComputerName LabMachine2k16 { gwmi win32_UserAccount} | Select Name, FullName, Caption, Domain, SID | ft -AutoSize

You can also use the Get-CimInstance command instead of the gwmi method.

You can export the output file to the text or CSV as shown below. In the below example, the output will be exported to the UserAccounts.Csv file.

gwmi win32_UserAccount | Select Name, FullName, Caption, Domain, SID | Export-Csv C:\Temp\Useracccounts.csv -NoTypeInformation

To export into the text file.

gwmi win32_UserAccount | Select Name, FullName, Caption, Domain, SID | Out-File C:\temp\UserAccounts.txt

If you want to check the specific user or the domain user list,

gwmi win32_UserAccount | where{$_.Domain -like "LabMachine2k16*"} | Select Name, FullName, Caption, Domain, SID  | ft -AutoSize

Output:

Output-2.4

  • To retrieve the local accounts only with the Password properties use the below command,

gwmi win32_useraccount | where{$_.LocalAccount -eq $true}  | Select Name, LocalAccount, PasswordChangeable, PasswordRequired

Example #3: Retrieving the ADUsers List

To get the domain user list, you can use the Get-ADUser command. To run this command you need to make sure that you have the RSAT (Remote Server Administration Tools) installed on the computer.

Get-ADUser -Filter *

The above command will get all users from the active directory domain.

Output-2.5

  • To expose all the properties of the users, you can use the -Property

Get-ADUser -Filter * -Properties *

  • To filter the specific properties,

Get-ADUser -Filter * -Properties * | Select Name, DisplayName, SamAccountName, UserPrincipalName

Output:

Output-2.6

  • To get the list of the users who have accounts Disabled and export it to the CSV file, use the below command.

Get-ADUser -Filter * | where {$_.Enabled -eq $false} | Export-Csv C:\DisabledUserAccounts.csv -NoTypeInformation

  • To get the specific user accounts details,

Get-ADUser -Identity beta -Properties *

You need to use the SamAccountName property in the -Identity parameter.

  • To retrieve the users from the specific Organization Unit (OU), use the below command.

Get-ADUser -Filter * -Properties * | where{$_.DistinguishedName -like "*CN=Users*"} | Select Name, DisplayName, userPrincipalName, SAMAccountName

  • Retrieving the list of users in the active directory domain whose password is expired or set to never expires with password properties in the output.
  • Retrieves the Expired Password user accounts.

Get-ADUser -Filter * -Properties * | where{$_.PasswordExpired -eq $true} | Select Name, SAMAccountName, PasswordExpired, PasswordLastSet

  • Retrieves the Password never expires user accounts.

Get-ADUser -Filter * -Properties * | where{$_.PasswordNeverExpires -eq $true} | Select Name, SAMAccountName, PasswordNeverExpires

Conclusion

As explained in this article, PowerShell uses the various commands to retrieve the list of the Users from the windows computer or from the active directory domain and that is helpful for administrators for their audit and clean-up tasks.  You can use the task scheduler to send emails to Administrators for the list of created, expired, about to expire accounts monthly.

Recommended Articles

This is a guide to PowerShell User List. Here we also discuss the definition and syntax of PowerShell User List along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. PowerShell Environment Variable
  2. PowerShell Write to Console
  3. PowerShell ZIP
  4. PowerShell Multiline String
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more