Wednesday, March 11, 2015

Using Powershell to update the hosts file on remote computers

Today I had the need to edit the hostfile on all the computers in a particular Active Directory OU.

I had previously set entries in the hosts file in a small remote office to test some system changes before rolling out the changes to everyone via DNS.

I edited my own hosts file and then ran this Powershell script to copy it to all computers in the target OU.

Import-Module ActiveDirectory
$Comps = Get-ADComputer -SearchBase 'OU=Comp,OU=OUname,DC=domainname,DC=com' -Filter '*' | Select -ExpandProperty Name
$source = "C:\Windows\System32\drivers\etc\hosts"
$path = "Windows\System32\drivers\etc\hosts"
$hamptonComps | foreach {Copy-Item $source -Destination \\$_\c$\$path}