0

The value of $DeviceID is : "PCI\VEN_8086&DEV_9D3A&SUBSYS_225617AA&REV_21\3&11583659&1&B0"

I'm trying to search for that string in .INF files with "Select-String" :

 Select-String -Path C:\file.inf -Pattern "$DeviceID"

But it won't take the string as is, it's having a problem with the "\V":

Select-String : La chaîne PCI\VEN_8086&DEV_9D3A&SUBSYS_225617AA&REV_21\
3&11583659&1&B0 n’est pas une expression régulière valide: analyse de
"PCI\VEN_8086&DEV_9D3A&SUBSYS_225617AA&REV_21\3&11583659&1&B0" - Séquence
d'échappement \V non reconnue.
Au caractère Ligne:15 : 5
+     Select-String -Path $($_.FullName) -pattern "$($erreur.DeviceID)"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument : (:) [Select-String], ArgumentException
    + FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand

Sorry for the french but it basically says "String is not a valid regex. Escape sequence \V not recognized".

3 Answers 3

1

Select-String, by default, uses the .NET regex engine. To do simple string matching, use the -SimpleMatch switch parameter:

Select-String -Path C:\file.inf -pattern "$DeviceID" -SimpleMatch
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, forgot about that switch! Added a further question about regex if you can chime in on that too.
@Rakha $null = $id -match "VEN_\w{4}&DEV_\w{4}" (for future reference, please post a new question rather than updating the existing question if you have more/new/followup/different questions)
1

PowerShell is trying to do a regular expression match. Add the -SimpleMatch switch to look for the literal string in $DeviceID, without regex.

Select-String -Path C:\file.inf -Pattern $DeviceID -SimpleMatch

1 Comment

Thanks, forgot about that switch! Added a further question about regex if you can chime in on that too.
1

I see you already have your answer but there is another switch that will do this as well.

For comparison...

-list

select-string -path "$TargeUNC\*.ps1" -Pattern 'Get-WmiObject' -list | 
Select-Object -First 3

# Results

2018-01-15 Enable the Disk Cleanup tool on Windows Server.ps1:45:$wmiOS = Get-WmiObject -Class Win32_OperatingSystem
3D_chart.ps1:1:get-wmiobject win32_perfformatteddata_perfdisk_logicaldisk
7 cmdlet Hyper-V Tips.ps1:15:$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class 

-vs -SimpleMatch

select-string -path "$TargeUNC\*.ps1" -Pattern 'Get-WmiObject' -SimpleMatch | 
Select-Object -First 3

# Results

2018-01-15 Enable the Disk Cleanup tool on Windows Server.ps1:45:$wmiOS = Get-WmiObject -Class Win32_OperatingSystem
3D_chart.ps1:1:get-wmiobject win32_perfformatteddata_perfdisk_logicaldisk
7 cmdlet Hyper-V Tips.ps1:15:$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.