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,462 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 (Shanghai Wicresoft Co., Ltd.) 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. Andreas Baumgarten 104K Reputation points MVP
    2021-05-16T12:55:30.973+00:00

    Hi @Suzana Eree ,

    What do you mean with "replace somehow those quotation marks/Double Quotes"? Replace with what? Or do you just want to remove the quotation marks/Double Quotes?

    You can use Trim to remove the quotation marks of a variable.

    $a = '"a test"'  
    $a  
    $b = $a.Trim('"')  
    $b  
    

    If you want to replace the quotation marks you can use this ( double quotes will be replaced with #):

    $a = '"a test"'  
    $a  
    $b = $a.Replace('"',"#")  
    $b  
    

    Another option could be to split at the ":

    $c = '<meta name="keywords" content="MY TEXT HERE"/>'  
    $d = ($c.Split('"'))[3]  
    $d  
    

    ----------

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

    Regards
    Andreas Baumgarten


  2. Suzana Eree 811 Reputation points
    2021-05-16T16:24:17.717+00:00

    hello @Andreas Baumgarten I mean it doesn't working. I believe the quotes before and after regex is the problem.

    See this print screen please.

    iBGXc1.jpg

    0 comments No comments

  3. Andreas Baumgarten 104K Reputation points MVP
    2021-05-16T19:15:47.68+00:00

    Hi @Suzana Eree ,

    please try this:

    $c = '<meta name="keywords" content="MY TEXT HERE"/>'  
    Select-String -InputObject $c -Pattern "(<meta name=`"keywords`" content=`").+(`"/>)"  
    

    ----------

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

    Regards
    Andreas Baumgarten


  4. Andreas Baumgarten 104K Reputation points MVP
    2021-05-16T20:01:28.41+00:00

    Hi @Suzana Eree ,

    please try this to get the "ReplaceValue" :

    $replaceValue = ((Select-String -InputObject $content -Pattern "(<meta name=`"keywords`" content=`").+(`"/>)").Matches.Value).Split('"')[3]  
    

    ----------

    (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