如何使用 PowerShell 讀寫 Windows 主機檔案?


對映主機名和 IP 地址的 Windows 主機檔案是一個純文字檔案。因此,讀取主機檔案很簡單。我們只需要使用 Get-Content 命令來檢索檔案內容。

通常,主機檔案儲存在目錄c:\windows\System32\drivers\etc\。 如果作業系統已安裝在該位置下。

或者,可以使用$env:Windir 獲取 Windows 目錄。

若要獲取檔案內容,

示例

Get-Content $env:windir\system32\drivers\etc\hosts

輸出

# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost

若要從遠端伺服器獲取主機檔案,

Get-Content \Computer1\c$\windows\system32\drivers\etc\hosts

更新於:2021 年 3 月 18 日

2K+ 瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.