# How do I select a different PC when using HKEY\_USERS?

**URL:** <https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866>\
**Category:** PowerShell Help\
**Created:** [July 12, 2022, 6:54am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866 "2022-07-12T06:54:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 6:54am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/1 "2022-07-12T06:54:30Z")

</div>

I have this code snippet:

```auto
$string = (Get-ChildItem Registry::\HKEY_Users |                                                           
	Where-Object { $_.PSChildName -NotMatch ".DEFAULT|S-1-5-18|S-1-5-19|S-1-5-20|_Classes" } |
	Select-Object -ExpandProperty PSChildName |
	ForEach-Object { Get-ChildItem Registry::\HKEY_Users\$_\Printers\Connections -Recurse | Select-Object -ExpandProperty Name })

```

I was wondering if there’s a way to specify which network pc it has to check.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 12, 2022, 7:08am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/2 "2022-07-12T07:08:17Z")

</div>

How about using

> **[Invoke-Command (Microsoft.PowerShell.Core) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.2)**
>
> The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote...

?

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 7:14am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/3 "2022-07-12T07:14:07Z")

</div>

Whenever I try to use Invoke, it gives an error saying I can’t connect:

```auto
Connecting to remote server kdtict004 failed with the following error message : Unable to connect to the destination specified in the request. Check whether the service
ce is executed at the destination and whether applications are accepted. Read the logs and documentation for the WS-Management service running at the destination (usually is
this IIS or WinRM). If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: winrm quickconfig. For more i 
nformation, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo : OpenError: (kdtict004:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken

```

I can’t really individually go and configure every pc, as I have about 1000 to go through.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 12, 2022, 7:18am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/4 "2022-07-12T07:18:30Z")

</div>

> [@MatissePriem](#):
>
> I can’t really individually go and configure every pc, as I have about 1000 to go through.

I’d expect you somehow configured other settings of these 1000 PCs to fit your requirements for your environment. How did you do that? 😉

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 7:19am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/5 "2022-07-12T07:19:36Z")

</div>

I’m not in charge of configuring the PCs on my network and the one in charge isn’t here.

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [July 12, 2022, 7:21am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/6 "2022-07-12T07:21:58Z")

</div>

As @Olaf says if you’re managing that many machines there’s probably infrastructure to support that.

Sounds like WinRM is not enabled. I’ve added a link to a previous thread with a somewhat similar premise:  
[Discussion about enabling WinRM with GPO](https://forums.powershell.org/t/help-using-get-pnpdevice-with-cimsession/18926/16)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 12, 2022, 7:22am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/7 "2022-07-12T07:22:04Z")

</div>

OK. But since we’re both no magicians … how would you like to proceed? 🤷🏽‍♂️

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 7:23am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/8 "2022-07-12T07:23:28Z")

</div>

Isn’t there a way to use a ComputerName variable and pass it into the HKEY\_USERS script?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 12, 2022, 7:25am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/9 "2022-07-12T07:25:08Z")

</div>

For that you need to have access to the remote computers. Therefor you need to have a valid configuration.

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [July 12, 2022, 7:30am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/10 "2022-07-12T07:30:02Z")

</div>

The ComputerName is just an address. If you don’t have a key - in this case WinRM - you can go to the address, but you will not be able to do anything with what’s at the address.

It’s possible that some of the machines on your network have WinRM enabled. In the thread I linked above there’s a small script that pings machines and checks whether WinRM is enabled. You can perhaps use that as a starting point.

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 7:34am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/11 "2022-07-12T07:34:39Z")

</div>

I just enabled WinRM on two devices, and I still can’t connect to them.

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [July 12, 2022, 7:41am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/12 "2022-07-12T07:41:25Z")

</div>

A number of questions:

- Did you reboot the devices after enabling WinRM? (shouldn’t really be needed, but Windows tends to behave better after a reboot)
- What version of Windows (and Powershell) on the devices?
- Can you ping the devices?
- In the thread I linked above I mentioned that enabling WinRM should open firewall ports in the built-in Windows firewall, but that it can fail. Have you verified that the ports are allowed in the firewall?
- Is there a third party firewall installed on the devices?
- Is there a Firewall GPO that affects the devices?
- Are they on the same VLAN/Subnet as the calling machine?
- If not on the same Subnet or VLAN is there an internal firewall blocking communication?

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 7:46am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/13 "2022-07-12T07:46:53Z")

</div>

A number of answers:

- Yes, still didn’t work.
- Latest Windows 10 Version (20H2, OS: 19042, 1766)
- Pinging Works
- Firewall is good
- I’m not certain, there probably is.
- They’re on the same Subnet

I should be able to connect to other PCs on my network, since we have a remote control service that works perfectly fine.

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [July 12, 2022, 8:01am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/14 "2022-07-12T08:01:01Z")

</div>

When you say you cannot connect. Do you mean that you can’t run the Invoke-Command that you wished to run in the first place or do they not respond to the Test-WSMan command?

If you have a third party remote control service running on the devices, is it possible that it uses the same port(s) as WinRM?

> A WinRm listener can listen two different ways; HTTP or HTTPS. The WinRM port for HTTP is 5985 while the WinRm port for HTTPS is 5986, by default.
> 
> - HTTP – Port 5985
> - HTTPS – Port 5986

In that case you would need to tweak both the WinRM listener service and the firewall as well as call the remote commands specifying the non-standard ports. **It’s really not recommended and quite a bit of work!**  
[Default WinRm Ports and How to Change Them](https://adamtheautomator.com/winrm-port/)

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 8:02am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/15 "2022-07-12T08:02:02Z")

</div>

I think it will be better to just find another way to run the script, then.

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 11:52am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/16 "2022-07-12T11:52:53Z")

</div>

@Olaf, @laage, I managed to connect to another pc on the network, but when I run the script it gives the HKEY\_USERS from the PC that initiated the script and not the remote PC. What do I do?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 12, 2022, 11:57am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/17 "2022-07-12T11:57:35Z")

</div>

> [@MatissePriem](#):
>
> I managed to connect to another pc on the network, but when I run the script it gives the HKEY\_USERS from the PC that initiated the script and not the remote PC

Please show the code you used.

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 11:58am UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/18 "2022-07-12T11:58:30Z")

</div>

```auto
#Getting the printer-data
function Get-Printers {
    param (
    )
    $string = (Get-ChildItem Registry::\HKEY_Users |                                                           
	Where-Object { $_.PSChildName -NotMatch ".DEFAULT|S-1-5-18|S-1-5-19|S-1-5-20|_Classes" } |
	Select-Object -ExpandProperty PSChildName |
	ForEach-Object { Get-ChildItem Registry::\HKEY_Users\$_\Printers\Connections -Recurse | Select-Object -ExpandProperty Name })
    #Adding data to temporary file to easily edit
    Add-Content -Path $TempPath -Value $string
}

Invoke-Command -ComputerName $RemotePC -ScriptBlock(Get-Printers)

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 12, 2022, 12:01pm UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/19 "2022-07-12T12:01:43Z")

</div>

Try it this way …

```auto
Invoke-Command -ComputerName $RemotePC -ScriptBlock {
    Get-ChildItem Registry::\HKEY_Users |
    Where-Object { $_.PSChildName -NotMatch ".DEFAULT|S-1-5-18|S-1-5-19|S-1-5-20|_Classes" } |
    Select-Object -ExpandProperty PSChildName |
    ForEach-Object { Get-ChildItem Registry::\HKEY_Users\$_\Printers\Connections -Recurse | Select-Object -ExpandProperty Name }    
}

```

---

<div class="post-metadata">

**Author:** ![MatissePriem](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@MatissePriem](https://forums.powershell.org/u/MatissePriem)\
**Post date:** [July 12, 2022, 12:02pm UTC](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866/20 "2022-07-12T12:02:55Z")

</div>

Doing that doesn’t give me any HKEY\_USERS. I don’t know why.

[Next page](https://forums.powershell.org/t/how-do-i-select-a-different-pc-when-using-hkey-users/19866.md?page=2)
