module/src/FindMailCmdlet.cs

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Management.Automation;
 
[Cmdlet("Find", "Mail")]
public class FindMailCmdlet : PSCmdlet {
 
    [Parameter(Mandatory = true)]
    public string Path;
    Regex regex = new Regex("^To: (.*)$");
    Regex innerRegex = new Regex("<(.*?)>$");
 
    protected override void BeginProcessing() {
        var files = System.IO.Directory.EnumerateFiles(Path);
 
 
        foreach (var file in files) {
            var result = ReadFile(file);
 
            if (null != result)
                WriteObject(PSObject.AsPSObject(result));
        }
    }
 
    private class FileResult {
        public string Path;
        public string Line;
        public string Match;
    }
 
    private FileResult ReadFile(string path) {
        using (var reader = new StreamReader(path, Encoding.UTF8, true, 1024)) {
            string line;
 
            while ((line = reader.ReadLine()) != null) {
                var match = regex.Match(line);
 
                if (match.Success) {
                    var group = match.Groups[1].Value;
                    return new FileResult() {
                        Path = path,
                        Line = match.Groups[0].Value,
                        Match = InnerMatch(match.Groups[1].Value)
                    };
                }
            }
        }
 
        return null;
    }
 
    private string InnerMatch(string line) {
        var match = innerRegex.Match(line);
        if (match.Success) {
            return match.Groups[1].Value;
        } else {
            return line;
        }
    }
}