Often times I find myself working with lists of computers (servers) where I need to get some information from the servers (i.e. uptime, OS version, etc.), or I need to run a command against them. I typically gather the computer names into a list in Excel for data manipulation, then I save the list of computer names into a text file to use from PowerShell. Now with PowerShell 5 I have the ability to work with data from the clipboard. I can select the list of computer names, copy it to the clipboard, and from PowerShell convert the text list in my clipboard to an array of objects that I can feed into other commands (i.e. Get-ADComputer, Get-CimInstance, etc.).
We selected the list of server names from Excel and copied them to the clipboard (Ctrl +C). Now we need to get the contents of the clipboard into PowerShell.
PS C:\> $ServerList = (Get-Clipboard) -match '\S'
Notice the -match operator? When copying a list from Excel to the clipboard, Excel adds an additional carriage return. The regular expression ‘\S’ means match any non-white space characters. By using the -match operator with this regular expression we are suppressing the extra carriage return (and any other empty lines).
Now let’s look at the results.
PS C:\> $ServerList
PS C:\> $ServerList.Count
Now we can run some commands against our list like Get-Uptime.
PS C:\> Get-Uptime -ComputerName $ServerList