• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 73
  • Last Modified:

PowerShell script Get-ItemProperty issue on Windows 10 (NOT ON Windows 7) - Registry issue

I have this function which is running fine on Windows 7 BUT Does NOT on Windows 10.

The parameter used is  HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds\{4CC1FD56-6978-41A1-B787-484B2C6FEGF2}"

The error got on WINDOWS 10 ONLY
Get-ItemProperty : Cannot find path
'C:\THESIS\HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds\{4CC1FD56-6978-41A1-B787-484B2C6FEGF2}' because it does not exist.

so the issue is that the C:\THESIS IS ADDED automatically.... (not in Windows 7). any idea how to solve this?

Code is below :

function CheckForConnection
    param (
    [string] $connectionKey = "" 
    [bool] $found = $false
#    foreach ($connectionKey in get-item 'HKCU:\Software\Microsoft\Workspaces\Feeds\*' 2> $null)  
    foreach ($connectionKey in get-item 'HKCU:\Software\Microsoft\Workspaces\Feeds\*')  
        Write-Host "URL searched is $URL"
      Write-Host "Connection Key is  $connectionKey"

        if ( ($connectionKey | Get-ItemProperty -Name URL).URL -eq $URL)
            $found = $true
    return $found
François VDC
François VDC
1 Solution
Win10 does not care about c:\thesis. Are you doing something differently without knowing? Are you starting the script from a different directory as compared to win7?
François VDCIcT service DeskAuthor Commented:
nothing different. It's clearly a difference between Windows 7 and Windows 10.
The difference is that you never bothered to update the Powershell on your Windows 7 - it's still on v2.0 from 2009. The script will fail on v3.0 and later.
The issue is the line
[string] $connectionKey = ""

Open in new window

With the [string] at the beginning of the variable you're assigning, you're "strongly typing" it, that is, it you say "This is a string, and never allow it to be anything else!" (at least the 'never' is true for PS 3.0 and later).
Don't do that. Unless you really, really know what you're doing, and that does not seem to be the case. Let Powershell take care of the variable types. Don't try to fix what's not broken.
Because what's happening in PS 3.0 and later in the ForEach loop is that the registry items are immediately force-casted to strings when assigning $connectionKey (because you told it to do so).
And when a registry item is casted to a string, the good old path ("HKEY_CURRENT_USER\Software\...") is returned, not the one that Get-itemProperty can handle ("HKCU:\Software" or "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software"). Since "HKEY_CURRENT_USER\Software\..." is only a partial path, PS adds the current location ('C:\Thesis').
If you hadn't strongly typed $connectionKey, it would remain a registry item, would be piped to Get-ItemProperty, which would analyze it, and find and use the PSPath property.

To fix it, simply remove the above line completely.
And while you're at it, remove the [bool] in front of the next line as well.
$found = $false  will work just fine all by itself.
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry, oBdA, but you explanation about the difference of W7 and W10 is wrong (just tested this moment). PS 2 handles it exactly the same as PS 5.1 - a strict string cast.
The string cast caused by strong typing is wrong here, that much is true, as the explanation why.

François, you might have been lucky or  in a different location on W7 to get the script working. I cannot see any way allowing successful execution, though.
Paste that into a PS console (PS 3.0 or later, obviously).
Line 2 will result in the expected error.
Line 3 will return the registry item.
$sb = {Write-Host -Fore Black -Back Yellow "Powershell v$($PSVersionTable.PSVersion.ToString())"; Remove-Variable connectionKey -ErrorAction SilentlyContinue; [string] $connectionKey = ""; foreach ($connectionKey in get-item 'HKCU:\Software') {$connectionKey | Get-ItemProperty}}
Invoke-Command $sb
powershell.exe -Version 2 -Command $sb

Open in new window

I rest my case.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now