-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExport-GCHostsFile.ps1
46 lines (41 loc) · 1.47 KB
/
Export-GCHostsFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#
.SYNOPSIS
Copies the hosts file to the path specified.
.DESCRIPTION
This is nothing more than an easy way of getting a copy
of the hosts file. Use it to create a backup of the current
hosts file before making changes.
Type 'Get-Help Export-GCHostsFile -Online' for extra information.
.PARAMETER Path
The destination path to export the existing hosts file.
If the destination is directory then the resulting file will
be named the 'hosts' file.
.PARAMETER Force
Prevents the confirmation dialog if the destination file
already exists.
#>
function Export-GCHostsFile {
[CmdletBinding(HelpUri = 'https://github.com/grantcarthew/ps-gcpowershell')]
[OutputType([Int])]
Param (
[Parameter(Mandatory=$true,
Position=0)]
[String]
$Path,
[Parameter(Mandatory=$false,
Position=1)]
[Switch]
$Force
)
Write-Verbose -Message "Function initiated: $($MyInvocation.MyCommand)"
$hostsFilePath = Join-Path -Path $env:SystemRoot -ChildPath '\System32\drivers\etc\hosts'
Write-Verbose -Message "Hosts file path set to: $hostsFilePath"
$pleaseConfirm = $false
if ((Test-Path -Path $Path) -and -not $Force) {
Write-Warning -Message "The destination file exists. Confirm overwrite?"
$pleaseConfirm = $true
}
Write-Verbose -Message "Copying hosts file to: $Path"
Copy-Item -Path $hostsFilePath -Destination $Path -Force -Confirm:$pleaseConfirm
Write-Verbose -Message "Function completed: $($MyInvocation.MyCommand)"
}