How Can I Check if a Specific Site ID is in File Contents with PowerShell?

0
6
Asked By CuriousCat88 On

I'm using PowerShell's Get-Content to read the contents of a file and store it in a variable, like this:

$VAR1 = Get-Content c:\file,info

The content looks like this:

"server":"https://somewebsite.com","newConfiguration":false,"proxy":"","site":"0088775487c2"

Now, I want to create a second variable that checks if the string "site":"0088775487c2" is in the file contents. If it's there, the variable should be set to "yes"; if not, it should be "no".

1 Answer

Answered By TechWhiz42 On

Have you tried using an if statement with a regex match? It sounds like you might not need a second variable at all. You can directly check for the presence of that string in your first variable. You could do something like this:

```powershell
$VAR1 = Get-Content c:\file,info
$exists = if ($VAR1 -match '"site":"0088775487c2"') {'yes'} else {'no'}
```

This way, you get 'yes' or 'no' right in the check! If you have any other specific requirements, let me know!

UserInNeed -

Thanks! That's exactly what I was looking for. I really appreciate the help!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.