< Back

Set-CHostsEntry

Sun Jan 12, 2020 11:10 pm

NAME Set-CHostsEntry



SYNOPSIS

Sets a hosts entry in a hosts file.





SYNTAX

Set-CHostsEntry [-IPAddress] <IPAddress> [-HostName] <String> [[-Description] <String>] [[-Path] <String>]

[-WhatIf] [-Confirm] [<CommonParameters>]





DESCRIPTION

Sets the IP address for a given hostname. If the hostname doesn't exist in the hosts file, appends a new entry to

the end. If the hostname does exist, its IP address gets updated. If you supply a description, it is appended to

the line as a comment.



If any duplicate hosts entries are found, they are commented out; Windows uses the first duplicate entry.



This function scans the entire hosts file. If you have a large hosts file, and are updating multiple entries,

this function will be slow.



You can operate on a custom hosts file, too. Pass its path with the `Path` parameter.



Sometimes the system's hosts file is in use and locked when you try to update it. The `Set-CHostsEntry` function

tries 10 times to set a hosts entry before giving up and writing an error. It waits a random amount of time (from

0 to 1000 milliseconds) between each attempt.





PARAMETERS

-IPAddress <IPAddress>

The IP address for the hosts entry.



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-HostName <String>

The hostname for the hosts entry.



Required? true

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Description <String>

An optional description of the hosts entry.



Required? false

Position? 3

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Path <String>

The path to the hosts file where the entry should be set. Defaults to the local computer's hosts file.



Required? false

Position? 4

Default value (Get-CPathToHostsFile)

Accept pipeline input? false

Accept wildcard characters? false



-WhatIf [<SwitchParameter>]



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Confirm [<SwitchParameter>]



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Set-CHostsEntry -IPAddress 10.2.3.4 -HostName 'myserver' -Description "myserver's IP address"



If your hosts file contains the following:



127.0.0.1 localhost



After running this command, it will contain the following:



127.0.0.1 localhost

10.2.3.4 myserver