2017-11-11 215 views
1

我需要检查一些路径是否存在于注册表中。 我用powershell。 但我有 “-match”搜索一些与“”与powershell的字符串 - 匹配运算符

$reg1 = "C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\SafeNet\LunaClient\win32;C:\Program File 
s\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\System Center Operations Manager 2007\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit" 

一个问题,当我使用-match运算符:

$reg1 -match "\Windows Kits\10" 

$reg1 -match "\Windows Kits\10" 

我得到 “假”

我不知道什么是错的。

回答

1

“匹配”运算符使用正则表达式,而“l​​ike”运算符将允许您使用通配符。如果您切换到“喜欢”并在其周围放置*符号,您应该开始获得您的匹配。

$reg1 -like "*\Windows Kits\10*" 

或者,如果你真的想使用正则表达式,你需要逃避你斜线你要搜索的的字符串中。它最终会看起来像这样:

$reg1 -match "\\Windows Kits\\10" 
+0

第一种方法它的工作,但第二:$ REG1 -match “\/\的Windows套件/ \ 10 \” 不,-illegal \月底,没有\在最后也不能工作 – mino

+0

哎呀 - 看起来像我在正则表达式中使用错误的斜杠方向作为转义字符。现在应该修复 –

+0

现在感谢它的工作:)但替代方法必须以“\”结尾;) – mino

1

反斜杠是正则表达式中的一个特殊字符。在正则表达式中,你必须使用反斜杠来转义特殊字符。例如,

'C:\Windows' -match '\\Windows' 
True 

在你的情况..

$reg1 -match "\\Windows Kits\\10" 
+0

谢谢,现在它工作:) – mino