Skip navigation

Four Ways to Get Computer Names to the -computerName Parameter

I'm teaching a class forSAPIEN Technologies in Chicago this week. Actually, everyone's doing a lab on WMI, watching me enter this. Don't you wish you were here? Anyway, we needed to look at various ways to feed computer names to any cmdlet that has a -computerName parameter. Here's what I cam up with:

# get names from a file, one name per line
Get-WmiObject -class Win32_Whatever -computer (Get-Content names.txt)
 
# get names from Active Directory
Import-Module ActiveDirectory
Get-WmiObject -class Win32_Something -comp (
    Get-ADComputer -filter * | Select-Object -expand name
)
 
# specify one computer
Get-WmiObject -class Win32_This -computer SERVER-R2
 
# specify many computers
Get-WmiObject -class Win32_That -computer WESTDC4,EASTDC5
 
# get names from a CSV file that has a "host" column
Get-WmiObject -class Win32_Those -computer (
    Import-CSV computerlist.csv | Select-Object -expand host
)

I used Get-WmiObject for all of the examples, but you can obviously use this with any cmdlet that supports -computerName. Enjoy!
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish