This article describes using the match operator in PowerShell to filter out the desired content from the array with regular expressions.
Let's look at the following example:
Copy Code code as follows:
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 is displaying the TCP/IP configuration information for the current computer, one of which is an IP address. If you have more than one network card, or a Web site configured with multiple IP addresses, can be clearly displayed. To show the match operator's ability to filter arrays, let's take this ipconfig for example.
The results of ipconfig execution should be roughly like this:
Copy Code code as follows:
PS c:\users\hong> (ipconfig)
Windows IP Configuration
Wireless LAN Adapter Local Area Connection * 13:
Media state ............: Media is disconnected
Connection-specific DNS suffix ...:
Wireless LAN Adapter Wi-Fi:
Connection-specific DNS suffix ...:
Local Link IPV6 address ... : fe80::a438:a0ec:e477:94c6%12
IPV4 Address ............:192.168.1.102
Subnet Mask ............:255.255.255.0
Default gateway ............. : 192.168.1.1
...... (Other network adapters are omitted here)
We want to see the IP address of the native configuration, so just look at the line that starts with IPV4. Then we can simply use "IPv4" as a matching keyword. So the PowerShell command can be written:
Copy Code code as follows:
(ipconfig)-match ' IPv4 '
Why do you enclose the ipconfig in parentheses here? Because we know that the ipconfig itself has some parameters, in order not to let the system mistakenly think that-match is also the parameter we assign to ipconfig, we add a pair of parentheses to Ipconfig. Parentheses give priority to the implementation, hehe, is to tell the system, Ipconfig is independent of the whole.
In the example, the result of ipconfig execution is an array of strings. Each element of an array is the output of each row, that is, how many rows of output, there is a number of array elements. The match parameter is an array of actions that find matching elements from the elements of the element and then output the matching elements.
About PowerShell use match to filter the array, this article introduces so much, hope to help you, thank you!