Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Avoiding comments while reading file in powershell

$
0
0

Hi Experts,

I am reading a host file file using getcontes cmdlet.

$lines = Get-Content myfile.txt | Where {$_ -notmatch '^\s+$'} 
foreach ($line in $lines) {
    $fields = $line -split '\s+'
    $ip = $fields[0]
    for ($i = 1; $i -lt $fields.Length; $i++) {
        $ESXHost = $fields[$i]

But in actual host file comments will be there starting with "#"

How to avoid those blank lines and comments while reading the file? Any regular expression to just read the line starting with IP address "127.0.0.1"?


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>