[英]How to remove Host entry through powershell command
我试图在主机文件中添加条目,如下所示,需要帮助从主机文件中删除相同的条目..
Add-Content -Path C:\\Windows\\System32\\drivers\\etc\\hosts -Value 20.0.0.1`tlocalhost -Force
首先制作原始文件的副本,这样您就可以随时恢复,但应该这样做:
$path = 'C:\Windows\System32\drivers\etc\hosts'
(Get-Content -Path $path) |
Where-Object { $_ -notmatch '^\s*20\.0\.0\.1\s+localhost'} |
Set-Content -Path $path -Force
正则表达式详细信息:
^ Assert position at the beginning of the string \s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 20 Match the characters “20” literally \. Match the character “.” literally 0 Match the character “0” literally \. Match the character “.” literally 0 Match the character “0” literally \. Match the character “.” literally 1 Match the character “1” literally \s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) + Between one and unlimited times, as many times as possible, giving back as needed (greedy) localhost Match the characters “localhost” literally
导入文件的内容,进行更改并再次导出...
$filecontent = Get-Content -Path C:\Windows\System32\drivers\etc\hosts
$changedfilecontent = $filecontent -replace "20.0.0.1`tlocalhost",""
$changedfilecontent | Out-File "C:\Windows\System32\drivers\etc\hosts"
(未测试)
我 100% 同意总是先复制关键文件,然后再对其进行处理。
$HostFile = Get-ChildItem -Path 'C:\Windows\System32\drivers\etc\hosts'
<#
Directory: C:\Windows\System32\drivers\etc
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 18-Mar-19 21:49 824 hosts
#>
Copy-Item -Path $HostFile.FullName -Destination "$($HostFile.DirectoryName)\$($HostFile.Name)_$(Get-Date -Format 'ddMMyyyy@HHmm')" -WhatIf
<#
What if: Performing the operation "Copy File" on target "Item: C:\Windows\System32\drivers\etc\hosts Destination: C:\Windows\System32\drivers\etc\hosts_2102202
0@1602".
#>
添加与删除更难,我不太同意。 因为它只是一个文本文件,一个重要的文件,但只是一个文本文件......
..vs 在那一行中挑选嵌入的东西。
# Find the line
Get-Content -Path 'C:\Windows\System32\drivers\etc\hosts' |
Select-String -Pattern '\d.*localhost.*'
<#
# 127.0.0.1 localhost
# ::1 localhost
#>
# Remove the line
(Get-Content -Path 'C:\Windows\System32\drivers\etc\hosts' |
Select-String -Pattern '\d.*localhost.*') -replace ''
# Add a line
Add-Content -Path 'C:\Windows\System32\drivers\etc\hosts' -Value ' 151.101.193.69 stackoverflow.com'
# Or just this
$HostFile = Get-ChildItem -Path 'C:\Windows\System32\drivers\etc\hosts'
Copy-Item -Path $HostFile.FullName -Destination "$($HostFile.DirectoryName)\$($HostFile.Name)_$(Get-Date -Format 'ddMMyyyy@HHmm')"
(Get-Content -Path 'C:\Windows\System32\drivers\etc\hosts' |
Select-String -Pattern '\d.*localhost.*') -replace '', ' 151.101.193.69 stackoverflow.com'
哦,顺便说一句,这已经存在一段时间了,通过 MS powershellgallery.com,所以没有必要从头开始,除非它是一种学习努力。
Find-Module -Name '*hostfile*' | Format-Table -AutoSize
<#
Version Name Repository Description
------- ---- ---------- -----------
1.0.2 Web.Helper.HostFile PSGallery Helper to manage the Windows host file
1.0.3 Add-HostFileEntry PSGallery Adds and removes entries from the HOSTS file
1.0 HostFile PSGallery Helper commands to interact with host file
#>
在本地和远程修改添加、删除 - Windows 主机文件 - PowerShell
您可以使用此 PowerShell 脚本在本地和远程修改添加、删除 - Windows 主机文件。 这个脚本有四部分#第一部分是关于添加主机记录,在本地计算机中或者我们可以通过gpo中的登录脚本部署。#第二部分是修改,替换或删除任何主机条目下载: modifyhost.ps1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.