1

So far I have this:

netsh wlan show profiles | Select-String '^    All User Profile     : (.*)' | ForEach-Object {
  $array +=
  $_.Matches[0].Groups[1].Value
}
$array[0]
$array[1]
$array[2]
$array[3]
$array[4]
$array[5]
$array[6]
$array[7]
$array[8]
pause

I want to be able to select the string after All User Profile : and put it into an array, but it is only selecting a single letter. How do I select the strings instead? I want each array to be a different string, and there doesn't have to be 8 there can be more or less.

jangles
  • 303
  • 1
  • 6
  • 22

2 Answers2

2

Split the selected string at ": ". (Note the space.) Then you get the profile name as the value of an array element.

$array = @()
netsh wlan show profiles | Select-String '^    All User Profile     : (.*)' | `
ForEach-Object `
-Process {
  $profile = ($_ -split ": ")[1]
  $array += $profile
} `
-End {$array}

Here's one way to think about how to extract the profile.

# A full string from netsh wlan show profiles
"    All User Profile     : WibbleCoffeeWiFi"

# Split it, and return the first element. There are leading and trailing spaces.
("    All User Profile     : WibbleCoffeeWiFi" -split ': ')[0] #     All User Profile     

# Split it, and return the second element.
("    All User Profile     : WibbleCoffeeWiFi" -split ': ')[1] #WibbleCoffeeWiFi

# Split it, and return the last element. Same as the second element in this case.
("    All User Profile     : WibbleCoffeeWiFi" -split ': ')[-1] #WibbleCoffeeWiFi
Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
  • Can I ask what the "^" is for at the beginning of the string? as well as (.*), I know the * is a wildcard but i'm not understanding what it is for. Thanks! – cet51 Jul 21 '17 at 19:33
  • I would like to know as well. Is it an indent? – jangles Jul 21 '17 at 19:35
  • And also what are the independent variable names? $Profile[1] and $Profile[2] don't seem to work, neither does $array[1] or $array[2]. – jangles Jul 21 '17 at 19:40
  • The string is a regular expression. The `^` character is an anchor; it represents the beginning of the string. (`$` anchors the end of a string.) The regular expression `'.*All User Profile.*'` would work here, too. I just didn't want to alter the OP's code too much. [Possibly misleading docs](https://learn.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions) – Mike Sherrill 'Cat Recall' Jul 21 '17 at 19:45
  • The expression with the -split` parameter returns an array having two elements. `(...)[0]` is the first element; OP isn't interested in that. `(...)[1]` contains a wlan profile name. – Mike Sherrill 'Cat Recall' Jul 21 '17 at 19:49
  • I think I understand the `[1]` part, but I still need all of the lines selected to be unique variables. So right now with this code, if I type `$profile` it will show the correct variable. However, there is another string that I would like to use, preferably named `$profile[1]` or something similar. – jangles Jul 21 '17 at 20:01
  • @jangles I really don't think you need different variables, you just need to learn how to index into an array. If `$profile` is an array of strings then `$profile[0]` is the first string, `$profile[1]` is the second string. Same variable, different indexes. – TheMadTechnician Jul 21 '17 at 20:13
  • If I use `$profile[0]` it comes up as what `$profile` equals, and then it just adds the `[0]` to it. I tried it with quotes and without quotes around it and it doesn't work either way. I know for a fact the array holds at least two though, because if I use `$array` it combines both strings together. – jangles Jul 21 '17 at 20:17
  • Sounds like you are trying to use it in a string. Try `$($profile[0])` instead. – TheMadTechnician Jul 21 '17 at 20:20
  • Doing that selects the first letter, `$($profile[1])` selects the second letter. – jangles Jul 21 '17 at 20:35
1

You are right to use the $matches variable.

$array = netsh wlan show profiles |
    ForEach-Object {
        if ($_ -match "\s*All User Profile\s*:\s*(.*)") { $($matches[1]) }
    }
$array

foreach ($wn in $array) {
    netsh wlan show profile name=$wn
}
lit
  • 14,456
  • 10
  • 65
  • 119
  • When I "echo" the profiles back with `$array[0]` and `$array[1]` it works, but when I use `Netsh WLAN show profile name=$array[0`] it combines the two profiles and just adds the `[0]` to the end of the string. I CAN however, set each part of the array to a separate variable, such as `$Profile1 = $array[0]` and `$Profile2 = $array[1]`, but I would like to not have to do this, as it would mean that there would be no expandability, for lack of a better word, where as it wouldn't matter the number of profiles. – jangles Jul 21 '17 at 22:12
  • Perfect! Works great. Now I may be just greedy now, but do you think it would be possible to have another `ForEach-Object` that runs `netsh WLAN show profile name=$($array[1])` for each object in the array? – jangles Jul 21 '17 at 22:30
  • Thank you so much! I will mark you as the answer. What is $wn? – jangles Jul 21 '17 at 23:10
  • $wn is the variable that contains each $array element in the foreach loop. I guess it is a poor name for "wireless network." – lit Jul 21 '17 at 23:13