0
function GetDefaultPrinter {
param (
        [string]$Comp
    )
    Invoke-Command -ComputerName $Comp -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-ItemPropertyValue 'Registry::\HKEY_Users\' + $_ + '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows'  'Device'}
    }
}

so the path is: 'Registry::\HKEY_Users' + $_ + '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows'

if I use: 'Registry::\HKEY_Users\S-1-5-21-166812146-2062891607-924725345-16845\SOFTWARE\Microsoft\Windows'

it works!

Thank you so much!

user1245735
  • 43
  • 1
  • 9
  • 1
    In short: In order to pass the result of an _expression_ (involving `+` operations, in this case) as an argument to a command, you need to enclose the expression in `(...)`; see [this answer](https://stackoverflow.com/a/41254359/45375) to the linked duplicate for a detailed discussion of how PowerShell parsed unquoted command arguments. – mklement0 Feb 18 '21 at 23:37

1 Answers1

4

You need to enclose string concatenation in parentheses, to force PowerShell out of argument-parsing mode 1:

Get-ItemPropertyValue ('Registry::\HKEY_Users\' + $_ + '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows')  'Device'

Alternatively and more practical, use double-quotes for string interpolation:

Get-ItemPropertyValue "Registry::\HKEY_Users\$_\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows"  'Device'

[1] Here is a simplified demo of what is happening in your original code:

Function Foo( $a, $b, $c ) { "a: $a"; "b: $b"; "c: $c" }

Foo 1 + 2

Output:

a: 1
b: +
c: 2
mklement0
  • 382,024
  • 64
  • 607
  • 775
zett42
  • 25,437
  • 3
  • 35
  • 72