How do I escape quotation marks/Double Quotes in a powershell string?

Suzana Eree 811 Reputation points
2021-05-16T08:52:44.36+00:00

I have the code below. What does this code do. It make a PARSING. I replace a little bit a powershell code that can be found HERE

So, I must copy the content from <meta name="keywords" content="MY TEXT HERE"/> and paste it into the content of <title>MY TITLE</title>.

After running Powershell, I will get <title>MY TEXT HERE</title> .

$sourcedir = "C:\Folder1\"  
 $resultsdir = "C:\Folder2\"  
 Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {  
     $content = Get-Content -Path $_.FullName -Raw  
     $replaceValue = ((Select-String -Pattern "(<title>).+(</title>)" -InputObject $content).Matches.Value).Split(" ")[1]    
     $findReplacement = Select-String -Pattern "(<meta name="keywords" content=").+("/>)" -InputObject $content  
     $split = ($findReplacement.Matches.Value).Split(" ")  
     $i = 0  
     do {  
         $find"/> = $split[$i]  
         $i++      
     } until ($find"/> -eq ""/>")  
     $replacementValue = ($split[1..($i - 2)]) -join ' '  
     $content.Replace("$replaceValue", "$replacementValue") | Out-File -FilePath $resultsdir\$($_.name)    
 }  

The errors are on the lines that contains quotation marks/Double Quotes on the REGEX formulas, as below

"(<title>).+(</title>)" - This will select all words from the TITLE tag

"(<meta name="keywords" content=").+("/>)" - This will select all the words from the KEYWORDS tag

} until ($find"/> -eq ""/>") - This "/> represents the closing tag from the KEYWORDS tag

So, I must replace somehow those quotation marks/Double Quotes as to work well. Can anyone help me with an updated code?

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,551 questions
0 comments No comments
{count} votes

Accepted answer
  1. Suzana Eree 811 Reputation points
    2021-05-17T09:32:32.907+00:00

    @Ian Xue and @Andreas Baumgarten Thank you very much. I write the complete code here.

     $sourcedir = "C:\Folder1\"  
     $resultsdir = "C:\Folder2\"  
     Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {  
     $content = Get-Content -Path $_.FullName -Raw  
     $replacementValue = (Select-String -InputObject $content -Pattern '(?<=<meta name="keywords" content=").+(?="/>)').Matches.Value  
     $replaceValue = (Select-String -InputObject $content -Pattern '(?<=<title>).+(?=</title>)').Matches.Value  
     $content.Replace("$replaceValue", "$replacementValue") | Out-File -FilePath $resultsdir\$($_.name)  
     }  
    
    0 comments No comments

13 additional answers

Sort by: Most helpful
  1. Suzana Eree 811 Reputation points
    2021-05-17T04:17:40.38+00:00

    good morning sir @Andreas Baumgarten

    I test your new code, this is what I get:

    9SVYfP.jpg

    0 comments No comments

  2. Andreas Baumgarten 111.3K Reputation points MVP
    2021-05-17T07:43:09.747+00:00

    Hi @Suzana Eree ,

    it looks like the $replaceValue or the $replacementValue (my guess is the first one) is empty.
    You should check both variables and their values.

    As I wrote before: Here the script works with the content of the 2 html files I posted above.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  3. Andreas Baumgarten 111.3K Reputation points MVP
    2021-05-17T08:06:57.253+00:00

    Please post the values of $replaceValue and $replacementValue after you run the script.

    $replaceValue
    $replacementValue
    

    Please post the output if you run the 2 lines above (after the script is finished).


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. Suzana Eree 811 Reputation points
    2021-05-17T08:30:29.107+00:00

    I get a blank file after running the code. I check if it is UTF-8, and it is.

    JYtVnd.jpg


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.