Generating Lists of Computer Names with PowerShell

PowerShell is particularly good for automating tasks that need to be performed on multiple computers, and many cmdlets are designed to allow multiple computer names to be specified. In many cases, the most difficult task is getting together a list of computers to use with some of the cmdlets and scripts available. There are several ways to generate these lists of names, and very often the situation will dictate which method should be used.

The simplest way is to manually type names of computers to generate an array of text strings in a variable. That variable can then be passed to cmdlets that accept an array of strings for the input of a ComputerName variable.

If this same list of names might be useful in the future, one way to save the list permanently is to save it to a text file.

When the list is needed again, the contents of the file can be read and stored to a variable.

When there’s more than a few computers to deal with, it’s much easier to get those names from the computer accounts in Active Directory. In a correctly configured domain environment, the Microsoft Active Directory cmdlets can be used to generate lists of computers.

Not everyone has the infrastructure in place to use the Microsoft cmdlets. Fortunately, Quest has freely available cmdlets for interacting with Active Directory.

Sometimes it’s difficult to filter a list of names using these cmdlets. Another option is to export all the computer account properties to a Comma Separated Values file, and then use Excel to filter the list.

When the spreadsheet has been narrowed down to the required names, it can then be imported back into PowerShell to get the array of computer names.

The capability of PowerShell to import Csv files is also useful when a list of computers is provided in an Excel spreadsheet from another IT department. It’s common for reporting software to generate spreadsheets of computer names along with all kinds of other data. By saving these Excel spreadsheets as Csv files, they can easily be imported directly into PowerShell.

Excel Computer List

Excel Save as Csv

Sometimes spreadsheets generated by reporting software will include spaces around the cell data. If computer names have spaces around them, cmdlets like Invoke-Command will not accept those names as input. One way to clean up the computer names is to use string methods like Trim.

In certain situations, IP addresses can be used instead of computer names. This is helpful when actions need to be run on all computers on a subnet, regardless of their names.

Just like anything in PowerShell, there are lots of different ways to accomplish a task. These are just a few ways to generate a list of computers, which can then be used with any number of scripts and functions.