Question:
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?
1 2 3 4 |
Write-Host "Begin" Test-Path "HKLM:\SOFTWARE\Classes\*\shell\Some shell extension" Write-Host "End" |
Answer:
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.
1 2 3 4 |
Write-Host "Begin" Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\*\shell\Some shell extension" Write-Host "End" |