0

I have a PS Script that should generate a html file with add-content $datei works fine but here is the Problem, I have something like that:

"some html... <textarea id="Text$counter" style="width: 100%; height: 100%;"></textarea> some html... <span>$NameEdit</span> some html... document.getElementById('Text$counter').select(); some html... " | add-content $datei 

so i have multible double and single qoutes and multible variables in the html part but i cant find out how to escape all that stuff right that the script runs as expected AND fill in the variables in the html code

im using PS 7.0

2
  • You should look at here-strings and the -f (format) operator. Commented Apr 17, 2020 at 17:56
  • I read this and some other sites but there is never an example for the problem i have :/ learn.microsoft.com/en-us/powershell/module/… Commented Apr 17, 2020 at 18:09

1 Answer 1

1

The solution you probably want is a Here String But you could also use the escape char `

@" some html... <textarea id="Text$counter" style="width: 100%; height: 100%;"></textarea> some html... <span>$NameEdit</span> some html... document.getElementById('Text$counter').select(); some html... "@ | add-content $datei 

To use a Here string you need to have the @" or @' then a new line where you will put your string. Then to close a Here string you will need to have a new line and "@ or '@. The "@ or '@ must be at the very first start of the new line.

Here are some examples that would fail

#This will NOT work @" Hey There "Buddy" "@ #This will NOT work @"Hey There "Buddy""@ #This will NOT work @" Hey There "Buddy""@ 

Here are couple Working correctly examples

#This WILL work @" Hey There "Buddy" "@ #This WILL work @" Hey There "Buddy" "@ #This WILL work $Test = @" Hey There "Buddy" "@ 

It does make beautifying code harder Example

Function Test(){ $Text1 = "Hello There" $Text2 = @" "Buddy" "@ return "$Text1 $Text2" } 

Now lets go over the escape char ` Its called a Backtick.

"Hey There `"Buddy`"" 

Is equal to Hey There "Buddy"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.