Get host file entries

Hello,

This is a quick script to help you in your daily work, in fact a script to list entries of you hosts file 🙂

Function Get-HostFileEntry {
    $HostOutput = @()
    $HostFile = $env:windir + "\System32\drivers\etc\hosts"
    [regex]$r="\S"
    Get-Content $HostFile | ? {
        (($r.Match($_)).value -ne "#") -and ($_ -notmatch "^\s+$") -and ($_.Length -gt 0)
    } | % {
        $_ -match "(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?<HOSTNAME>\S+)" | Out-Null
        $HostOutput += New-Object -TypeName PSCustomObject -Property @{'IP'=$matches.ip;'Hostname'=$matches.hostname}
    }
    $HostOutput
}

Regards,