Powershell to view all host files

By steve, 16 December, 2015

To view all host file entries for all machines (assuming the machine you are on has the AD powershell modules, and can reach all machines):

foreach ($comp in Get-ADComputer -Filter * -Properties Description | Where-Object { $_.Description.Length -eq 0 -or $_.Description -ne "Failover cluster virtual network name account" }) 
{ 
	$hostfile= "\\"+ $comp.name +"\c$\windows\system32\drivers\etc\hosts"; 
	$comp.name; 
	$data=Get-Content $hostfile;
	foreach($line in $data) {
		if($line.Length -eq 0 -or $line.StartsWith("#")) {
			continue;
		}
		$line
	}
}

Comments