Let’s take a look at the following example:
PS C:\Users\Hong> (ipconfig) -match 'IPv4'
IPv4 address . . . . . . . . . . . . . . . : 192.168.1.102
IPv4 address . . . . . . . . . . . . . . . : 192.168.193.1
IPv4 address . . . . . . . . . . . . . . . : 192.168.93.1
We know that ipconfig displays the TCP/IP configuration information of the current computer, one of which is the IP address. If there are multiple network cards, or if there are multiple IP addresses configured on a website, they can be clearly displayed. In order to show the function of the match operator to filter arrays, we will use this ipconfig as an example.
The result after ipconfig execution should be roughly like this:
PS C:\Users\Hong> (ipconfig)
Windows IP Configuration
Wireless LAN Adapter Local Connection* 13:
Media Status . . . . . . . . . . . . . : Media has been disconnected
Connect to specific DNS suffixes . . . . . . . . :
Wireless LAN Adapter Wi-Fi:
Connect to specific DNS suffixes . . . . . . . . :
Local link IPv6 address. . . . . . . . . : fe80::a438:a0ec:e477:94c6%12
IPv4 address . . . . . . . . . . . . . . . : 192.168.1.102
Subnet mask . . . . . . . . . . . . : 255.255.255.0
Default gateway. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
... (Other network cards are omitted here)
If we want to see the IP address configured by the machine, we only need to look at the line starting with IPv4. Then we can simply use "IPv4" as the matching keyword. So, the PowerShell command can be written:
(ipconfig) -match 'IPv4'
Why do I need to enclose ipconfig in brackets here? Because we know that ipconfig itself also has some parameters, in order not to mistakenly think that -match is also the parameter we assigned to ipconfig, we added a pair of brackets to ipconfig. Small brackets are executed first, haha, that is, telling the system that ipconfig is an independent whole.
In the example, the result after ipconfig is an array of strings. Each element of an array is each row of output, which means that as many rows of output as there are, there are as many array elements as there are. The operation of the match parameter on an array is to find the matching element from the array element and output the matching element.
This article introduces so much about PowerShell using match to filter arrays. I hope it will be helpful to you, thank you!