# Matching on multiple lines with -contains or -like

**URL:** <https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811>\
**Category:** PowerShell Help\
**Created:** [August 17, 2020, 8:51am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811 "2020-08-17T08:51:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike-ratcliffe](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@mike-ratcliffe](https://forums.powershell.org/u/mike-ratcliffe)\
**Post date:** [August 17, 2020, 8:51am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/1 "2020-08-17T08:51:45Z")

</div>

Hello,

I would like to be able to match on multiple lines and I am having troubles trying to figure out how to do this. I have a feeling I am going to need regex but I am not sure. Any help would be greatly appreciated.

`$url = “http://contoso.com/robots.txt”`

`$disallowall = {`  
`User-agent: *`  
`Disallow: /`  
`}`

`$robots = (Invoke-WebRequest -Uri $url -ErrorAction Stop).content | Out-String`  
`if ($robots -contains $disallowall){`

`Write-Host “yes” -ForegroundColor Green`

`}`

The issue I am having is “`$robots -contains $disallowall`” says its false even though $disallowall definitely contains these two lines. I have tried to use “_$disallow_” and other variations but cannot get this to match on the multiple lines in $disallowall variable with -contains or -like. Obviously the URL in this example wouldnt match but in my working example it does.

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [August 17, 2020, 9:31am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/2 "2020-08-17T09:31:39Z")

</div>

It is unclear if `{}` are required in your match string. A beginning and ending pair of `{}` denotes a script block in PowerShell. When comparing strings, it is likely unnecessary. Using quotes, here-strings, and other string formatting mechanisms will be the way to go.

If the downloaded content has line breaks that match your `[console]::NewLine` configuration, you could simply just use a here-string with the `-like` operator:

```
$disallowall = @'
User-agent: *
Disallow: /
'@

$robots = (Invoke-WebRequest -Uri $url -ErrorAction Stop).content | Out-String

if ($robots -like "*$disallowall*") {

    Write-Host "yes" -ForegroundColor Green

}
```

If the line break characters are unknown, you can test with `-match`, which uses regex. That will allow for a specific newline character pattern match. This will match 0 or 1 carriage returns and a line feed. It also escapes the literal `*`.

```
$disallowall = 'User-agent: \*\r?\nDisallow: /'

$robots = (Invoke-WebRequest -Uri $url -ErrorAction Stop).content | Out-String

if ($robots -match $disallowall) {

    Write-Host "yes" -ForegroundColor Green

}
```

---

<div class="post-metadata">

**Author:** ![mike-ratcliffe](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@mike-ratcliffe](https://forums.powershell.org/u/mike-ratcliffe)\
**Post date:** [August 17, 2020, 9:37am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/3 "2020-08-17T09:37:53Z")

</div>

It appears your code blocks took on some formatting. Can you please re-post your code suggestions?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 17, 2020, 9:37am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/4 "2020-08-17T09:37:57Z")

</div>

There are several issues. First, are you sure that Invoke-RestMethod is returning what you expect as it’s odd to refer directly to a txt file. The $disallowall is defined as a script block. The contains operator is an array comparison:

```
PS C:\WINDOWS\system32> "red","white","blue" -contains "white"
True

PS C:\WINDOWS\system32> "red","white","blue" -contains "purple"
False
```

This most likely would not work unless the content is array and a line matches exactly. What exactly does the $robots contain?

---

<div class="post-metadata">

**Author:** ![mike-ratcliffe](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@mike-ratcliffe](https://forums.powershell.org/u/mike-ratcliffe)\
**Post date:** [August 17, 2020, 9:45am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/5 "2020-08-17T09:45:17Z")

</div>

Rob,

Using my real URL (not [contoso.com](http://contoso.com)), the $robots contains the following.

#robots.txt file

User-agent: \*  
Disallow: /

I am checking robots.txt to confirm that file contains the following two lines consecutively.

User-agent: \*  
Disallow: /

Also, I have tried to match on this using regex and I cannot get that working either.

$robots -match [regex]“User-agent: \*[\r\n]Disallow: /”)

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 17, 2020, 9:52am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/6 "2020-08-17T09:52:44Z")

</div>

When using the `-contains` operator on an array, it will look at/match elements.

```auto
$array = @(
    "one",
    "two",
    "three"
)

$array -contains 'three' # true

$array -contains 'hree' # false

```

String types have a .Contains method you can use to find text. Something like this works

`($array | Out-String).Contains('hree') # true`

---

<div class="post-metadata">

**Author:** ![mike-ratcliffe](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@mike-ratcliffe](https://forums.powershell.org/u/mike-ratcliffe)\
**Post date:** [August 17, 2020, 10:10am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/7 "2020-08-17T10:10:16Z")

</div>

Thanks Adminofthings45! This seemed to be the part I needed.

$disallowall = @‘  
User-agent: \*  
Disallow: /  
’@

Can you tell me why you wrap the variable value in @? I understand that leading with @ forces it to be an array (even if it only contains a single value) however why is it necessary to end with @ also?

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [August 17, 2020, 10:13am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/8 "2020-08-17T10:13:39Z")

</div>

`@‘’@` is a [here-string](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7). It is used to help preserve formatting for strings that span multiple lines.

---

<div class="post-metadata">

**Author:** ![mike-ratcliffe](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@mike-ratcliffe](https://forums.powershell.org/u/mike-ratcliffe)\
**Post date:** [August 17, 2020, 11:19am UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/9 "2020-08-17T11:19:14Z")

</div>

Perfect! Thank you AdminOfThings45! I really appreciate your guidance.

---

<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:29pm UTC](https://forums.powershell.org/t/matching-on-multiple-lines-with-contains-or-like/14811/10 "2024-05-16T20:29:04Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


