header image

Add a trusted host

Posted by: | December 29, 2019 Comments Off on Add a trusted host |

Last time I showed how to read the trusted host list – this is how you add a trusted host

function add-trustedhost {
param (
[string]$computername = $env:COMPUTERNAME

if (Test-Connection -ComputerName $computername -Quiet -Count 1) {
$th = Get-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername |
Select-Object -ExpandProperty TrustedHosts

if ($th) {
$newth = $th + “, $trustedhost”
else {
$newth = $trustedhost

Set-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername -ValueSet @{TrustedHosts = $newth}
else {
Write-Warning -Message “$computername is unreachable”



Get the current trusted host list and append the new trusted host name. Use Set-WSManInstance to write back the new trusted host list.


The function currently only takes a single new trusted host but you could modify the code to accept an array of computer names and iterate through the array to create the new trusted host list.


You need to be running PowerShell with elevated privileges (as administrator) to be able to write the trusted hosts list back to the system.


This function and last times get-trustedhost both have a computername parameter (defaults to local computer) so you can use these functions (and the upcoming functions to clear and remove trusted hosts) on the local or remote machines

under: PowerShell

Comments are closed.