Extracting Specific value within String via powershell

Nandan Hegde 36,146 Reputation points MVP Volunteer Moderator
2022-11-06T14:57:29.153+00:00

Hey,

Attaching the sample file257559-tempfile.txt

sample image:
257631-image.png

I need to extract the string values encapsulated between the "name": "<<>>",
meaning in image example I need to output to be wait2 and wait3.

I have tried the below code via regex expression but I am not getting the required output. Am I doing something wrong in my below code or there is an another way to achieve it?

 $Pattern='"name": "(.*?)",'  
$ListNm= ([regex]::Matches((Get-Content $TempFilePath),$pattern).groups|Where-Object Name -EQ '1').Value  

where TempFilePath is the location where the file is located.

@Rich Matheisen

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-11-06T16:54:24.393+00:00

    That data look like it's JSON. but it won't pass validation.

    If all you're looking for is the correct syntax to accomplish the extraction of the value of every property named "name", regardless of where it exists in the structured data, then this should do it:

    $x = get-content c:\junk\something.json -raw   
      
    $Pattern='"name": "(.*?)",'  
    $ListNm= ( ([regex]::Matches($x,$pattern) ).groups | Where-Object {$_.Name -EQ '1'}).value  
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.