Reading the hostsfile (Get-Content)

Here is a small script which can read a hostsfile of a remote machine (appropriate Firewall Settings and local admin rights required)

This can all be aquired with only one line of code:

Get-Content “\\$computername\c$\windows\system32\drivers\etc\Hosts”

$computername has to be changed to the machine Name of the remote Computer, eg. ws01

Adding new entries (Add-Content)

To add new entries you can use the Add-Content commandlet:

Add-Content “C:\Windows\System32\drivers\etc\hosts” “`r`n127.0.0.1`t`t`t`”

This method can be useful for Software Distribution if host entries are required. You can’t use Add-Content commandlet for a remote machine. But there is a solution. Just start a remote Powershell Session (PS Remoting has to be enabled)

Start-Process powershell.exe -ArgumentList “-noexit -command Enter-PSSession -ComputerName $ComputerName”

in this session puttygen download , you can use the Add-Content Commandlet.