如何在使用 PowerShell 時向 Windows 主機檔案新增條目?


如需將內容新增到主機檔案,我們首先需要使用Get-Content 命令檢索內容,然後需要在新增條目後將內容設定為主機檔案。如下所示。我們需要向其新增全域性條目。

示例

$file = "C:\Windows\System32\drivers\etc\hosts"
$hostfile = Get-Content $file
$hostfile += "8.8.8.8   Google.com"
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

更新於:2021 年 3 月 18 日

9K+ 觀看

啟動您的 工作

透過完成該課程獲得認證

立即開始
廣告
© . All rights reserved.