Modify Windows HOSTS file using SCCM (System Center Configuration Manager) and Powershell

Introduction

I was just doing some work today where I needed to modify the content of the HOSTS file in Windows on a good bunch of devices. (This is the file being located in C:\Windows\System32\drivers\etc)

I figured this is something anybody might find useful, so I wanted to share the Powershell script I ended up creating for the purpose.

For your convenience, I’m also illustrating how this can be used in combination with ConfigMgr as this was a requirement for automation purposes 🙂

Powershell

The use of the script is pretty straightforward. It’s created to accept parameters, so you don’t have to modify the content of the actual script on a need to need basis.

Adding a new entry to the hosts file is done by running the script with the -AddHost parameter:

  • .\Edit-HostsFile.ps1 -AddHost -IP 192.168.1.1 -Hostname ftw.imab.dk

Removing an existing entry from the hosts file is done by running the script with the -RemoveHost parameter:

  • .\Edit-HostsFile.ps1 -RemoveHost -Hostname ftw.imab.dk

Configuration Manager

Using the script with ConfigMgr is easy as well.

Create a package with a program running following command line:

  • powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File .\Edit-HostsFile.ps1 -AddHost -IP 127.0.0.1 -Hostname ftw.imab.dk

Deploy it to the desired devices. In this example, this is done as an available deployment displaying in the Software Center.

You can also run the script directly in your task sequence as illustrated below.

End result

The end result is a delicious HOSTS file now including your new modifications.

Download

https://gallery.technet.microsoft.com/Modify-Windows-HOSTS-file-795b6907

9 thoughts on “Modify Windows HOSTS file using SCCM (System Center Configuration Manager) and Powershell”

  1. Hello,

    Really nice tool. One issue though:

    If there is no blank line at the end of HOSTS file then the first entry that you add with the command is appended to the last entry of the hosts file, making both entries (old and new) useless.

    Perhaps you could add a `n character in the script so that it leaves a blank line by design?
    The script does the trick fine for multiple lines because you make sure that a blank line is added after each entry you add.

    The issue starts when someone edits the HOSTS file manually and leaves no blank line at the end of the file.

    • Thanks! I can see that. Let me see what I can come up with 🙂 One quick workaround would be to remove the host that was manually entered and add it back using the script, seeing that my script always include a new line (Out-File does that automatically)

  2. For now, the easiest work around for me was this.

    Change line 45 from $fIP + “t" + $fHostname | Out-File -Encoding ASCII -Append $File to "n” + $fIP + “`t” + $fHostname | Out-File -Encoding ASCII -Append $File.

    So now i’m sure that a blank line is left before each entry. This leads of course to new entries being separated by a blank line but i don’t mind. Sometimes it makes it easier to read them. 🙂

    • Yeah, that was my first approach as well, but I don’t like the additional new line. I’ll see if I can come up with something else 🙂

  3. how do you deal with the elevated privilegs that are necessary to edit the host file.
    i’m stuck on this one.

    on the other hand , very decent script!

    • Hi, if you run the script with system privileges (whether or not a user is logged on), you should be able to modify the hosts file 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.