I've written a script to invoke an external command and capture a part of its output using a regular expression. Here is an example (not real-world) script to demonstrate how I'm doing this:
$output = clip
if ($output -imatch 'usage')
{
Write-Output $matches[0]
}
If I run clip
at the command prompt, its output looks like this:
INFO: Type "CLIP /?" for usage.
In the above code, the match is successful, but $matches
isn't set. If I set $output
using a different method such as the following, the problem goes away:
$output = 'INFO: Type "CLIP /?" for usage.'
Why is $matches
not set when I invoke the command? How can I fix this?