Pradeep Shanbhag
Pradeep Shanbhag

Reputation: 477

Powershell script value fetching

I wanted to fetch default gatway using powershell script and I am able to get it as below.

Get-WmiObject -Class Win32_IP4RouteTable |
    where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | 
        Sort-Object metric1 | select nexthop | select-object -first 1

The result

nexthop
-------
0.0.0.0

However I want to fetch only the value "0.0.0.0", not the header, any solution for this ?

Upvotes: 3

Views: 221

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125257

You should get property value using either of following scripts.

Using (your script).PropertyName:

(Get-WmiObject -Class Win32_IP4RouteTable |
    where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | 
        Sort-Object metric1 | select nexthop | select-object -first 1).nexthop

Or by using Using your script | select -ExpandProperty PropertyName:

Get-WmiObject -Class Win32_IP4RouteTable |
    where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | 
        Sort-Object metric1 | select nexthop | select-object -first |
            select -ExpandProperty nexthop

Upvotes: 2

Vincent K
Vincent K

Reputation: 1346

You don't have to use Select-Object cmdlet multiple time.

Get-WmiObject -Class Win32_IP4RouteTable -Filter "Destination = '0.0.0.0' AND Mask = '0.0.0.0'" |    
        Sort-Object metric1 | Select-Object -First 1 -ExpandProperty nexthop

or

(Get-WmiObject -Class Win32_IP4RouteTable -Filter "Destination = '0.0.0.0' AND Mask = '0.0.0.0'" |    
        Sort-Object metric1 | Select-Object -First 1).nexthop

Upvotes: 1

Related Questions