0

Please explain how I can check the output of the value DynamicPortRangeStartPort in the get-nettcpsetting cmdlet.

The output looks like this:

get-nettcpsetting | select DynamicPortRangeStartPort

DynamicPortRangeStartPort
-------------------------
                         
1024                     
1024                     
1024                     
1024                     
1024  

  

The cmdlet returns an array of settings, and I just want to check if DynamicPortRangeStartPort is the value 1024 on any of the returned items.

I'm not sure what I'm missing.

I've tried:

if ((get-nettcpsetting | Select DynamicPortRangeStartPort)[1] -eq 1024) { write-host "Yes" }
(get-nettcpsetting | select DynamicPortRangeStartPort) -contains "1024"
(get-nettcpsetting | select DynamicPortRangeStartPort) -in "1024"
(get-nettcpsetting | select DynamicPortRangeStartPort).Contains(1024)
(get-nettcpsetting | select DynamicPortRangeStartPort).Contains("1024")

Forgive my ignorance...

Appleoddity
  • 647
  • 1
  • 6
  • 21
  • 2
    `Where-Object` is your friend :) `if(Get-NetTCPSetting |Where DynamicPortRangeStartPort -eq 1024){ ... }` – Mathias R. Jessen Mar 03 '22 at 19:45
  • In short: [`Select-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) by default returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty ` instead - see the [linked answer](https://stackoverflow.com/a/48809321/45375) for details and alternatives. – mklement0 Mar 04 '22 at 03:47

1 Answers1

2

I discovered that Select DynamicPortRangeStartPort returns an array of objects with exactly one property (DynamicPortRangeStartPort).

Instead, I can use Select -ExpandProperty DynamicPortRangeStartPort to generate a stream of values, rather than a stream of objects with a property.

if ((get-nettcpsetting | Select -Expandproperty DynamicPortRangeStartPort) -contains (1024)) { write-host "Yes" }

As mentioned in a comment, Where-Object also works:

if(Get-NetTCPSetting | Where DynamicPortRangeStartPort -eq 1024){ Write-Host "Yes" }
Appleoddity
  • 647
  • 1
  • 6
  • 21