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.

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

Share this:

  • Print (Opens in new window) Print

Like this:

Like Loading…

Related

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

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