# Powershell: parsing html links (meta tags)

**URL:** <https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814>\
**Category:** PowerShell Help\
**Created:** [June 14, 2021, 5:43pm UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814 "2021-06-14T17:43:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)\
**Post date:** [June 14, 2021, 5:43pm UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814/1 "2021-06-14T17:43:50Z")

</div>

hi, I must to copy the link from top line to the bottom lines, in several html files. Each html files has an unique **“canonical”** link. For example:

```
<link rel="canonical" href="https://website.com/en/america.html" />

<html code>
<html code>

<div class="somers"><a href="https://website/darertss.html" class="flags bg" hreflang="bg" title="bk"></a>
<a href="https://website.com/pas-lofet.html" class="flags sk" hreflang="sk" title="sk"></a>
<a href="https://website.com/latinamer.html" class="flags uk" hreflang="uk" title="uk"></a>
<a href="https://website.com/sacrdo.html" class="flags uk" hreflang="uk" title="uk"></a>

```

**The output should be**

```
<div class="somers"><a href="https://website.com/en/america.html" class="flags bg" hreflang="bg" title="bk"></a>
<a href="https://website.com/en/america.html" class="flags sk" hreflang="sk" title="sk"></a>
<a href="https://website.com/en/america.html" class="flags uk" hreflang="uk" title="uk"></a>
<a href="https://website.com/america.html" class="flags uk" hreflang="uk" title="uk"></a>

```

**My powershell code is almost good, but only replaces the first line (the one with \<div class…). And I must replace all the lines**

```auto
$sourcedir = "C:\Folder1\"
  $resultsdir = "C:\Folder1\"
  Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {
  $content = Get-Content -Path $_.FullName -Raw
  $replacementValue = (Select-String -InputObject $content -Pattern '(?<=<a href=").+(?=</a>)').Matches.Value
  $replaceValue = (Select-String -InputObject $content -Pattern '(?<=<link rel="canonical" href=").+(?=" />)').Matches.Value
  $content.Replace("$replacementValue", "$replaceValue") | Out-File -FilePath $resultsdir\$($_.name)
  }

```

Also, I try to use -AllMatches, but doesn’t work. Can anyone update my code a little bit so as to replace all the lines ?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 15, 2021, 8:10am UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814/2 "2021-06-15T08:10:16Z")

</div>

```auto
$sourcedir = "E:\Temp\Folder1\"
$resultsdir = "E:\Temp\Folder2\"

Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {
    $content = Get-Content -Path $_.FullName -Raw
    $replaceValue = (Select-String -InputObject $content -Pattern '(?<=<link rel="canonical" href=").+(?=" />)').Matches.Value
    $content = $content -replace 'https:\/\/.+.html',$replaceValue
    Set-Content -Path $resultsdir\$($_.name) $content
}

```

---

<div class="post-metadata">

**Author:** ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)\
**Post date:** [June 15, 2021, 8:41am UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814/3 "2021-06-15T08:41:04Z")

</div>

thanks a lot @matt-bloomfield

---

<div class="post-metadata">

**Author:** ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)\
**Post date:** [June 15, 2021, 8:52am UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814/4 "2021-06-15T08:52:12Z")

</div>

by the way, @matt-bloomfield I’m thinking of a similar case. For example:

```auto
  <html code>
  <html code>
	
	<link rel="canonical" href="https://website.com/en/laptop.html" />

    <html code>
    <html code>

<meta property="og:url" content="https://website.com/accente-pronunce.html"/>

    <html code>
    <html code>
	

"@id": "https://website.com/mom-and-dad.html"

```

So, I want to parse the canonical link to the other links below, from meta and @id.

I don’ know why is not working my code (I update your code a little bit)

```auto
$sourcedir = "C:\Folder1\"
 $resultsdir = "C:\Folder1\"

Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {
   $content = Get-Content -Path $_.FullName -Raw
   $replaceValue = (Select-String -InputObject $content -Pattern '(?<=<link rel="canonical" href=").+(?=" />)').Matches.Value
   $content = $content -replace '(?<=<meta property="og:url" content=").+(?="/>)',$replaceValue
   $content = $content -replace '(?<="@id": ").+(?=")',$replaceValue
    Set-Content -Path $resultsdir\$($_.name) $content

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 15, 2021, 10:16am UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814/5 "2021-06-15T10:16:46Z")

</div>

If you look at my code, I simplified the regular expression to just replace `https://<anything>.html`, this would work regardless of what else you have between the tags.

---

<div class="post-metadata">

**Author:** ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)\
**Post date:** [June 15, 2021, 1:21pm UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814/6 "2021-06-15T13:21:49Z")

</div>

DONE. Thanks @matt-bloomfield

```auto
$sourcedir = "C:\Folder1\"
 $resultsdir = "C:\Folder1\"

Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {
   $content = Get-Content -Path $_.FullName -Raw
   $replaceValue = (Select-String -InputObject $content -Pattern '(?<=<link rel="canonical" href=").+(?=" />)').Matches.Value
   $content = $content -replace '(?<=<meta property="og:url" content=").+(?="/>)',$replaceValue
      Set-Content -Path $resultsdir\$($_.name) $content
	  
   $content = $content -replace '(?<="@id": ").+(?=")',$replaceValue
  Set-Content -Path $resultsdir\$($_.name) $content
}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:24pm UTC](https://forums.powershell.org/t/powershell-parsing-html-links-meta-tags/16814/7 "2024-05-16T20:24:12Z")

</div>


