Monday, January 4, 2021

Getting a List of All of Your Windows Servers Using PowerShell

One of the most often asked for things in an IT department is a current list of servers. Key word here, CURRENT. Keeping an accurate inventory of the servers on your network can be a daunting task. And the larger your network, the more daunting the task can become.

Thankfully, PowerShell gives you a way to collect this information from Active Directory.

To run the scripts presented in this article, you will need to be running one of the following:

  • Windows 7 (or newer) with the Remote Server Administration Tools (RSAT) installed.
  • Windows Server 2008 R2 with RSAT installed.

Assuming you are logged on to your computer with a domain account, you can run the following PowerShell command to retrieve a list of servers in your domain:

Get-ADComputer -Filter {OperatingSystem -like "*windows*server*"}

This command produces the following output on my test network:


Assuming you only want to display the computer name and the operating system, you can pipe your output to the Select-Object cmdlet like so:
Get-ADComputer -Filter {OperatingSystem -like "*windows*server*"} -Properties OperatingSystem | 
    Select-Object -Properties SamAccountName,OperatingSystem

This command truncates the output as follows:


Lastly, if you want to save the output of this command so that you can work with it in a spreadsheet, you can export the list of servers to a CSV file with the Export-CSV cmdlet. Here's an example:
Get-ADComputer -Filter {OperatingSystem -like "*windows*server*"} -Properties OperatingSystem | 
    Select-Object -Properties SamAccountName,OperatingSystem |
    Export-Csv -Path C:\Temp\Test.csv -NoTypeInformation

If you found this blog helpful or have a question, please leave a comment.

Thanks for reading.

No comments:

Post a Comment