Can't Set New-ItemProperty to Registry Path Containing Astrix

Welcome Forums General PowerShell Q&A Can't Set New-ItemProperty to Registry Path Containing Astrix

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #33924

    Participant
    Points: 0
    Rank: Member

    While converting a few .reg files to .ps1, I've run into a problem with this script:

    $key = "Registry::HKEY_CLASSES_ROOT\*\shell\Open with Notepad"
    New-Item -Path $key"\Command" -Value "Notepad.exe `"%1`"" -Force
    New-ItemProperty -Path $key -Name "Icon" -Value "Notepad.exe" -PropertyType String -Force

    While New-Item creates the key just fine, New-ItemProperty is getting hung up on the astrix in the path, interpreting it as a wildcard. Is there any way around this?

  • #33928

    Participant
    Points: 0
    Rank: Member

    Use single quotes instead of double quotes around your key string. Single quotes indicate a string literal.

    $key = 'Registry::HKEY_CLASSES_ROOT\*\shell\Open with Notepad'
    New-Item -Path $key"\Command" -Value "Notepad.exe `"%1`"" -Force
    New-ItemProperty -Path $key -Name "Icon" -Value "Notepad.exe" -PropertyType String -Force
    

    Alternately, if you want to use double quotes, you can use a back tick to tell powershell not to interpret the special meaning of *

    $key = "Registry::HKEY_CLASSES_ROOT\`*\shell\Open with Notepad"
    New-Item -Path $key"\Command" -Value "Notepad.exe `"%1`"" -Force
    New-ItemProperty -Path $key -Name "Icon" -Value "Notepad.exe" -PropertyType String -Force
    
  • #33929

    Member
    Points: 0
    Rank: Member

    That's not the first time I've seen PowerShell's path abstractions have problems with the realities of the registry. (It also has trouble with paths that contain forward slashes in a key name.)

    You can work around this by using the underlying .NET methods instead:

    $hive = [Microsoft.Win32.RegistryKey]::OpenBaseKey('ClassesRoot', 'Default')
    $subKey = $hive.CreateSubKey('*\shell\Open with Notepad', $true)
    $subkey.SetValue('Icon', 'Notepad.exe', 'String')
    $subKey = $subKey.CreateSubKey('Command', $true)
    $subkey.SetValue($null, "Notepad.exe `"%1`"", 'String')
    

    I'm not 100% sure if I've translated your code properly, but you can run this and then verify the proper keys / values with regedit.

  • #33930

    Participant
    Points: 0
    Rank: Member

    Thanks, for the tip about the single quotes, Curtis. I wasn't aware they're treated differently. Unfortunately neither option worked in this case.

    Your code worked perfectly, Dave. Thanks! I guess these cmdlets never expected to have to deal with these otherwise illegal characters.

  • #33938

    Participant
    Points: 0
    Rank: Member

    I tested with the escape character and it worked for me. Granted, the New-ItemProperty took a while, but it completed.

    $key = "Registry::HKEY_CLASSES_ROOT\`*\shell\Open with Notepad"
    New-Item -Path $key"\Command" -Value "Notepad.exe `"%1`"" -Force
    New-ItemProperty -Path $key -Name "Icon" -Value "Notepad.exe" -PropertyType String
    

The topic ‘Can't Set New-ItemProperty to Registry Path Containing Astrix’ is closed to new replies.