# Get Exclusive value

**URL:** <https://forums.powershell.org/t/get-exclusive-value/16176>\
**Category:** PowerShell Help\
**Created:** [April 5, 2021, 3:22pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176 "2021-04-05T15:22:46Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 5, 2021, 3:22pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/1 "2021-04-05T15:22:46Z")

</div>

Hello everybody,  
I have a script that collects a series of information and now I came across unexpected behavior. The data is in a list in the following format:

```
1/1/0
1/1/1
1/1/2
1/1/3
.
.
.
1/1/10
1/1/11
```

My problem is that I always need to obtain the absolute value based on an entry defined by the user, for example: 1/1/1  
the problem is that in this case, my loop selects the values ​​that contain this data, example: 1/1/10, 1/1/11, 1/1/12 …  
Would anyone have an idea how to get around this?

---

<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:** [April 5, 2021, 3:39pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/2 "2021-04-05T15:39:58Z")

</div>

How are you selecting the values? If you use regex you can use the end of line anchor

```auto
$value -match ‘1/1/1$’

```

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 5, 2021, 3:55pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/3 "2021-04-05T15:55:50Z")

</div>

Hello, thank for your reply!

Here is the how I am selecting:

```auto
$lookupTable | Where-Object { $_.PortName -match [REGEX]::escape($userInput) }

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 5, 2021, 3:59pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/4 "2021-04-05T15:59:09Z")

</div>

Could you please format your code as code here in the forum. There are charachters missing in your code.

When you post code, error messages, sample data or console output format it as code, please.

Here you can read how that works: Guide to Posting Code.

> [@Guide to Posting Code - Redux](https://forums.powershell.org/t/guide-to-posting-code-redux/15797):
>
> Discourse uses [Markdown](https://www.markdownguide.org/getting-started/) for text formatting. There is a reference sheet and quick tutorial here: [Markdown Reference](https://commonmark.org/help/) You can produce in-line code formatting by wrapping your code in backticks (`). For example, `Get-ChildItem` produces Get-ChildItem. For larger code blocks, place a line containing three backticks above and below your code. For example ``` # 256-Color Foreground & Background Charts echo “`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m” foreach ($fgbg in 38,48) { #…

You can go back and edit your existing post. You don’t have to create a new one. 😉

Thanks in advance.

---

<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:** [April 5, 2021, 4:09pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/5 "2021-04-05T16:09:29Z")

</div>

```auto
$lookupTable = 0..25 | foreach {"1/1/{0}" -f $_} 
$userInput = Read-Host 'Select a port'
$lookupTable | Where{$_ -like "*/$userInput"}

```

Output:

```auto
Select a port: 22
1/1/22

```

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 5, 2021, 5:23pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/6 "2021-04-05T17:23:26Z")

</div>

Thanks!! Here is my code to get data:

```auto
 $table = @($getportname)

$lookupTable = ($table) |
ForEach-Object {
    [PSCustomObject]@{
        PortNumber = $_.Substring(24,10)
        PortName = $_ -replace '.*\"(.*?)\"\s*$','$1'
    }
}

$teste = for ($i=1;$i -le 1;$i++) {
    $userInput = $port
   
    $lookupTable | Where-Object { $_.PortName -match [REGEX]::escape($userInput) }

} 

In $userinput have the list with all values in that format: 
0/0/1
1/1/1
1/2/1
 And many other. Have a Variable that we input what data we can get, ex: 1/1/1. If at the list have the value like 1/1/10 or 1/1/11 both appear in the result.
```

---

<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:** [April 5, 2021, 5:45pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/7 "2021-04-05T17:45:35Z")

</div>

If you they are inputting 1/1/1 and you want to find a record for 1/1/1, then why not simply use the equal operator (-eq). Regex is going to find matches versus a specific record

```auto
$lookupTable | Where-Object { $_.PortName -eq $userInput }

```

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 5, 2021, 6:08pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/8 "2021-04-05T18:08:16Z")

</div>

> [@rob-simmers](#):
>
> `$lookupTable | Where-Object { $_.PortName -eq $userInput }`

Thanks Mr. Rob, but I don’t know why do not work with -eq or -ceq or -like… only works with -match or -cmatch.

---

<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:** [April 5, 2021, 6:10pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/9 "2021-04-05T18:10:06Z")

</div>

Can you show a couple rows of $lookupTable?

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 5, 2021, 6:13pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/10 "2021-04-05T18:13:32Z")

</div>

Here:

```auto
.1.3.6.1.2.1.4194312192 = STRING:0/1/0
.1.3.6.1.2.1.4194312448 = STRING:0/1/1
.1.3.6.1.2.1.4194312704 = STRING:0/1/2
.1.3.6.1.2.1.4194312960 = STRING:0/1/3
.1.3.6.1.2.1.4194313216 = STRING:0/1/4
.1.3.6.1.2.1.4194313472 = STRING:0/1/5
.1.3.6.1.2.1.4194313728 = STRING:0/1/6
.1.3.6.1.2.1.4194313984 = STRING:0/1/7
.1.3.6.1.2.1.4194314240 = STRING:0/1/8
.1.3.6.1.2.1.4194314496 = STRING:0/1/9
.1.3.6.1.2.1.4194314752 = STRING:0/1/10
.1.3.6.1.2.1.4194315008 = STRING:0/1/11
.1.3.6.1.2.1.4194315264 = STRING:0/1/12
.1.3.6.1.2.1.4194315520 = STRING:0/1/13
.1.3.6.1.2.1.4194315776 = STRING:0/1/14
.1.3.6.1.2.1.4194316032 = STRING:0/1/15
.1.3.6.1.2.1.4194320384 = STRING:0/2/0
.1.3.6.1.2.1.4194320640 = STRING:0/2/1
.1.3.6.1.2.1.4194320896 = STRING:0/2/2
.1.3.6.1.2.1.4194321152 = STRING:0/2/3
.1.3.6.1.2.1.4194321408 = STRING:0/2/4
.1.3.6.1.2.1.4194321664 = STRING:0/2/5
.1.3.6.1.2.1.4194321920 = STRING:0/2/6
.1.3.6.1.2.1.4194322176 = STRING:0/2/7
.1.3.6.1.2.1.4194322432 = STRING:0/2/8
.1.3.6.1.2.1.4194322688 = STRING:0/2/9
.1.3.6.1.2.1.4194322944 = STRING:0/2/10
.1.3.6.1.2.1.4194323200 = STRING:0/2/11
.1.3.6.1.2.1.4194323456 = STRING:0/2/12
.1.3.6.1.2.1.4194323712 = STRING:0/2/13
.1.3.6.1.2.1.4194323968 = STRING:0/2/14
.1.3.6.1.2.1.4194324224 = STRING:0/2/15 

```

---

<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:** [April 5, 2021, 6:15pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/11 "2021-04-05T18:15:23Z")

</div>

Using your parse of what I assume is your provided in your $table variable, this is what it looks like:

```auto
PortNumber PortName
---------- --------
= STRING:0 .1.3.6.1.2.1.4194312192 = STRING:0/1/0
= STRING:0 .1.3.6.1.2.1.4194312448 = STRING:0/1/1
= STRING:0 .1.3.6.1.2.1.4194312704 = STRING:0/1/2
= STRING:0 .1.3.6.1.2.1.4194312960 = STRING:0/1/3

```

My assumption is you are trying to do something like this:

```auto
$lookupTable = ($table) |
ForEach-Object {
    [PSCustomObject]@{
        PortNumber = ($_ -split ' = ')[1] -replace "STRING:"
        PortName = ($_ -split ' = ')[0]
    }
}
PS C:\Users\rasim> $lookupTable

PortNumber PortName
---------- --------
0/1/0 .1.3.6.1.2.1.4194312192
0/1/1 .1.3.6.1.2.1.4194312448
0/1/2 .1.3.6.1.2.1.4194312704
0/1/3 .1.3.6.1.2.1.4194312960
0/1/4 .1.3.6.1.2.1.4194313216
0/1/5 .1.3.6.1.2.1.4194313472
0/1/6 .1.3.6.1.2.1.4194313728
0/1/7 .1.3.6.1.2.1.4194313984
0/1/8 .1.3.6.1.2.1.4194314240
0/1/9 .1.3.6.1.2.1.4194314496
0/1/10 .1.3.6.1.2.1.4194314752
0/1/11 .1.3.6.1.2.1.4194315008
0/1/12 .1.3.6.1.2.1.4194315264
0/1/13 .1.3.6.1.2.1.4194315520
0/1/14 .1.3.6.1.2.1.4194315776
0/1/15 .1.3.6.1.2.1.4194316032
0/2/0 .1.3.6.1.2.1.4194320384
0/2/1 .1.3.6.1.2.1.4194320640
0/2/2 .1.3.6.1.2.1.4194320896
0/2/3 .1.3.6.1.2.1.4194321152
0/2/4 .1.3.6.1.2.1.4194321408
0/2/5 .1.3.6.1.2.1.4194321664
0/2/6 .1.3.6.1.2.1.4194321920
0/2/7 .1.3.6.1.2.1.4194322176
0/2/8 .1.3.6.1.2.1.4194322432
0/2/9 .1.3.6.1.2.1.4194322688
0/2/10 .1.3.6.1.2.1.4194322944
0/2/11 .1.3.6.1.2.1.4194323200
0/2/12 .1.3.6.1.2.1.4194323456
0/2/13 .1.3.6.1.2.1.4194323712
0/2/14 .1.3.6.1.2.1.4194323968
0/2/15 .1.3.6.1.2.1.4194324224

PS C:\Users\rasim> $lookupTable | Where {$_.PortNumber -eq '0/1/7'}

PortNumber PortName
---------- --------
0/1/7 .1.3.6.1.2.1.4194313984

PS C:\Users\rasim>

```

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 5, 2021, 7:49pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/12 "2021-04-05T19:49:13Z")

</div>

Thanks Mr. Rob!

It is almost!! 🙂

The PortNumber is the data `.1.3.6.1.2.1.4194324224` and the PortName is `0/1/7`. Based PortName the script return the PortNumber. The problem is if I put in the search a value that begin with other, the script return all data. EX:

```auto
0/1/0 .1.3.6.1.2.1.4194312192
0/1/1 .1.3.6.1.2.1.4194312448
0/1/2 .1.3.6.1.2.1.4194312704
0/1/3 .1.3.6.1.2.1.4194312960
0/1/4 .1.3.6.1.2.1.4194313216
0/1/5 .1.3.6.1.2.1.4194313472
0/1/6 .1.3.6.1.2.1.4194313728
0/1/7 .1.3.6.1.2.1.4194313984
0/1/8 .1.3.6.1.2.1.4194314240
0/1/9 .1.3.6.1.2.1.4194314496
0/1/10 .1.3.6.1.2.1.4194314752
0/1/11 .1.3.6.1.2.1.4194315008
0/1/12 .1.3.6.1.2.1.4194315264
0/1/13 .1.3.6.1.2.1.4194315520
0/1/14 .1.3.6.1.2.1.4194315776
0/1/15 .1.3.6.1.2.1.4194316032
0/2/0 .1.3.6.1.2.1.4194320384
0/2/1 .1.3.6.1.2.1.4194320640
0/2/2 .1.3.6.1.2.1.4194320896
0/2/3 .1.3.6.1.2.1.4194321152
0/2/4 .1.3.6.1.2.1.4194321408
0/2/5 .1.3.6.1.2.1.4194321664
0/2/6 .1.3.6.1.2.1.4194321920
0/2/7 .1.3.6.1.2.1.4194322176
0/2/8 .1.3.6.1.2.1.4194322432
0/2/9 .1.3.6.1.2.1.4194322688
0/2/10 .1.3.6.1.2.1.4194322944
0/2/11 .1.3.6.1.2.1.4194323200
0/2/12 .1.3.6.1.2.1.4194323456
0/2/13 .1.3.6.1.2.1.4194323712
0/2/14 .1.3.6.1.2.1.4194323968
0/2/15 .1.3.6.1.2.1.4194324224

```

If getting `0/2/1` is returned:

```auto
0/2/1 .1.3.6.1.2.1.4194320640
0/2/10 .1.3.6.1.2.1.4194322944
0/2/11 .1.3.6.1.2.1.4194323200
0/2/12 .1.3.6.1.2.1.4194323456
0/2/13 .1.3.6.1.2.1.4194323712
0/2/14 .1.3.6.1.2.1.4194323968
0/2/15 .1.3.6.1.2.1.4194324224

```

---

<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:** [April 5, 2021, 8:33pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/13 "2021-04-05T20:33:25Z")

</div>

Again, you should be able to use -eq…

```auto
$table = @"
.1.3.6.1.2.1.4194312192 = STRING:0/1/0
.1.3.6.1.2.1.4194312448 = STRING:0/1/1
.1.3.6.1.2.1.4194312704 = STRING:0/1/2
.1.3.6.1.2.1.4194312960 = STRING:0/1/3
.1.3.6.1.2.1.4194313216 = STRING:0/1/4
.1.3.6.1.2.1.4194313472 = STRING:0/1/5
.1.3.6.1.2.1.4194313728 = STRING:0/1/6
.1.3.6.1.2.1.4194313984 = STRING:0/1/7
.1.3.6.1.2.1.4194314240 = STRING:0/1/8
.1.3.6.1.2.1.4194314496 = STRING:0/1/9
.1.3.6.1.2.1.4194314752 = STRING:0/1/10
.1.3.6.1.2.1.4194315008 = STRING:0/1/11
.1.3.6.1.2.1.4194315264 = STRING:0/1/12
.1.3.6.1.2.1.4194315520 = STRING:0/1/13
.1.3.6.1.2.1.4194315776 = STRING:0/1/14
.1.3.6.1.2.1.4194316032 = STRING:0/1/15
.1.3.6.1.2.1.4194320384 = STRING:0/2/0
.1.3.6.1.2.1.4194320640 = STRING:0/2/1
.1.3.6.1.2.1.4194320896 = STRING:0/2/2
.1.3.6.1.2.1.4194321152 = STRING:0/2/3
.1.3.6.1.2.1.4194321408 = STRING:0/2/4
.1.3.6.1.2.1.4194321664 = STRING:0/2/5
.1.3.6.1.2.1.4194321920 = STRING:0/2/6
.1.3.6.1.2.1.4194322176 = STRING:0/2/7
.1.3.6.1.2.1.4194322432 = STRING:0/2/8
.1.3.6.1.2.1.4194322688 = STRING:0/2/9
.1.3.6.1.2.1.4194322944 = STRING:0/2/10
.1.3.6.1.2.1.4194323200 = STRING:0/2/11
.1.3.6.1.2.1.4194323456 = STRING:0/2/12
.1.3.6.1.2.1.4194323712 = STRING:0/2/13
.1.3.6.1.2.1.4194323968 = STRING:0/2/14
.1.3.6.1.2.1.4194324224 = STRING:0/2/15
"@ -split [environment]::NewLine

$lookupTable = ($table) |
ForEach-Object {
    [PSCustomObject]@{
        PortName = ($_ -split ' = ')[1] -replace "STRING:"
        PortNumber = ($_ -split ' = ')[0]
    }
}

$rh = Read-Host "Enter port name"

$lookupTable | Where {$_.PortName -eq $rh}

```

Output:

```auto
Enter port name: 0/2/1

PortName PortNumber
-------- ----------
0/2/1 .1.3.6.1.2.1.4194320640

```

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 7, 2021, 11:41am UTC](https://forums.powershell.org/t/get-exclusive-value/16176/14 "2021-04-07T11:41:54Z")

</div>

Hello, I don’t know why, but with `-eq` it doesn’t work. If I use `-match` or `-cmatch`, the value will be returned.

Clarifying a little more about the code: I do an SNMPWalk on a device and all the data I need is placed in a table. When I insert the slot I need, the script searches the table and returns a number, which is named in a PortNumber variable.

Again, thank you very much for your help!

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 7, 2021, 5:21pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/15 "2021-04-07T17:21:48Z")

</div>

[RESOLVED]

The problem was a space character and so the `-eq` didn’t work. After the adjustment, the script worked fine. Thank you very much for all your help!

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 24, 2022, 4:57pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/16 "2022-01-24T16:57:05Z")

</div>

Hi people, here we again :-p

Same problem, but now, with the strings…  
The Array (:

```auto
hundred-gigabit-ethernet-1/1/1
hundred-gigabit-ethernet-1/1/10
hundred-gigabit-ethernet-1/1/11
hundred-gigabit-ethernet-1/1/12
hundred-gigabit-ethernet-1/1/13
hundred-gigabit-ethernet-1/1/14

```

The Userinput

```auto
hundred-gigabit-ethernet-1/1/1

```

The code:

```auto
 $teste = for ($i=1;$i -le 1;$i++) {
    $userInput = $port
   
    $lookupTable | Where-Object { $_.PortName -match [REGEX]::escape($userInput) }

} 

```

The regex with match brings the entire array that contains Hundred-gigabit-ethernet-1/1/1, that is, the entire informed array.

The -EQ operator does not work with strings.

Does anyone have any ideas for a workaround for this?

---

<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:** [January 24, 2022, 6:19pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/17 "2022-01-24T18:19:23Z")

</div>

You’ve not provided a good example of your data so I’ve just used an array and simplified your code.

If you don’t use the `Escape()` method, you can append `$` to show that that’s the end of the string that should be matched:

```auto
$lookupTable = @(
    'hundred-gigabit-ethernet-1/1/1',
    'hundred-gigabit-ethernet-1/1/10',
    'hundred-gigabit-ethernet-1/1/11',
    'hundred-gigabit-ethernet-1/1/12',
    'hundred-gigabit-ethernet-1/1/13',
    'hundred-gigabit-ethernet-1/1/14'
)

$port = 'hundred-gigabit-ethernet-1/1/1'

$lookupTable | Where-Object { $_ -match "$($port)$" }

```

The sub-expression `$($port)` isn’t strictly necessary - `{ $_ -match "$port$" }` would work - but I think using the sub-expression makes it easier to read.

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 24, 2022, 6:27pm UTC](https://forums.powershell.org/t/get-exclusive-value/16176/18 "2022-01-24T18:27:04Z")

</div>

Hi Mr. [matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield),

I have change the code as your suggestion and appear to working. I am doing some tests and after that, I reply if works OK.

Thanks a lot

---

<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/get-exclusive-value/16176/19 "2024-05-16T20:19:41Z")

</div>


