Freigeben über


PowerShell – Read an XML configuration file

I was recently asked a question about PowerShell's ability to read in an XML configuration file at a Virtual Academy I ran last week.  One of the strengths of PowerShell is its ability to perform lots of time saving tasks … one of which is reading in an XML file.  The Get-Content command can read in an XML file and you can easily loop through the contents.

Example:

 [xml]$computerlist = Get-Content computers.xml
foreach( $computer in $computerlist.computers.target) 
{ 
    Write-Host $computer.name
} 

What would the XML file look like?

 <computers> 
  <target> 
    <Name>server1</Name> 
  </target> 
  <target> 
    <Name>server2</Name> 
  </target> 
</computers>

Nice and simple really.