Apologies in advance! Self taught (read: just started with no coding experience), so I may have some incorrect terminology.
I have a function that loads registry user hives and produces a global variable $global:userprofiles with userprofile SIDs. This part works fine and looks like this:
SID
---
S-1-5-21-3411627100-1852253463-1168398051-500
S-1-5-21-3711182449-2936729792-314897268-1001
DEFAULTUSER
After that function I need to manipulate a registry key. If I do the following ....
$key = "Registry::HKEY_USERS\$($userprofile.SID)\Something\someone"
foreach($userprofile in $global:userprofiles)
{echo $key}
I get the following output:
Registry::HKEY_USERS\DEFAULTUSER\Something\someone
Registry::HKEY_USERS\DEFAULTUSER\Something\someone
Registry::HKEY_USERS\DEFAULTUSER\Something\someone
Not great.
If I put $key inside the foreach like this:
foreach($userprofile in $global:userprofiles)
{
$key = "Registry::HKEY_USERS\$($userprofile.SID)\Something\someone"
echo $key
}
It works:
Registry::HKEY_USERS\S-1-5-21-3411627100-1852253463-1168398051-500\Something\for\someone
Registry::HKEY_USERS\S-1-5-21-3711182449-2936729792-314897268-1001\Something\for\someone
Registry::HKEY_USERS\DEFAULTUSER\Something\for\someone
It took me entirely too long to figure that out and I can't seem to understand where I went wrong. Can someone provide an ELI5 so I don't goof something similar in the future?