Trying to extract a 6 digit numeric string from a paragraph of text using PowerShell but it works only under one scenario. The 6 digit string sits inside a paragraph of text in the Windows Clipboard. In my code I'm expecting the variable $Matches[0] to be the 6 digits I'm looking for but the result is always blank. If I uncomment line 2 then $Matches[0] will always be whatever the 6 digit code in line 2 is, meaning 123456 as shown below. But if I comment out line 2, then copy a paragraph of text from my real world example, and re-run the code, instead of $Matches[0] being the expected 6 digit string, it is always blank. I will walk through both examples and their outputs below. Not sure what I am doing wrong.
Working Example:
$Matches[0] = $null
Set-Clipboard -value "Your PIN is 123456."
$PIN = (Get-Clipboard) -match '\d{6}'
# Get-Clipboard
Write-Output $Matches[0]
The above code will output the below, as expected:
Non-working Example: If I comment out line 2:
$Matches[0] = $null
# Set-Clipboard -value "Your PIN is 123456."
$PIN = (Get-Clipboard) -match '\d{6}'
# Get-Clipboard
Write-Output $Matches[0]
and given this paragraph of text, copied into the Windows Clipboard:
Hello,
Your authentication code is 351370
This code will expire in 20 minutes to keep your account secure.
The output shows blank, instead of the expected 351370:
Thoughts?