3

I'd like to add and remove hosts file entries at a regular interval. I believe the easiest way to do this is powershell and task scheduler.

So how do you add/remove hosts file entries?

2 Answers 2

5

I wrote a module a while back that provides a bunch of Cmdlets for managing the hosts file. It also supports tab completion of the hosts, which is handy.

The source is on GitHub and it can be installed using PsGet:

install-module PsHosts

You can then use it using the verbs:

# Add a new entry
Add-HostEntry mysite.local 127.0.0.1

# Hit enter for all matching, hit tab to complete
Get-HostEntry mysite*

# Change existing
Set-HostEntry mysite.local 127.0.0.1

# Remove entry
Remove-HostEntry something.local

# Comments out an entry
Disable-HostEntry something.local

And because its all pipelined, it's pretty flexible:

# Remove all disabled/commented *.local entries
Get-HostEntry *.local | ?{-not $_.Enabled} | Remove-HostEntry

If you spend more time in the IIS GUI than Powershell, I also made an IIS extension a few years back (they share code and unit tests) that has some additional site-specific features. It's available on Codeplex.

1
  • Nice module, how would one Toggle (Enabled/Disabled) an entry ? Jul 31, 2015 at 13:31
4

I wrote up a few powershell scripts that take care of this. Just plop them into task scheduler at whatever interval you'd like.

Don't forget to change powershell execution policies to make sure the script runs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .