Use the new PowerShell cmdlet Convert-String to parse email addresses

image_thumb[9]Tired of hacking away at RegEx and string functions to parse text? This post is for you!

New toys

PowerShell 5.x includes a number of new features. One of the lesser-known and incredibly powerful is the string conversion set of cmdlets. The names are very similar. Check out how Get-Help describes them:

PS C:\> Get-Help Convert*-String | Format-Table Name,Synopsis -AutoSize

Name               Synopsis
----               --------
Convert-String     Formats a string to match examples.
ConvertFrom-String Extracts and parses structured objects from string content.


I will admit that doesn’t sound very interesting on first glance. But check out what they can do! Let’s start with a simple example of Convert-String:

'Alice Alpha <>;' |            
Convert-String -Example 'Alice Alpha <>;=Alice,Alpha,'

The -Example parameter is a string set up as SampleData=DesiredFormat. The above example will parse Alice's name and email into clean CSV data. It doesn’t always have to be CSV, but that is what I need for this use case.



Now let’s take a list of email addresses pasted from Outlook. Notice that we pipe them to –Split, giving us an array for string conversion split on new lines:

$Emails = @"
Alice Alpha <>;
Bob Beta <>;
Charlie Charles <>;
Doug Delta <>
"@ -Split "`n"            
$Emails |            
Convert-String -Example 'Alice Alpha <>;=Alice,Alpha,'

This code turns a list of email addresses into nice CSV data. Because the addresses are all formatted similarly, the one example of Alice shows how to parse all the lines.


CSV baby!

Let’s add ConvertFrom-CSV to include some headers.

$Emails = @"
Alice Alpha <>;
Bob Beta <>;
Charlie Charles <>;
Doug Delta <>
"@ -Split "`n"            
$Emails |            
Convert-String -Example 'Alice Alpha <>;=Alice,Alpha,' |            
ConvertFrom-Csv -Header 'FirstName','LastName','Email'

Now we have clean PowerShell object output for a list of email addresses pasted from Outlook. Cool!

FirstName LastName Email                      
--------- -------- -----                      
Alice     Alpha    
Bob       Beta       
Charlie   Charles
Doug      Delta

Convert-String works with a single parsing example. If you have a variety of complex patterns to parse, then use ConvertFrom-String instead. We will look at that in tomorrow’s post.

Your turn…

I decided to keep this post short and leave other things for you to discover with the cmdlet. I have shown you one way to use it. There is more! Get-Help is your friend.

Now take this and go parse some of your own flat text use cases. Use the comments below to share your challenges and victories. Enjoy!

Comments (4)
  1. Great tip!! thanks for sharing!!

  2. A must-have for a lot of parsing tools!

  3. js2010 says:

    No spaces around equals sign, and -inputobject can’t take a list:

    Convert-String -inputobject $names[0] -Example “Patti Fuller=Fuller, P.”

Comments are closed.

Skip to main content