Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Powershell Parsing

Posted on January 16, 2007August 5, 2009

In PowerShell, Get-WMIObject is a terrific cmdlet for remotely managing systems. If you have a text list of server or computer names, here's a quick method you could enumerate that list and do something to each server.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

foreach ($server in (Get-Content s:\servers.txt)) {
#skip blank lines
if (($server).length -gt 0)
{ $server
Get-WmiObject win32_operatingsystem -computer $server.Trim()}
}

S:\Servers.text is just a text file like this:

Server1
Server2
DC1
Server3

The ForEach loop reads the contents of the file, assigning the name to $server. I've added some code to catch blank lines that sometimes end up at the end of files like this. If the length of $server is greater than 0 than process the server. The code sample will echo the name to the console and then run the Get-WMIObject cmdlet passing $server as the computername. I found it helpful to call the Trim method as an extra space can confuse PowerShell.

I'm not saying this is the only way to do something like this but it works for me.

PowerShell Scripting cmdlet


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d