# Assign output to variable and checking if string exists in it only work after outfile

**URL:** <https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495>\
**Category:** PowerShell Help\
**Created:** [February 3, 2022, 7:41am UTC](https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495 "2022-02-03T07:41:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Netanelp](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Netanelp](https://forums.powershell.org/u/Netanelp)\
**Post date:** [February 3, 2022, 7:41am UTC](https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495/1 "2022-02-03T07:41:54Z")

</div>

Hey guys,  
I’m having some issues with assigning a command output to a variable and then checking if a string is present in that variable.

Basically what I’m trying to do is get a list of my computer’s static route and check if a list of IPs I have is present in the static routes.

```auto
$NSLookup = (nslookup www.google.com | Select-String -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches).Matches.Value | where { $_ -notmatch "10.105.203.50"}
Get-NetRoute | where{$_.NextHop -eq '192.168.110.253'} | select DestinationPrefix | ft -HideTableHeaders | Out-File 'C:\routes.txt'
$StaticRoute = Get-Content 'C:\routes.txt'
$Mask = x.x.x.x
$Gateway = x.x.x.x

foreach($IP in $NSLookup){
if($StaticRoute -match $IP ){
Write-Host $IP "Already in static addresses"

}else{
route add -p $IP mask $Mask $Gateway
Write-Host $IP "Added to static route"
}
}

```

As you can see from the script i’m outputting the **Get-NetRoute** command to txt and then extracting the content out of it - this works fine.  
What I was trying to do was to assign the Get-NetRoute command directly to the variable and checking if static routes variable contains that IP.For some reason It was not able to give me correct results and was always giving me that static routes does not contain this IP even if it does.

Thanks in advance

---

<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:** [February 3, 2022, 9:00am UTC](https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495/2 "2022-02-03T09:00:39Z")

</div>

Hi,

This is a bit hard to troubleshoot because you’ve posted a working script 🙂

Can you show us how you were assigning the output from `Get-NetRoute` to the variable and maybe we can tell you why it wasn’t working? If I had to guess, you were trying to use `ft` or `Format-Table` and that broke it.

If you change it to the line below, it should work:

```auto
$staticRoute = Get-NetRoute | Where-Object {$_.NextHop -eq '0.0.0.0'} | Select-Object DestinationPrefix

```

If you want to remove the header from the variable use:

```auto
$staticRoute = Get-NetRoute | Where-Object {$_.NextHop -eq '0.0.0.0'} | Select-Object -ExpandProperty DestinationPrefix

```

---

<div class="post-metadata">

**Author:** ![Netanelp](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Netanelp](https://forums.powershell.org/u/Netanelp)\
**Post date:** [February 3, 2022, 9:25am UTC](https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495/3 "2022-02-03T09:25:48Z")

</div>

Haha yeah I guess.I will try to find the draft of the not working one and post it.  
But i’m pretty sure it was looking like this:

```auto
$StaticRoute = Get-NetRoute | where{$_.NextHop -eq '192.168.110.253'} | select DestinationPrefix | ft -HideTableHeaders 

```

I will check if the format table is the reason this was not working well but any idea why is it so?

---

<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:** [February 3, 2022, 9:42am UTC](https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495/4 "2022-02-03T09:42:27Z")

</div>

The `Format` cmdlets add special formatting that’s meant for on-screen display. They can also be used with some of the `Out` cmdlets but you can’t pipe them to other cmdlets and you’ll get unexpected results when you assign them to a variable.

Compare:

```auto
Get-NetRoute | Where-Object {$_.NextHop -eq '0.0.0.0'} | 
    Select-Object DestinationPrefix | ConvertTo-Html

```

and

```auto
Get-NetRoute | Where-Object {$_.NextHop -eq '0.0.0.0'} | 
    Select-Object DestinationPrefix | Format-Table -HideTableHeaders | ConvertTo-Html

```

---

<div class="post-metadata">

**Author:** ![Netanelp](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Netanelp](https://forums.powershell.org/u/Netanelp)\
**Post date:** [February 3, 2022, 12:56pm UTC](https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495/5 "2022-02-03T12:56:44Z")

</div>

Thank you very much for the explanation this is now working great.  
I’ve used the line you provided in your first comment removing the header.

THANKS!

---

<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:19pm UTC](https://forums.powershell.org/t/assign-output-to-variable-and-checking-if-string-exists-in-it-only-work-after-outfile/18495/6 "2024-05-16T20:19:41Z")

</div>

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


