Extracting Specific value within String via powershell

Nandan Hegde 30,716 Reputation points MVP
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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,446 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,596 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