user3770612
user3770612

Reputation: 1779

how to write a script to read double quotes in powershell

Hi every one i am trying to read a string which is enclosed in double quotes within a file e.g "cars" : I am using this command but unable to read it may be I am not using the right format to read the quotes

$houseid = $json |Get-Content |Select-String -pattern ""houseid":"| measure | select -exp count

write-host "houseid = " $houseid 

I am reading the file from json file

Upvotes: 1

Views: 189

Answers (1)

Frode F.
Frode F.

Reputation: 54981

Wrap you pattern in single quotes when it contains quotes. Although it's not necessary, you should add -SimpleMatch to specify that it is a normal string-pattern and not regex.

#Valid sample
'"houseid":myjsonvalue' | Select-String -Pattern '"houseid":' -SimpleMatch | measure | select -ExpandProperty count    
1

#Invalid sample
'houseid:myjsonvalue' | Select-String -Pattern '"houseid":' -SimpleMatch | measure | select -ExpandProperty count
0

Upvotes: 2

Related Questions