0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Get Computer Objects from AD

Use the PowerShell command Get-ADComputer -Filter * to retrieve all computer objects from Active Directory.
📋

Examples

InputGet all computer objects
OutputDistinguishedName : CN=COMPUTER1,OU=Computers,DC=domain,DC=com Name : COMPUTER1 ObjectClass : computer ...
InputGet computer objects with name starting with 'SRV'
OutputDistinguishedName : CN=SRV01,OU=Servers,DC=domain,DC=com Name : SRV01 ObjectClass : computer ...
InputGet computer objects in a specific OU
OutputDistinguishedName : CN=PC01,OU=Workstations,DC=domain,DC=com Name : PC01 ObjectClass : computer ...
🧠

How to Think About It

To get computer objects from Active Directory, you use a command that queries AD for objects of type 'computer'. You can filter to get all computers or specify conditions like name or location. The command returns details about each computer object.
📐

Algorithm

1
Connect to Active Directory using PowerShell.
2
Run a query to get all computer objects or filtered ones.
3
Collect the results and display them.
💻

Code

powershell
Import-Module ActiveDirectory
$computers = Get-ADComputer -Filter * -Properties Name,OperatingSystem
foreach ($computer in $computers) {
    Write-Output "$($computer.Name) - $($computer.OperatingSystem)"
}
Output
COMPUTER1 - Windows 10 Pro SRV01 - Windows Server 2019 PC01 - Windows 11 Enterprise
🔍

Dry Run

Let's trace getting all computer objects and printing their names and OS.

1

Import Active Directory module

Module ActiveDirectory loaded.

2

Get all computer objects

Retrieve list: COMPUTER1, SRV01, PC01 with their OS properties.

3

Print each computer's name and OS

Output lines: 'COMPUTER1 - Windows 10 Pro', 'SRV01 - Windows Server 2019', 'PC01 - Windows 11 Enterprise'

Computer NameOperating System
COMPUTER1Windows 10 Pro
SRV01Windows Server 2019
PC01Windows 11 Enterprise
💡

Why This Works

Step 1: Import Active Directory Module

The Import-Module ActiveDirectory command loads the tools needed to query AD.

Step 2: Get Computer Objects

The Get-ADComputer -Filter * command fetches all computer objects from AD.

Step 3: Display Results

Looping through each computer object, we print its Name and OperatingSystem properties.

🔄

Alternative Approaches

Using LDAP Filter
powershell
Get-ADComputer -LDAPFilter "(objectClass=computer)" -Properties Name,OperatingSystem | ForEach-Object { Write-Output "$($_.Name) - $($_.OperatingSystem)" }
LDAP filter syntax can be more flexible but is less readable than -Filter.
Using Filter by Name Pattern
powershell
Get-ADComputer -Filter "Name -like 'SRV*'" -Properties Name | ForEach-Object { Write-Output $_.Name }
Filters computers whose names start with 'SRV', useful for targeted queries.

Complexity: O(n) time, O(n) space

Time Complexity

The command queries all computer objects, so time grows linearly with the number of computers in AD.

Space Complexity

Results are stored in memory, so space grows linearly with the number of returned objects.

Which Approach is Fastest?

Using -Filter is generally faster and more readable than -LDAPFilter, but LDAP filters offer more complex querying.

ApproachTimeSpaceBest For
Get-ADComputer -FilterO(n)O(n)Simple and readable queries
Get-ADComputer -LDAPFilterO(n)O(n)Complex LDAP queries
Filtered by Name PatternO(m)O(m)Targeted queries with fewer results
💡
Always import the ActiveDirectory module before running AD commands with Import-Module ActiveDirectory.
⚠️
Forgetting to import the ActiveDirectory module causes Get-ADComputer to be unrecognized.