Gordon
Gordon

Reputation: 6863

Strange Get-Item behaviour

If I simply do

Get-Item "HKLM:\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE"

I get this on the console

NAME
----
@microsoft.com/GENUINE

However, this

$test = Get-Item "HKLM:\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE"
Write-Host "$($test.name)"

returns the full path, not just the name of the key.

Is this a bug? Intended behavior? Me doing something incorrectly?

Upvotes: 2

Views: 62

Answers (1)

Matt
Matt

Reputation: 46680

Is this a bug? Intended behavior? Me doing something incorrectly?

The middle one.

PowerShell formatting files (*.format.ps1xml located in the directory defined in $pshome by default) explain the difference here. In a default output scenario PowerShell checks for known formatting definitions, which are loaded from aforementioned files, for given object types. If present, it will use those, which govern what data and how that data is outputted.

So, in your case, you have Microsoft.Win32.RegistryKey objects. The format definitions for that are stored in registry.format.ps1xml. Just going to show a truncated section of that file so you can see how your top example is created.

 .... output truncated ....
 <TableControl>
                <TableHeaders>
                   <TableColumnHeader>
                        <Width>30</Width>
                        <Label>Name</Label>
                    </TableColumnHeader>
                    <TableColumnHeader>
                      <Label>Property</Label>
                    </TableColumnHeader>
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                      <Wrap/>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>PSChildName</PropertyName>
.... output truncated ....

So this defines an output table with 2 columns: name, property. The name column is actually the objects pschildname property. This is separate from the actual name property of the object. This is why you get the difference you see above.

There are other examples of this misconception in the PS world as well. The more you know.


The go-to resource on formatting files would be about_format.ps1xml. If you have issues with the default formatting and are not satisfied with simple Select-Object then you can create your own from copies.


FWIW you could have found both properties by just doing something like

Get-Item "HKLM:\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE" |  Format-List *

That would have forced all properties to show and you would have seen name and pschildname.

Upvotes: 4

Related Questions