Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a registry key with an asterisk to Test-Path

I want to run this registry path by Test-Path in PowerShell but it contains an asterisk, which is valid in the registry but not in Windows paths.

The problem is, when I pass it, Test-Path treats the asterisk as a wild card, so this takes a very, very long time because it checks all the sub paths of Classes and is not anything like what I want to test anyway.

Is there any way I can correctly pass that asterisk? Some escape mechanism?

Write-Host "Begin"
Test-Path "HKLM:\SOFTWARE\Classes\*\shell\Some shell extension"
Write-Host "End"
like image 517
joshcomley Avatar asked Jul 19 '15 21:07

joshcomley


People also ask

How do I query a registry key in PowerShell?

One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .

What is a registry key path?

A registry key can be thought of as being a bit like a file folder, but it exists only in the Windows Registry. Registry keys contain registry values, just like folders contain files. Registry keys can also contain other registry keys, which are sometimes referred to as subkeys.

What does test-path do in PowerShell?

The Test-Path cmdlet determines whether all elements of the path exist. It returns $True if all elements exist and $False if any are missing. It can also tell whether the path syntax is valid and whether the path leads to a container or a terminal or leaf element.


1 Answers

Use the -LiteralPath parameter to prevent globbing/wildcard matching.

-LiteralPath<String[]>

Specifies a path to be tested. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.

Write-Host "Begin"
Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\*\shell\Some shell extension"
Write-Host "End"
like image 197
Ansgar Wiechers Avatar answered Oct 15 '22 13:10

Ansgar Wiechers