Remove entry from trusted hosts list

The last variant I want to show is removing a single entry from the list
function remove-trustedhost {
[CmdletBinding()]
param (
[string]$trustedhost,
[string]$computername = $env:COMPUTERNAME
)
if (Test-Connection -ComputerName $computername -Quiet -Count 1) {
$th = Get-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername |
select -ExpandProperty TrustedHosts
if ($th) {
$ths = $th -split “, |,”, 0, “Regex”
$newth = ($ths -ne $trustedhost) -join “, ”
Set-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername -ValueSet @{TrustedHosts = $newth}
}
else {
Write-Warning -Message “No trusted hosts to remove”
}
}
else {
Write-Warning -Message “$computername is unreachable”
}
}
After testing that you can connect to the remote machine use Get-WsManinstance to pull the current trusted hosts list. Split the list into an array. I’ve used a regular expression fo the delimiter so I catch the cases with and without a space after the comma – I tend to leave a space but other people don’t.
Recreate the string without the entry you don’t want.
Its probable you could use the replace operator for this instead but I wanted to show how to remove an entry from an array.
Use set-WSManInstance to reset the trusted hosts list.
Now you have 4 functions for managing your trusted hosts list – enjoy
 Comment on this Post