 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update the windows host file entry using PowerShell?
Let say you want to update the host file particular entry, we have the below host file in our local computer.
Example
Get-Content $env:windir\system32\drivers\etc\hosts
Output
# For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 8.8.8.8 Google.com
We need to update the google.com entry to IP address 4.4.4.4.
Example
$hostfile = "$env:windir\system32\drivers\etc\hosts" $file = Get-Content $hostfile $newfile = $file -replace "8.8.8.8 Google.com","4.4.4.4 Google.com" Set-Content -Value $newfile -Path $hostfile -Force
Once you check the host file again, a new entry will be displayed.
To update the host file on the remote computer, just change the $hostfile variable location and the rest content will be the same.
$hostfile = "\RemoteServer\C$\Windows\system32\drivers\etc\hosts"
Advertisements
 