Set-ItemProperty ignores trailing .

141 Views Asked by At

Using Powershell 5.1, I am trying to create a registry value under:
HKLM:\SOFTWARE\Classes\.
(Note trailing period)

Unfortunately Set-ItemProperty appears to ignore the trailing . and creates the value under:
HKLM:\SOFTWARE\Classes
instead.

I'm running Powershell ISE as admin, and tested all sorts of quotes, Path & LiteralPath, back-tick escape and trailing / to no avail.

I've tried the following under a different key in case there is anything weird with Classes...

$PSVersionTable.PSVersion

New-Item -Path 'HKLM:\SOFTWARE\aTest\.' -ItemType RegistryKey -Force -ErrorAction SilentlyContinue > $null
New-Item -Path 'HKLM:\SOFTWARE\aTest\`.' -ItemType RegistryKey -Force -ErrorAction SilentlyContinue > $null

Set-ItemProperty -LiteralPath HKLM:\SOFTWARE\aTest\.   -Name 'Test01' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\aTest\.' -Name 'Test02' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath "HKLM:\SOFTWARE\aTest\." -Name 'Test03' -Value 'emptyFile' -Verbose;

Set-ItemProperty -Path HKLM:\SOFTWARE\aTest\.   -Name 'Test04' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path 'HKLM:\SOFTWARE\aTest\.' -Name 'Test05' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path "HKLM:\SOFTWARE\aTest\." -Name 'Test06' -Value 'emptyFile' -Verbose;

Set-ItemProperty -LiteralPath HKLM:\SOFTWARE\aTest\`.   -Name 'Test07' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\aTest\`.' -Name 'Test08' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath "HKLM:\SOFTWARE\aTest\`." -Name 'Test09' -Value 'emptyFile' -Verbose;

Set-ItemProperty -Path HKLM:\SOFTWARE\aTest\`.   -Name 'Test10' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path 'HKLM:\SOFTWARE\aTest\`.' -Name 'Test11' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path "HKLM:\SOFTWARE\aTest\`." -Name 'Test12' -Value 'emptyFile' -Verbose;

Set-ItemProperty -LiteralPath HKLM:\SOFTWARE\aTest\.\   -Name 'Test13' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\aTest\.\' -Name 'Test14' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath "HKLM:\SOFTWARE\aTest\.\" -Name 'Test15' -Value 'emptyFile' -Verbose;

Set-ItemProperty -Path HKLM:\SOFTWARE\aTest\.\   -Name 'Test16' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path 'HKLM:\SOFTWARE\aTest\.\' -Name 'Test17' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path "HKLM:\SOFTWARE\aTest\.\" -Name 'Test18' -Value 'emptyFile' -Verbose;

but the output shows it ignores the . unless literally escaped (when it creates a key with the back-tick in it...

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17134  228
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test01".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test02".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test03".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test04".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test05".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test06".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test07".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\`. Property: Test08".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test09".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test10".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\`. Property: Test11".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test12".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test13".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test14".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test15".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test16".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test17".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test18".

Please what am I missing?

1

There are 1 best solutions below

0
On

Think about the following examples:

PS D:\PShell> New-Item -Path 'HKLM:\SOFTWARE\Test\.' -ItemType RegistryKey -WhatIf
What if: Performing the operation "New Item" 
         on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\Test".

PS D:\PShell> New-Item -Path 'HKLM:\SOFTWARE\Test\..' -ItemType RegistryKey -WhatIf
What if: Performing the operation "New Item" 
         on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE".

PS D:\PShell> (Resolve-Path -LiteralPath 'HKLM:\Software\Test\.').Path
HKLM:\Software\Test

PS D:\PShell> (Resolve-Path -LiteralPath 'HKLM:\Software\Test\..').Path
HKLM:\Software

Apparently, Powershell's Registry provider takes an alone period (and doubled period as well) used as a key path component by analogy to suitable FileSystem's directory component as described in Naming Conventions:

  • Use a period as a directory component in a path to represent the current directory, for example ".\temp.txt". For more information, see Paths.
  • Use two consecutive periods (..) as a directory component in a path to represent the parent of the current directory, for example "..\temp.txt". For more information, see Paths.

However, reg.exe allows to create, read, set or delete registry keys and values including . and .. keys, see the following examples:

==> reg query "HKCU\Software\Test\."

HKEY_CURRENT_USER\Software\Test\.
    value_inside_PeriodKey    REG_SZ


==> reg add "HKCU\Software\Test\.."
The operation completed successfully.

==> reg query "HKCU\Software\Test"

HKEY_CURRENT_USER\Software\Test
    SomeTestString    REG_SZ    Test String

HKEY_CURRENT_USER\Software\Test\.
HKEY_CURRENT_USER\Software\Test\..

Please note use of HKCU instead of HKLM in the latter examples for obvious reasons (security).