If you maintain more than one Windows server sometimes you need a quick way to determine the host file differences on each server, the code below will help.
function Read-HostFile() { [CmdletBinding()] Param( [Parameter(Mandatory = $True)] [String] $Computer, [boolean] $ListComputerName = $true ) $Pattern = '^(?<IP>\d{1,3}(\.\d{1,3}){3})\s+(?<Host>.+)$' $File = "\\$Computer\c$\Windows\System32\Drivers\etc\hosts" $Entries = @() (Get-Content -Path $File) | ForEach-Object { If ($_ -match $Pattern) { if ($ListComputerName) { $Entries += "$Computer,$($Matches.IP),$($Matches.Host)" } else { $Entries += "$($Matches.IP),$($Matches.Host)" } } } $Entries } function Compare-HostFiles() { [CmdletBinding()] Param( [Parameter(Mandatory = $True)] [String] $Computer1, [Parameter(Mandatory = $True)] [String] $Computer2 ) Write-Output "Comparing host file from $($Computer1.ToLower()) to $($Computer2.ToLower())" Compare-Object ` -ReferenceObject $(Read-HostFile ` -Computer $Computer1 ` -ListComputerName $false) ` -DifferenceObject $(Read-HostFile ` -Computer $Computer2 ` -ListComputerName $false) ` -IncludeEqual | Format-Table ` -AutoSize ` @{Label = "Host Entry"; Expression = { $_.InputObject } }, @{Label = "Comparison"; Expression = { $_.SideIndicator } } } Clear-Host # Compare Servers Compare-HostFiles -Computer1 dev-webserver-p01 -Computer2 dev-webserver-p02 # Read individual files Read-HostFile 'dev-webserver-p01' Read-HostFile 'dev-webserver-p02'