{"id":9261,"date":"2006-11-22T23:31:00","date_gmt":"2006-11-22T23:31:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/11\/22\/processing-eml-files-with-select-string-and-setcreationtime\/"},"modified":"2019-02-18T13:21:08","modified_gmt":"2019-02-18T20:21:08","slug":"processing-eml-files-with-select-string-and-setcreationtime","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/processing-eml-files-with-select-string-and-setcreationtime\/","title":{"rendered":"Processing .EML files with Select-String and SetCreationTime()"},"content":{"rendered":"<p>In our newsgroup Microsoft.Public.Windows.PowerShell, Steve Foster asked&nbsp;whether PowerShell could help him&nbsp;with a bunch of emails in .EML format.&nbsp; Here was his query:<\/p>\n<blockquote>\n<p>Got a bunch of emails (.EML) that have been copied around various places <br \/>with the result that their file timestamps bear no resemblance to the <br \/>dates in the emails.<\/p>\n<p>It would be really helpful to extract the date from the headers in the <br \/>emails, and update the timestamps on the files accordingly.<\/p>\n<p>It strikes me that this would be the sort of thing PowerShell ought to be <br \/>really good at &#8211; but could it do the job? And if so, how?<\/p>\n<\/blockquote>\n<p>Remember that old TV game show where the participants would guess how many notes it took to Name That Tune?&nbsp; Well:<\/p>\n<blockquote>\n<p><em><strong>I can code that script in 2 lines!<\/strong><\/em><\/p>\n<\/blockquote>\n<p>foreach ($record in Select-String ^Date: *.eml) {<br \/>&nbsp; [System.IO.File]::SetCreationTime($Record.Path, [datetime]($record.line.substring(6)))<br \/>}<\/p>\n<p>Ok so I didn&#8217;t count the trailing &#8220;}&#8221; as a line &#8211; sue me!&nbsp; \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>There are 2 lessons to draw here:<\/p>\n<p>1) <strong>Select-String is NOT grep.&nbsp;<\/strong> Here is an example to drive home that point:<\/p>\n<blockquote>\n<p>PS&gt; Select-String ^Date: *.eml |Get-Member -MemberType Property<\/p>\n<p>&nbsp;&nbsp; TypeName: Microsoft.PowerShell.Commands.MatchInfo<\/p>\n<p>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>Filename&nbsp;&nbsp; Property&nbsp;&nbsp; System.String Filename {get;}<br \/>IgnoreCase Property&nbsp;&nbsp; System.Boolean IgnoreCase {get;set;}<br \/>Line&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String Line {get;set;}<br \/>LineNumber Property&nbsp;&nbsp; System.Int32 LineNumber {get;set;}<br \/>Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String Path {get;set;}<br \/>Pattern&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String Pattern {get;set;}<\/p>\n<p>PS&gt; Select-String ^Date: *.eml |format-list *<\/p>\n<p>IgnoreCase : True<br \/>LineNumber : 4<br \/>Line&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Date: Tue, 21 Nov 2006 09:30:19 -0800<br \/>Filename&nbsp;&nbsp; : t1.eml<br \/>Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : D:\\Temp\\t1.eml<br \/>Pattern&nbsp;&nbsp;&nbsp; : ^Date:<\/p>\n<p>IgnoreCase : True<br \/>LineNumber : 4<br \/>Line&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Date: Sat, 09 July 2005 09:30:19 -0800<br \/>Filename&nbsp;&nbsp; : t3.eml<br \/>Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : D:\\Temp\\t3.eml<br \/>Pattern&nbsp;&nbsp;&nbsp; : ^Date:<\/p>\n<\/blockquote>\n<p>2) .<strong>NET ROCKS<\/strong>.&nbsp; Ok so we didn&#8217;t get around to shipping a cmdlet to change the times on a filename.&nbsp; .NET provides this so the fact that we can access .NET means that you are not stuck waiting for us.&nbsp; In this example we are using a static method on PATH.&nbsp; Here is how you find out what other statics are available on that class:<\/p>\n<blockquote>\n<p>PS&gt; [System.IO.File] |Get-Member -Static<\/p>\n<p>&nbsp;&nbsp; TypeName: System.IO.File<\/p>\n<p>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>AppendAllText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void AppendAllText(String ..<br \/>AppendText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.StreamWriter AppendText..<br \/>Copy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Copy(String sourceFil..<br \/>Create&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.FileStream Create(Strin..<br \/>CreateText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.StreamWriter CreateText..<br \/>Decrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Decrypt(String path)<br \/>Delete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Delete(String path)<br \/>Encrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Encrypt(String path)<br \/>Equals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Boolean Equals(Object objA..<br \/>Exists&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Boolean Exists(String path)<br \/>GetAccessControl&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Security.AccessControl.Fil..<br \/>GetAttributes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.FileAttributes GetAttri..<br \/>GetCreationTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.DateTime GetCreationTime(S..<br \/>GetCreationTimeUtc&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.DateTime GetCreationTimeUt..<br \/>GetLastAccessTime&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.DateTime GetLastAccessTime..<br \/>GetLastAccessTimeUtc Method&nbsp;&nbsp;&nbsp;&nbsp; static System.DateTime GetLastAccessTime..<br \/>GetLastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.DateTime GetLastWriteTime(..<br \/>GetLastWriteTimeUtc&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.DateTime GetLastWriteTimeU..<br \/>Move&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Move(String sourceFil..<br \/>Open&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.FileStream Open(String ..<br \/>OpenRead&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.FileStream OpenRead(Str..<br \/>OpenText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.StreamReader OpenText(S..<br \/>OpenWrite&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.IO.FileStream OpenWrite(St..<br \/>ReadAllBytes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Byte[] ReadAllBytes(String..<br \/>ReadAllLines&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.String[] ReadAllLines(Stri..<br \/>ReadAllText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.String ReadAllText(String ..<br \/>ReferenceEquals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Boolean ReferenceEquals(Ob..<br \/>Replace&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void Replace(String source..<br \/>SetAccessControl&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetAccessControl(Stri..<br \/>SetAttributes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetAttributes(String ..<br \/>SetCreationTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetCreationTime(Strin..<br \/>SetCreationTimeUtc&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetCreationTimeUtc(St..<br \/>SetLastAccessTime&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetLastAccessTime(Str..<br \/>SetLastAccessTimeUtc Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetLastAccessTimeUtc(..<br \/>SetLastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetLastWriteTime(Stri..<br \/>SetLastWriteTimeUtc&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void SetLastWriteTimeUtc(S..<br \/>WriteAllBytes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void WriteAllBytes(String ..<br \/>WriteAllLines&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void WriteAllLines(String ..<br \/>WriteAllText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static System.Void WriteAllText(String p..<\/p>\n<\/blockquote>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/MMC Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our newsgroup Microsoft.Public.Windows.PowerShell, Steve Foster asked&nbsp;whether PowerShell could help him&nbsp;with a bunch of emails in .EML format.&nbsp; Here was his query: Got a bunch of emails (.EML) that have been copied around various places with the result that their file timestamps bear no resemblance to the dates in the emails. It would be really [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>In our newsgroup Microsoft.Public.Windows.PowerShell, Steve Foster asked&nbsp;whether PowerShell could help him&nbsp;with a bunch of emails in .EML format.&nbsp; Here was his query: Got a bunch of emails (.EML) that have been copied around various places with the result that their file timestamps bear no resemblance to the dates in the emails. It would be really [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=9261"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9261\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=9261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}