Wednesday, October 22, 2014

How to Add HOST file Entry using PowerShell

Here is the nifty PowerShell script to add HOST file entry using PowerShell:

$HostFile = 'C:\Windows\System32\drivers\etc\hosts'

# Create a backup copy of the Hosts file
$dateFormat = (Get-Date).ToString('dd-MM-yyyy hh-mm-ss')
$FileCopy = $HostFile + '.' + $dateFormat  + '.copy'
Copy-Item $HostFile -Destination $FileCopy

#Hosts to Add
$Hosts = @("", "Intranet", "")

# Get the contents of the Hosts file
$File = Get-Content $HostFile

# write the Entries to hosts file, if it doesn't exist.
foreach ($HostFileEntry in $Hosts) 
    Write-Host "Checking existing HOST file entries for $HostFileEntry..."
    #Set a Flag
    $EntryExists = $false
    if ($File -contains " `t $HostFileEntry") 
        Write-Host "Host File Entry for $HostFileEntry is already exists."
        $EntryExists = $true
    #Add Entry to Host File
    if (!$EntryExists) 
        Write-host "Adding Host File Entry for $HostFileEntry"
        Add-content -path $HostFile -value " `t $HostFileEntry"

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...