1

I'm using powershell to locate an email address in a htm file and write it out to a text file. I'm using select-string which finds the string OK, but writes the line number as well as the email address to the file.

All I want is the email address! It seems simple enough, but I can't crack it.

Here's my code:

$List_htm = Get-ChildItem -Filter *.htm

# Loop:
foreach ($htm in $List_htm)
{
    # Locate recipient email address to send to:
    # Regex pattern to match:
    $pattern = '(^\W*.*@.*\.{1,}\w*$)'

    $sel = select-string -list $htm -pattern $pattern | Select-Object Line
    If ($Sel -eq $null)
{
    write-host "FAILS - $htm does NOT contain $pattern"
}
Else 
{
    write-host "WORKS! $pattern `n$sel"
} 

Write-host "end"

    $EmailAddressee = $PDFFolder + "EmailAddressee.txt"
    $sel | Out-File $EmailAddressee
}

However emailaddressee.txt looks like this:

Line                                                                                                                                                                    
----                                                                                                                                                                    
                [email protected]      

All I want is a single line with the email address in:

[email protected]      

I could obviously further process this results file in powershell to get this, but I'm hoping someone can come up with a simple one stage result.

Thanks

Ian

1 Answer 1

5

Change the following line:

$sel = select-string -list $htm -pattern $pattern | Select-Object Line

To:

$sel = select-string -list $htm -pattern $pattern | Select-Object -ExpandProperty Line

That will ensure you write the property of the object rather than the textual representation of the object itself

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! Thanks arco444. Works a treat. I've also added $sel = $sel.trim() to remove the leading spaces and just leave the email address. I don't have enough reputation points yet to upvote you, but thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.