Jump to content

PowerShell string both quotes

I'm writing a PowerShell script for work to quicky configure the config files so I can test my server locally as is standard. 

 

One of the lines I need to change has both single quotes and double quotes but I can't seem to find how to include both properly in a string variant. 

 

I have tried:

$var = 'myconfig''stuff'' and "other stuff"'
$var = "myconfig'stuff' and `"other stuff`""
$var myconfig'stuff' and "other stuff"

The third variable causes errors. 

 

The first two don't seem to match the text closely enough. 

 

If I don't include correct "ignore this quote" things like the back quote or extra single quote, then it throws a runtime error. 

 

How do I include both quotes and replace the file in the text?

 

I found the following command online that works for the other lines I'm replacing:

(Get-content -path $file -Raw) -replace $beforeText, $afterText | Set-Content -Path $file

 

Link to comment
https://linustechtips.com/topic/1468452-powershell-string-both-quotes/
Share on other sites

Link to post
Share on other sites

27 minutes ago, fpo said:

How do I include both quotes and replace the file in the text?

The first two variable should work.

  • start string with single quote. You use double single quote to escape
  • start string with double quote. You use backtick to escape

Third variable is not correct syntax?

Link to post
Share on other sites

17 minutes ago, C2dan88 said:

The first two variable should work.

  • start string with single quote. You use double single quote to escape
  • start string with double quote. You use backtick to escape

I thought they would but they don't seem to be. 

 

The literal text might be what it's looking for and then it might be not finding it because of the backquote or extra single quote somehow. 

 

I'll have to look into forcing a version of PowerShell maybe. 

17 minutes ago, C2dan88 said:

Third variable is not correct syntax?

Correct, but I wanted to be sure it wouldn't work. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×