To query Active Directory for Windows workstations using PowerShell, you typically use the Get-ADComputer cmdlet, which is a part of the ActiveDirectory module.
If you haven’t done so, you need to install the Remote Server Administration Tools (RSAT) to get the ActiveDirectory module. The way to get RSAT depends on your Windows version. For Windows 10, for example, you can add it through the optional features. For Windows Server, you can install it through the Add Roles and Features Wizard.
First, you’ll need to import the ActiveDirectory module.
Import-Module ActiveDirectory
Query Active Directory for Windows Workstations: If you simply want to retrieve all computers, you can do:
Get-ADComputer -Filter *
If you specifically want to target workstations (and exclude servers), it’s a bit trickier since AD doesn’t make a direct distinction between a workstation and a server. However, you can make an educated guess based on the operating system.
Get-ADComputer -Filter {(OperatingSystem -like "*Windows 10*") -or (OperatingSystem -like "*Windows 8*") -or (OperatingSystem -like "*Windows 7*")}
Filtering and Formatting: If you want only the names of the workstations, you can use the Select-Object cmdlet.
Get-ADComputer -Filter {(OperatingSystem -like "*Windows 10*") -or (OperatingSystem -like "*Windows 8*") -or (OperatingSystem -like "*Windows 7*")} | Select-Object Name
To export the results to a CSV file, make use of the Export-CSV cmdlet.
Get-ADComputer -Filter {(OperatingSystem -like "*Windows 10*") -or (OperatingSystem -like "*Windows 8*") -or (OperatingSystem -like "*Windows 7*")} | Select-Object Name | Export-Csv -Path "C:\path\to\file.csv" -NoTypeInformation
Make sure you run these commands in a PowerShell session with appropriate permissions to query Active Directory.
Keep in mind that you might want to adjust the operating system names in the filter depending on your environment and what versions of Windows workstations are in use. The examples above filter for Windows 7, 8, and 10, but you might have other versions or want to include/exclude differently.