A cmdlet that creates a file returns a block of text, and in that is the name of the file. The file has a format of string[number].ps1, but the number is random, so I would like to extract that number and store it in a variable.
Block of text for reference:
+ ~~~~~\nA 'using' statement must appear before any other statements in\
\ a script.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
1.1.3\\Downloads\\script35.ps1:20 char:8\n+ using Microsoft.Build.Framework;\n\
+ ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
1.1.3\\Downloads\\script35.ps1:20 char:3\n+ using Microsoft.Build.Framework;\n\
+ ~~~~~\nA 'using' statement must appear before any other statements in\
\ a script.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
1.1.3\\Downloads\\script35.ps1:21 char:8\n+ using Microsoft.Build.Utilities;\n\
+ ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
1.1.3\\Downloads\\script35.ps1:21 char:3\n+ using Microsoft.Build.Utilities;\n\
+ ~~~~~\nA 'using' statement must appear before any other statements in\
I thought $result -match '\d\d+(?=\.\w+)'
would work, but it only returned the lines the number was found on, instead of just the number.
With a collection (as opposed to a single value) as the LHS, the
-match
operator:acts as a filter - i.e. returns the subarray of matching elements rather than a Boolean
does not populate the automatic
$Matches
variable with the matching operator's results.That's why your output comprised the matching lines as a whole.
To return only the matching parts, you can use a
switch
statement with the-Regex
option to loop over your array of lines, in which case$Matches
is populated for each input line:The above yields:
Note that
switch
also supports reading lines directly from a file, using the-File
option(
switch -Regex -File $filePath { ... }
).