Friday, June 3, 2011

Home /Microsoft /PowerShell /PowerShell Profile /Windows /Windows 2008 R2 / Modify HOSTS File Remotely–PowerShell Script

Modify HOSTS File Remotely–PowerShell Script

You can use this PowerShell script to update a HOSTS file remotely.  This procedure can be used to modify any files remotely.

Input File: Servers.csv – This file contains server name or IP address.

image

Script

image

Output – You will see the status update on the screen as displayed in the following screenshot:

image

Download – This script can be downloaded from the following locations:

www.sivarajan.com

TechNet Script Gallery

SS

Santhosh Sivarajan

Microsoft MVP · Identity & Cybersecurity Architect

Santhosh has 30+ years of hands-on enterprise experience in Identity and Access Management, Microsoft Entra ID, Active Directory, Microsoft 365 and Zero Trust architecture. He is the author of two books on Windows Server and security, and leads consulting, assessments and training at SAG Business Group.

7 comments:

old hint, you should get through the Admin$ share when accessing a remote %SystemRoot% directory. This was especially true in the old good Win NT4 VS Windows 2000 VS Windows 2003 times when the directory gradually changes from \WINNT to \WINDOWS for fresh installs...

This logic is valid regardless of the Operation System version. You don’t need to know whether it is WINNT or Windows if you are using ADMIN$ or Administrative Shares. By default, all Administrative Shares (includes C$) is still valid in windows 2008 and Widows 2008R2.

This logic never going to “expire” :)

Hi Santhosh,

I'm having an issue using this script. The test path is invalid. I can reach the servers im providing in the server.csv file by unc path fine (\\severname\c$) but am unable to access this path through the script. Any thoughts?

Thanks!
Mike

My issue was the header (servername) not being in the csv file. Script work great. Thanks!

Hi Santhosh,

I have a question about the powershell script. Can it be modified to remove a line from a hosts file on a remote computer?

Any help would be appreciated.

Thanks,

Michel

Hi,
This code will not work on Windows 2000 Server as \c$\Windows\System32\drivers\etc\hosts is not valid path host file reside under C:\WINNT\system32\drivers\etc

Any chance you could include a check to see if the entry is already present? Thanks.

Post a Comment

Popular Posts
Workstation Trust Relationship Issue Issue: You receive the following error message, when you try to login to the domain.  The security database on the server does not have ... ADMT Service Account - Permission and Configuration The ADMT service account needs to have proper permission in source and target domains.  You don’t need to use 2 separate accounts.  You can ... My First Peek into Microsoft Exchange 2010 By Santhosh Sivarajan Before I really dive into Exchange 2010, I thought I would install and play with it first. I took some screen shots and notes during the ins... ObjectSID and Active Directory What is an objectSID in Active Directory? When a new object is created in Active Directory, Domain Controller assigns a unique value used ... AD Group Report - List Group Members in Active Directory–PowerShell Script Updated Script - http://portal.sivarajan.com/2011/10/search-ad-collect-local-admin-group.html Script #1 This script... Add Users to a Group–PowerShell Script Purpose – Add users to a group from an input file – PowerShell V2 Script.  Input file – Input file (Users.csv) contains samAccountName in... User Account Migration and Merging – Part I (ADMT) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM pre-creating user account ... User Account Migration and Merging – Part II (Quest Migration Manager) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM Pre-creating user account in... Delete Stale or Inactive Computer Accounts from Active Directory Here is an easy way to identify and delete inactive or stale computers in an Active Directory environment.  Using the dsquery command you c... Converting PowerShell (PS1) to EXE / Standalone Application As we know, there many applications available to convert a PowerShell file to a standalone executable file.  Based on my experience, PowerSh...