You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在纯净PowerShell 5环境中快速获取网络配置概览

如何在纯净PowerShell 5环境中快速获取网络配置概览

我太懂这种纠结了——之前在Linux上靠ifconfig和ip route就能轻松拿到大部分需要的网络信息,刚接触PowerShell 5的时候,总觉得网络配置信息要么散在好几个命令里,要么默认输出格式根本没法快速看明白。而且有时候在别人的机器上操作,既没有自己写的自定义函数,还连不上网没法装模块,确实头疼。

针对这种情况,你可以直接用PowerShell内置的命令封装两个轻量的自定义函数,不需要任何额外安装,复制到终端里执行就能用:

1. 替代ifconfig的自定义命令

这个函数整合了Get-NetIPConfiguration的信息,把分散的接口、IP、MAC、链路状态等关键数据整理成清晰的表格:

function ifconfigAlias {
    Get-NetIPConfiguration -Detailed | Select-Object `
        @{ name = "Idx"; expr = { $_.NetAdapter.InterfaceIndex } },
        InterfaceAlias,
        @{ name = "Address"; expr = {$_.IPv4Address}; },
        @{ name = "MAC"; expr = { $_.NetAdapter.LinkLayerAddress } },
        @{ name = "Link"; expr = { $_.NetAdapter.Status -replace "Disconnected", "Down" } },
        @{ name = "Speed"; expr = { $_.NetAdapter.LinkSpeed } },
        @{ name = "Forwarding"; expr = { $_.NetIPv4Interface.Forwarding } },
        InterfaceDescription `
    | Sort-Object InterfaceAlias | Format-Table -Wrap -AutoSize
}
Set-Alias -Name my-ipconfig -Value ifconfigAlias

执行完之后,直接输入my-ipconfig就能看到类似ifconfig的简洁输出了。

2. 替代ip route的自定义命令

这个函数过滤了不必要的路由条目(比如广播地址、多播地址),还支持按接口或目标地址排序,输出更贴近我们习惯的路由表格式:

function routePrintAlias {
    param(
        [Parameter(HelpMessage="Sort order")]
        [ValidateSet("iface", "dest")]
        [string]$SortBy = "dest"
    )

    $Lut = @{"iface" = "InterfaceIndex"; "dest" = "DestinationPrefix"}
    $SortProp = $Lut[$SortBy]

    Get-NetRoute | `
        Sort-Object $SortProp | `
        Where-Object { `
            ($_.AddressFamily -eq 'ipv4') -and `
            ($_.DestinationPrefix -ne '255.255.255.255/32') -and `
            ($_.DestinationPrefix -ne '224.0.0.0/4') } | `
        Select-Object `
            DestinationPrefix, `
            NextHop, `
            @{name = "Idx"; expr = { $_.InterfaceIndex } }, `
            InterfaceAlias, `
            RouteMetric | `
        Format-Table
}
Set-Alias -Name my-route -Value routePrintAlias

使用的时候,默认按目标地址排序,输入my-route即可;如果想按接口排序,输入my-route -SortBy iface就行。

临时使用小技巧

如果只是在陌生机器上临时用,直接把上述代码复制到PowerShell窗口里执行一遍,就能立刻用my-ipconfig和my-route命令了。要是想长期用,可以把这些函数添加到你的PowerShell配置文件(通过$PROFILE查看路径)里,每次启动PowerShell都会自动加载。

备注:内容来源于stack exchange,提问作者jackhab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 15:12:57