How to Get the List of Computer Accounts in an Active Directory Domain Using PowerShell

This article explains how to use Powershell to retrieve a list of computer accounts.

Active Directory (AD) contains information about all computers within a domain and therefore, system administrators need to query and manage computer accounts in AD efficiently. Active Directory computer accounts can be retrieved and filtered using PowerShell cmdlets.

Prerequisites

Before running PowerShell commands to fetch computer accounts, ensure the following:

  • You have administrative privileges to query Active Directory.

  • The Active Directory module for PowerShell is installed.

  • You are running the script on a system joined to the domain or with proper permissions to query AD.

Importing the Active Directory Module

To work with Active Directory in PowerShell, you need to import the Active Directory module. If it is not installed, you may need to install the Remote Server Administration Tools (RSAT).

Import-Module ActiveDirectory

Retrieving All Computer Accounts

To get a complete list of all computer accounts in the domain, use the Get-ADComputer cmdlet:

Get-ADComputer -Filter * | Select-Object Name, OperatingSystem, LastLogonDate

This command retrieves all computer accounts and displays their name, operating system, and last logon date.

Filtering Computer Accounts by Organizational Unit (OU)

To fetch computers from a specific Organizational Unit (OU), use the SearchBase parameter:

Get-ADComputer -Filter * -SearchBase "OU=Computers,DC=example,DC=com" | Select-Object Name, OperatingSystem

Replace OU=Computers,DC=example,DC=com with the appropriate OU path in your domain.

Filtering Computer Accounts by Operating System

To list all computers running Windows Server, use:

Get-ADComputer -Filter "OperatingSystem -like '*Windows Server*'" | Select-Object Name, OperatingSystem

Finding Inactive Computer Accounts

Inactive computer accounts can be identified using the LastLogonDate property:

Get-ADComputer -Filter * -Properties LastLogonDate | Where-Object { $_.LastLogonDate -lt (Get-Date).AddDays(-90) } | Select-Object Name, LastLogonDate

This retrieves computers that have not logged in for the last 90 days.

Exporting the List to a CSV File

To save the list of computer accounts to a CSV file for reporting or further analysis:

Get-ADComputer -Filter * | Select-Object Name, OperatingSystem, LastLogonDate | Export-Csv -Path C:\ComputerList.csv -NoTypeInformation