如何使用 PowerShell 在 Windows Host 文件中添加条目?

powershellmicrosoft technologiessoftware & coding

要将内容添加到 Host 文件,我们需要首先使用 Get-Content 命令检索内容,并在添加条目后将内容设置到 Host 文件。代码如下所示。我们需要在其中添加全局条目。

示例

$file=  ... Set-Content-Path$file-Value$hostfile-Force

检查主机文件后,"8.8.8.8           Google.com" 条目将被添加到主机文件中。

要在远程计算机上添加该条目,您只需将该文件位置指向远程服务器的主机文件,其余内容将保持不变。

示例

$file = \Comptuter1\C$\Windows\System32\drivers\etc\hosts
$hostfile = Get-Content $file
$hostfile += "8.8.8.8   Google.com"
Set-Content -Path $file -Value $hostfile -Force

相关文章