在 powershell 中获取过滤结果

get Filtered result in powershell

我想根据命令输出的 'some string value' 过滤结果。 例如,我需要从 ipconfig 的输出中获取 MAC 地址。 我试过了

 ipconfig /all | Select-String -Pattern "Physical Address" 

我得到了所有 MAC 个像

这样的地址
 Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA
 Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA

我只想 Mac wifi 适配器的地址。 所以输出将是 AA-AA-AA-AA-AA-AA。 我不想要整行

Physical Address. . . . . . . . . : AA-AA-AA-AA-AA-AA

您可以使用以下命令:

ipconfig /all | Select-String -Pattern "\w\w-\w\w-\w\w-\w\w-\w\w-\w\w" -AllMatches | % { $_.Matches } | % { $_.Value }

ipconfig /all | Select-String -Pattern "Physical Address" 可以进一步扩展为通过“:”分隔符拆分输出并显示第二个字段。示例如下。

PS C:\Users\jump> ipconfig /all | Select-String -SimpleMatch Physical
 -AllMatches | ForEach-Object { $_.ToString().split(":")[1]}
 B8-AC-6F-28-08-C5

这适用于我的系统:

$Wireless_Adapter_Regex  =
@'
(?ms).+?Wireless LAN adapter .+?
   Physical Address. . . . . . . . . : (\S+)
'@

(ipconfig /all | out-string) -match $Wireless_Adapter_Regex > $null
$matches[1]

C4-D9-87-41-37-F1