Modify profileimagepath without known SID?

Hey again everyone. I know I am close but for some reason I cant seem to figure out the last step. So I have a local user account that I don’t know the SID for (part of multiple machine test), that we need to update the profileimagepath on. I have it working to find the appropraite profileimagepath in the registry but I just cant seem to get it to update the path with the new value. it tells me that my $user.pspath is NULL…can anyone help point out my stupidity please?

$valuetochange = 'old_profileimagepathlocation'
$newvalue = 'new_profileimagepathlocation'
$user = Get-childItem ‘HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList’ | % {Get-ItemProperty $_.pspath } | ? {$_.profileimagepath -match $valuetochange} 
Get-ItemProperty -Path $user.pspath profileimagepath | %{set-itemproperty -Path $_.PSPath profileimagepath -Value ( $_.profileimagepath -Replace $valuetochange, $newvalue) -whatif}

Hi,

That looks OK. Have you tried outputting the values with Write-Host to see if your variables have the values that you expect.

Is that all of your code?
You mentioned multiple machines, are you running the script on remote computers?

What is the full error message that you get?

This is the full error. It says $user.pspath is NULL but I must be missing something simple because I can see that its not.

The above code is the full code so far. I WILL be running this against multiple remote computers but right now I am just running against one to test.

You say that you can see it’s not null, but I don’t believe you because you haven’t shared the output :slight_smile:

Your code is working for me when $valuetochange is set to a known good value.

If you’re sure it’s not null, what output do you get if you change the last line to Write-Host $user.pspath ?

Assuming there’s no output, does it work when you change the first line to:

$valuetochange = $Env:username

When I say its not null, I am speaking about looking at the code. I can clearly see where I have identified both the $user variable and the .pspath in that code so I am not understanding why its returning a NULL value for $user.pspath. So the code doesn’t work for me otherwise I would get some kind of change to that registry value and not a cannot bind argument.

If i set the value to says C:\Users\Administrator for example and try to change it to say C:\Users\NewAdmin…it makes no difference as nothing changes for that registry entry because of the NULL error. No matter what i choose to Write-Host, it jumps it and immediately gives the null error you see in my reply.

PS C:\Windows\system32> $valuetochange = 'C:\\Users\Administrator'
$newvalue = 'C:\\Users\NewAdmin'
$user = Get-childItem ‘HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList’ | % {Get-ItemProperty $_.pspath } | ? {$_.profileimagepath -match $valuetochange} 
Get-ItemProperty -Path $user.pspath profileimagepath | %{set-itemproperty -Path $_.PSPath profileimagepath -Value ( $_.profileimagepath -Replace $valuetochange, $newvalue) -whatif}
Write-Host $user.pspath
Get-ItemProperty : Cannot bind argument to parameter 'Path' because it is null.
At line:4 char:24
+ Get-ItemProperty -Path $user.pspath profileimagepath | %{set-itemprop ...
+                        ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ItemProperty], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemPropertyCommand

Well it is null because your third line is not finding the profile. Just declaring a variable as input to a parameter doesn’t make it ‘not null’.

When I test your code with a double backslash, it doesn’t like that at all. What result do you get when you just use ‘Administrator’? If that doesn’t work, does Administrator actually have a folder under C:\Users?

You need to verify you are actually getting output from these two lines

$valuetochange = 'C:\\Users\Administrator'
Get-childItem 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' | % {Get-ItemProperty $_.pspath } | ? {$_.profileimagepath -match $valuetochange} 

Which you will see you are not getting any output. The reason being you are using -Match which uses regex. \ in regex needs to be escaped which it looks like you escaped the first one, but not the second. So if you run this, you should see output (assuming there is a profileimagepath that matches)

$valuetochange = 'C:\\Users\\Administrator'
Get-childItem 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' | % {Get-ItemProperty $_.pspath } | ? {$_.profileimagepath -match $valuetochange} 

Lastly, you can simplify your code greatly

$valuetochange = 'C:\\Users\\Administrator'
$newvalue = 'C:\Users\NewAdmin'
$user = Get-childItem 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' | 
    Get-ItemProperty | Where-Object profileimagepath -match $valuetochange
$user | Set-ItemProperty -Name profileimagepath -Value ($user.profileimagepath -Replace $valuetochange, $newvalue) -whatif
2 Likes

If I output just the first line…it finds the SID no problem so its finding the SID just fine.

The double backslash is required to escape out the variable; it wont work without it as that is what is in the profileimagepath registry key (its the folder location for the user)

PS C:\Windows\system32> $valuetochange = 'C:\Users\Administrator'
$newvalue = 'C:\Users\NewAdmin'
$user = Get-childItem ‘HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList’ | % {Get-ItemProperty $_.pspath } | ? {$_.profileimagepath -match $valuetochange} 
Get-ItemProperty -Path $user.pspath profileimagepath | %{set-itemproperty -Path $_.PSPath profileimagepath -Value ( $_.profileimagepath -Replace $valuetochange, $newvalue) -whatif}
Write-Host $user.pspath
parsing "C:\Users\Administrator" - Unrecognized escape sequence \U.
At line:3 char:127
+ ... mProperty $_.pspath } | ? {$_.profileimagepath -match $valuetochange}
+                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

I cant use Administrator as I can only do this by locating the appropraite SID that has the aforementioned profileimagepath.

Yes the profile and folder location exists on the demo system.

I had escaped out code before that didn’t need it in the entire path so I assumed the same would work here as well but apparently not. Now the script executes without error so thank you for that aspect.

However its still not replacing the data in the profileimagepath after the script runs without error; any ideas?

It worked in my testing so I’d guess maybe you’re not running it as admin (needs elevation to write there)

Definitely running powershell as admin:

The reg path doesnt update

Do you even read the output of the commands you run?

what if: Performing the operation...
2 Likes

Ahh gotcha…the whatif was messing it up for me. Good call good sir!

1 Like