Skip to main content
Active Directory

Get-ADComputer: Find Computer Details in Active Directory with PowerShell

By December 27, 2019October 4th, 2020No Comments

You can use the PowerShell cmdlet Get-ADComputer to get various information about computer account objects (servers and workstations) from Active Directory domain. This is one of the most useful cmdlets for searching AD computers by various criteria (to get information about AD user accounts, another cmdlet is used – Get-ADUser).

Suppose your task is to find all inactive computers in Active Directory that have not been registered in a domain for more than 120 days and disable these accounts.

Before using Get-ADComputer cmdlet, you have to import Active Directory Module for Windows PowerShell with the command:

Import-Module activedirectory

Tip. In PowerShell 3.0 (introduced in Windows Server 2012) or later, this module is imported by default, if the following component is installed: Remote Server Administration Tools -> Role Administration Tools -> AD DS and AD LDS Tools -> Active Directory module for Windows PowerShell. To use the Get-ADComputer cmdlet in the desktop OSs (Windows 10, 8.1 or Windows 7), you must download and install the RSAT for your version of the OS and enable the AD-Powershell module from the Control Panel or using the command:

Enable-WindowsOptionalFeature -Online -FeatureName RSATClient-Roles-AD-Powershell

enabling ad module for powershell

Get-ADComputer – Cmdlet Syntax

You can get help on Get-ADComputer cmdlet parameters as usual with Get-Help command:

Get-Help Get-ADComputer

Get-ADComputer cmdlet syntax help

To get information from AD using the cmdlets from the AD for PowerShell module, you don’t need to have the domain admin privileges. It is sufficient that the account under which the cmdlet is being run is a member of the Domain Users / Authenticated Users group.

To get information about a specific computer account in the domain, specify its name as an argument of the -Identity parameter:

Get-ADComputer -Identity SRV-DB01

Get-ADComputer -Identity
DistinguishedName : CN=SRV-DB01,OU=Servers,OU=London,OU=UK,DC=contoso,DC=com
DNSHostName       : SRV-DB01.contoso.com
Enabled           : True
Name              : SRV-DB01
ObjectClass       : computer
ObjectGUID        : 87654321-1234-5678-0000-123412341234
SamAccountName    : SRV-DB01$
SID               : S-1-5-21-123456780-1234567890-0987654321-1234
UserPrincipalName :

The cmdlet Get-ADComputer returned only the basic properties of the Computer object from AD. We are interested in the time of the last computer registration in the AD domain, but this information is not displayed in the output of the command above. You can list all available properties of this computer object from Active Directory:

Get-ADComputer -Identity SRV-DB01 -Properties *

All AD computer properties

Using Get-Member, you can get a list of all the properties of the Computer class in AD:

Get-ADComputer -Filter * -Properties * | Get-Member

As you can see, the last logon date of this computer to the network is specified in the computer’s attribute LastLogonDate – 09/21/2015 0:20:17.

The Get-ADComputer cmdlet allows you to display any of the computer’s properties in the command results. Remove all unnecessary information leaving only values of Name and LastLogonDate attributes.

Get-ADComputer -identity SRV-DB01 -Properties * | FT Name, LastLogonDate -Autosize

Get-ADComputer LastLogonDate

So, we received data on the last time of registration in the domain for a single computer. Then you have to modify the command to make it display the information about the time of the last network registration for all computers in the domain. To do it, replace –Identity to –Filter *:

Get-ADComputer -Filter * -Properties * | FT Name, LastLogonDate -Autosize

LastLogonDate of AD computers - table view

We got a simple table that contains only 2 fields: computer name and LastLogonData date. You can add other fields of the Computer object from AD to this table.

To display the information about the computer objects in a particular OU (organizational unit), use the –SearchBase parameter:

Get-ADComputer -SearchBase ‘OU=Paris,DC=contoso,DC=loc’ -Filter * -Properties * | FT Name, LastLogonDate -Autosize

Sort the query results by the date of the last logon using the Sort cmdlet:

Get-ADComputer -Filter * -Properties * | Sort LastLogonDate | FT Name, LastLogonDate -Autosize

Sort by LastLogonDate

So, we have got the list of computers and the date they last logged on to the Active Directory domain. Now we want to disable the computer accounts that weren’t used for 120 days or more.

Using Get-Date we can get the value of the current date in the variable and reduce it to 120 days:

$date_with_offset= (Get-Date).AddDays(-120)

The resulting date variable can be used as a filter of Get-ADComputer query in LastLogonDate field:

Get-ADComputer -Properties LastLogonDate -Filter {LastLogonDate -lt $date_with_offset } | Sort LastLogonDate | FT Name, LastLogonDate -Autosize

So we have created a list of inactive computer accounts that have not been registered on the network for more than 120 days. Use the Disable-ADAccount or Set-ADComputer command to disable them.

Tip. For the first time, it’s better to test the results of the command with the –WhatIf switch, which allows to see what happens if the command has been run with no changes to the system.

Get-ADComputer -Properties LastLogonDate -Filter {LastLogonData -lt $date_with_offset } | Set-ADComputer -Enabled $false -whatif

Now you can disable all inactive computer accounts:

Get-ADComputer -Properties LastLogonDate -Filter {LastLogonData -lt $datecutoff} | Set-ADComputer -Enabled $false

Note. Also you can get a list of blocked, disabled and inactive computers and domain users using a separate cmdlet Search-ADAccount.

Get-ADComputer – Examples

Below are some more useful examples of using the Get-ADComputer cmdlet to query and search computer objects in the domain by specific criteria.

Get the total number of all active (unlocked) computers in Active Directory:

(Get-ADComputer -Filter {enabled -eq "true"}).count

Calculate the number of Windows Server instances in the AD domain:

(Get-ADComputer -Filter {enabled -eq "true" -and OperatingSystem -Like '*Windows Server*' }).count

Get-ADComputer count desktop or server objects in the ad

Get a list of computers in a specific OU whose names begin with LonPC:

Get-ADComputer -Filter {Name -like "LonPC*"} -SearchBase ‘OU=London,DC=contoso,DC=com’  -Properties IPv4Address | Format-table Name,DNSHostName,IPv4Address | ft -Wrap –Auto

When searching in the OU, you can use the additional parameter -SearchScope 1, which means that you need to search only in the OU root. The -SearchScope 2 option indicates a recursive search for computers in all nested OUs.

To find all workstation computers running Windows 10:

Get-ADComputer -Filter {OperatingSystem -like '*Windows 10*'}

Get the list of servers in the domain with the OS version, Service Pack installed and IP address:

Get-ADComputer -Filter 'operatingsystem -like "*Windows server*" -and enabled -eq "true"' -Properties  Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack,IPv4Address | Sort-Object -Property Operatingsystem | Select-Object -Property Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address| ft -Wrap –Auto

The output was such a beautiful table with a list of Windows Server in the AD:

get-ad-computers list with os version and ip address

The -LDAPFilter attribute allows you to use various LDAP queries as a parameter of the Get-ADComputer cmdlet, for example:

Get-ADComputer -LDAPFilter "(name=*db*)"|ft

Find all disabled computers in a specific Active Directory OU:

Get-ADComputer -filter * -SearchBase ‘OU=Computers,OU=London,DC=contoso,dc=com’ | Where-Object {$_.enabled -eq $False}

To delete all computer accounts that have not been logged into the domain for more than 6 months, you can use the command:
Get-ADComputer -properties lastLogonDate -filter * | where { $_.lastLogonDate -lt (get-date).addmonths(-6) } | Remove-ADComputer

The result of the Get-ADComputer command can be exported to a plain text file:

Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server 2008*' } -Properties OperatingSystem | Select DNSHostName, OperatingSystem | Format-Table -AutoSize C:\Script\server_system.txt

You can also get a list of computers and export it to a CSV file:

Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemServicePack | Export-CSV All-Windows.csv -NoTypeInformation -Encoding UTF8

Or get an HTML report file with a list of computers and necessary properties:

Get-ADComputer -Filter {OperatingSystem -Like '*Windows Server 2012*' } -Properties * | Select-Object Name,OperatingSystem | ConvertTo-Html | Out-File C:\ps\ad_computers_list.html

ad-computer objects html report

To perform a specific action with all the computers in the resulting list, you must use the Foreach loop. In this example, we want to create a list of servers in the domain and request specific information from each server (the result file should contain the server name, manufacturer and server model).

$Computers = Get-ADComputer -Filter {OperatingSystem -Like '*Windows Server*'}
Foreach ($Computer in $Computers)
{
$Hostname = $Computer.Name
$ComputerInfo = (Get-WmiObject -Computername $Hostname Win32_ComputerSystem)
$Manufacturer = $Computer.Manufacturer
$Model = $Computer.Model
Write-Host "Name: $Hostname"
Write-Host "Manufacturer: $Manufacturer"
Write-Host "Model: $Model"
Write-Host " "
$Content = "$Hostname;$Manufacturer;$Model"
Add-Content -Value $Content -Path "C:\PS\ServersInfo.txt"
}

You can use a shorter loop syntax. Suppose you need to run a specific command on all computers in a specific OU (in this example, I want to run a group policy update command on all servers):

get-adcomputer -SearchBase "OU=Servers,DC=contoso,DC=com" -Filter * | %{ Invoke-Command -Computer $_.Name -ScriptBlock {gpupdate /force} }

Using Get-AdComputer and the PowerShell startup script, you can control various computer settings. For example, I monitor the status of the SCCM agent (service) on users’ computers. A small logon script is executed on each computer during startup, which saves the ccmexec service status to a unused computer attribute – extensionAttribute10.

Then, using the following command, I can find computers on which the CCMExec service is missing or not running.

get-adcomputer -filter {extensionAttribute10 -ne "SCCM Agent:Running"} -SearchBase “OU=Compters,OU=London,DC=contoso,DC=com” -properties dNSHostName,extensionAttribute10,LastLogonDate  |select-object dNSHostName,extensionAttribute10,LastLogonDate

get service status on the ad computers

Leave a Reply