Saturday, November 2, 2013

Sorting a list of email recipients

I like using scripting to make aspects of business work easier. Here's an example that is useful to anyone sending emails to groups of 15 or more and wanting the recipient lists alphabetized.



What problem am I solving?
When sending emails, I prefer the recipient's names alphabetized. This can be done manually pretty quickly, but for distributions over 10 it's easier to use a simple PowerShell script.


What's PowerShell?
If you're not familiar, PowerShell is Microsoft's scripting language to replace batch files and VBScript. It has all the power of .Net (if you ever need it), but for my purposes it provides a language for accomplishing minor items like the one below.


Is this more trouble than it's worth?
It might be for you if scripting is foreign. However, this is low effort and high reward since I especially prefer names in large lists sorted.



What do I do?
"b_person@email.com; z_person@email.com; a_person@email.com;" | sort_string.ps1

From the PowerShell command line, I copy/paste the contents of an email's "To:" (need to enclose in quotes so PowerShell treats it like a string) onto the PowerShell command line and pipe the results to sort_string.ps1. The sorted emails are sent back clipboard ready for the email.


How does it work?
1. The script can accept two parameters: the list of email addresses and a delimiter.
param(
    [String]$sort_string = "",
    [string]$delimiter = ";"
)



2. It creates an array of emails, trims spaces, sorts them alphabetically, and sends the emails to the clipboard - ready to be pasted back into the email's "To:".
$sort_string.Split($delimiter) | %{$_.Trim(" ")} | sort | clip



What's the whole script look like?
~~~~~~
sort_string.ps1

param(
    [Parameter(
        Position = 0,
        ValueFromPipeline=$true,
        ValueFromPipelinebyPropertyName=$true)
    ]
    [String]$sort_string = "needs_sorting.txt",
    [string]$delimiter = ";"
)

<#
.SYNOPSIS
   Sort a delimited string
#>

$sort_string.Split($delimiter) | %{$_.Trim(" ")} | sort | clip

No comments:

Post a Comment