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 VDCIcT service DeskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Qlemo"Batchelor", 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.