Uwaga
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
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.
Convert-String
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 <alice.alpha@contoso.com>;' |
Convert-String -Example 'Alice Alpha <alice.alpha@contoso.com>;=Alice,Alpha,alice.alpha@contoso.com'
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.
Alice,Alpha,alice.alpha@contoso.com
1up
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 <alice.alpha@contoso.com>;
Bob Beta <bob.beta@contoso.com>;
Charlie Charles <charlie.charles@contoso.com>;
Doug Delta <doug.delta@contoso.com>
"@ -Split "`n"
$Emails |
Convert-String -Example 'Alice Alpha <alice.alpha@contoso.com>;=Alice,Alpha,alice.alpha@contoso.com'
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.
Alice,Alpha,alice.alpha@contoso.com
Bob,Beta,bob.beta@contoso.com
Charlie,Charles,charlie.charles@contoso.com
Doug,Delta,doug.delta@contoso.com
CSV baby!
Let’s add ConvertFrom-CSV to include some headers.
$Emails = @"
Alice Alpha <alice.alpha@contoso.com>;
Bob Beta <bob.beta@contoso.com>;
Charlie Charles <charlie.charles@contoso.com>;
Doug Delta <doug.delta@contoso.com>
"@ -Split "`n"
$Emails |
Convert-String -Example 'Alice Alpha <alice.alpha@contoso.com>;=Alice,Alpha,alice.alpha@contoso.com' |
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 alice.alpha@contoso.com
Bob Beta bob.beta@contoso.com
Charlie Charles charlie.charles@contoso.com
Doug Delta doug.delta@contoso.com
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
- Anonymous
October 11, 2016
Great tip!! thanks for sharing!! - Anonymous
April 18, 2017
A must-have for a lot of parsing tools! - Anonymous
May 25, 2017
Example 1 on this page doesn't even work! https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/convert-string?f=255&MSPPError=-2147217396 - Anonymous
May 25, 2017
No spaces around equals sign, and -inputobject can't take a list:Convert-String -inputobject $names[0] -Example "Patti Fuller=Fuller, P."