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) is like a central hub that holds all the information about computers within a domain. Because of this, system administrators need to be able to efficiently query and manage these computer accounts in AD. You can easily retrieve and filter Active Directory computer accounts using PowerShell cmdlets.

Prerequisites

Before you dive into running PowerShell commands to fetch computer accounts, make sure you have the following in place:

  • You have administrative privileges to query Active Directory.
  • The Active Directory module for PowerShell is installed.
  • You’re executing the script on a system that’s joined to the domain or has the right permissions to query AD.

Importing the Active Directory Module

To get started with Active Directory in PowerShell, you’ll need to import the Active Directory module. If it’s not already installed, you might have to set up the Remote Server Administration Tools (RSAT).

Import-Module ActiveDirectory

Retrieving All Computer Accounts

If you want to pull up a complete list of all computer accounts in the domain, you can use the Get-ADComputer cmdlet like this:

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

This command will give you all the computer accounts along with their names, operating systems, and last logon dates.

Filtering Computer Accounts by Organizational Unit (OU)

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

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

Just replace OU=Computers,DC=example,DC=com with the correct OU path for your domain.

Filtering Computer Accounts by Operating System

If you’re looking to list all computers that are running Windows Server, you can do it like this:

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

Finding Inactive Computer Accounts

To identify inactive computer accounts, you can check the LastLogonDate property:

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

This command will pull up all the computers that haven’t logged in for the past 90 days.

Exporting the List to a CSV File

If you want to save this list of computer accounts to a CSV file for reporting or further analysis, you can use the following command:

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

Final Thoughts

PowerShell makes it easy to manage and filter computer accounts in Active Directory. From retrieving all accounts to finding inactive ones or exporting results, these commands help streamline AD administration and keep your environment organized.