Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script value fetching

Tags:

powershell

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 ?

like image 625
Pradeep Shanbhag Avatar asked Apr 25 '26 19:04

Pradeep Shanbhag


1 Answers

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
like image 135
Reza Aghaei Avatar answered Apr 28 '26 14:04

Reza Aghaei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!