PowerShell Tip: Get-Clipboard

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.).

list of Servers
Excel list copied to clipboard.

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.

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.

Now we can run some commands against our list like Get-Uptime.


Leave a Reply

Your email address will not be published. Required fields are marked *