{"id":75742,"date":"2015-12-24T00:01:00","date_gmt":"2015-12-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/12\/24\/a-holiday-special-rusty-the-red-eyed-scripter-part-4\/"},"modified":"2019-02-18T09:20:33","modified_gmt":"2019-02-18T16:20:33","slug":"a-holiday-special-rusty-the-red-eyed-scripter-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-holiday-special-rusty-the-red-eyed-scripter-part-4\/","title":{"rendered":"A Holiday Special: Rusty the Red-Eyed Scripter, Part 4"},"content":{"rendered":"<p><b>Summary<\/b>: Rusty the Red-Eyed Scripter learns how PowerShell can convert raw string data to an object.<\/p>\n<p><b>&nbsp; &nbsp;Note<\/b>&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-holiday-special-rusty-the-red-eyed-scripter-part-1\/\" target=\"_blank\">Rusty the Red-Eyed Scripter, Part 1<\/a><br \/>Rusty discovers compressing files and working with the Clipboard&nbsp;<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-holiday-special-rusty-the-red-eyed-scripter-part-2\/\" target=\"_blank\">Rusty the Red-Eyed Scripter, Part 2<\/a><br \/>Rusty discovers how Windows PowerShell 5.0 can pull modules from a central repository<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-holiday-special-rusty-the-red-eyed-scripter-part-3\/\" target=\"_blank\">Rusty the Red-Eyed Scripter, Part 3<\/a><br \/>Rusty learns how to unblock files and tweak event logs&nbsp;<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-holiday-special-rusty-the-red-eyed-scripter-part-4\/\" target=\"_blank\">Rusty the Red-Eyed Scripter, Part 4<\/a><br \/>Rusty learns how PowerShell can convert raw string data to an object<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-holiday-special-rusty-the-red-eyed-scripter-part-5\/\" target=\"_blank\">Rusty the Red-Eyed Scripter, Part 5<\/a><br \/>Rusty discovers the power of classes and enums&nbsp;<\/li>\n<\/ul>\n<p>Yesterday, our good friend Rusty found his head spinning with ideas after getting shown some new PowerShell cmdlets&mdash;cmdlets he could immediately put to work when using Windows PowerShell 5.0.<\/p>\n<p>He decided to move to the front row with Thomas to get a better view. There was some powerful stuff coming on the screen, and he didn&rsquo;t want to miss a bit. &ldquo;And now we have a special treat for you folks,&rdquo; the speaker started, &ldquo;Here to introduce one of the coolest features in Windows PowerShell 5&mdash;your friend and mine, Ed &lsquo;Hey, Scripting Guy&rsquo; Wilson!&rdquo;<\/p>\n<p>Rusty almost fell out of his chair. He&rsquo;d heard of the mysterious Microsoft Scripting Guy, but he thought it was just some imaginary creature. Was there actually a real Scripting Guy?<\/p>\n<p>&ldquo;Hey everybody!&rdquo; his voice boomed out with a smooth southern accent. &ldquo;Welcome to PowerShell Saturday where I&rsquo;m going to show you possibly some of the coolest features yet!&rdquo;<\/p>\n<p>Boy was Rusty excited. Here was the actual Scripting Guy from Microsoft about to introduce some PowerShell 5 bits. Was he glad he moved to the front row!<\/p>\n<p>&ldquo;Now!&rdquo; Ed clapped his hands together, &ldquo;There are some cmdlets you might have overlooked on the way to Windows PowerShell 5. How would you like an easy way to create a temporary file?&rdquo;<\/p>\n<p>Rusty didn&rsquo;t get that one. You make a folder, put your file in there and&#8230;<\/p>\n<p>As if reading his thoughts, the Scripting Guy continued, &ldquo;<b>New-TemporaryFile<\/b> produces a blank file in the user&rsquo;s temp folder with a proper .tmp extension.&rdquo;<\/p>\n<p>Ed demonstrated the use this cmdlet, which was a simple line that produced the following output:<\/p>\n<p style=\"margin-left:30px\">New-TemporaryFile<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/3.PNG\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/3.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>&ldquo;This cmdlet,&rdquo; he continued, &ldquo;not only creates the blank file in a proper temporary location, it also returns the path of the file for you to consume with your script or module.&rdquo;<\/p>\n<p>Rusty sat upright. &ldquo;Such a cool and simple idea!&rdquo; he burst out loud.<\/p>\n<p>&ldquo;Exactly! That&rsquo;s the same thing I thought!&rdquo; responded Ed. &ldquo;Now if you thought that was cool, hold on to your seats for this one. So who here loves parsing log files manually?&rdquo;<\/p>\n<p>Strangely, not a single hand went up. Well one&hellip;it appears that Sean was being a bit of troublemaker.<\/p>\n<p>&ldquo;OK,&rdquo; he smiled at Sean, &ldquo;for those of us who don&rsquo;t live on coffee&hellip;&rdquo;<\/p>\n<p>&ldquo;Log files and large text files can be a royal pain. Especially files that aren&rsquo;t in nice neat columns. This brings us to <b>ConvertFrom-String<\/b>, which is like magic!&rdquo;<\/p>\n<p>Eyebrows began to raise about the room. Thomas nudged Rusty, &ldquo;You&rsquo;re going to love this!&rdquo;<\/p>\n<p>&ldquo;Here we have a simple text file that contains names and addresses. I could probably write something with regular expressions to parse it, but I&rsquo;d rather be home relaxing with the Scripting Wife.&rdquo;<\/p>\n<p style=\"margin-left:30px\">Joe B Smith<\/p>\n<p style=\"margin-left:30px\">906 110th Avenue<\/p>\n<p style=\"margin-left:30px\">Seattle, WA<\/p>\n<p style=\"margin-left:30px\">253-555-5938<\/p>\n<p style=\"margin-left:30px\">Peter O Jones<\/p>\n<p style=\"margin-left:30px\">545 Microsoft Way<\/p>\n<p style=\"margin-left:30px\">Seattle, WA<\/p>\n<p style=\"margin-left:30px\">425-555-2481<\/p>\n<p style=\"margin-left:30px\">Mary J Beeblebrox<\/p>\n<p style=\"margin-left:30px\">585 110th Avenue<\/p>\n<p style=\"margin-left:30px\">Seattle, WA<\/p>\n<p style=\"margin-left:30px\">425-555-1149<\/p>\n<p>&ldquo;As you can see, there is a pattern. But we have nothing to say, &ldquo;Here&rsquo;s the name, here&rsquo;s the address, and so on. With <b>ConvertFrom-String<\/b>, I can convert this string array into an actual object including specifying types of data.&rdquo;<\/p>\n<p>Ed pulled up a small file called DBTemplate.txt. &ldquo;In this file, I have added some parameters around actual data from our file. There&rsquo;s a new engine feature in PowerShell that allows it to intelligently analyze the data and look for the same pattern.&rdquo;<\/p>\n<p style=\"margin-left:30px\">{Userinfo*:{First:Louise} {Middle:X} {Last:Vader}<\/p>\n<p style=\"margin-left:30px\">{Street:1 Microsoft Way}<\/p>\n<p style=\"margin-left:30px\">{City:Redmond}, {State:WA}<\/p>\n<p style=\"margin-left:30px\">{Phone:360-555-4467}}<\/p>\n<p style=\"margin-left:30px\">Joe B Smith<\/p>\n<p style=\"margin-left:30px\">906 110th Avenue<\/p>\n<p style=\"margin-left:30px\">Seattle, WA<\/p>\n<p style=\"margin-left:30px\">253-555-5938<\/p>\n<p style=\"margin-left:30px\">Peter O Claus<\/p>\n<p style=\"margin-left:30px\">545 Microsoft Way<\/p>\n<p style=\"margin-left:30px\">Seattle, WA<\/p>\n<p style=\"margin-left:30px\">425-555-2481<\/p>\n<p>&ldquo;The first tag you&rsquo;ll see is called <b>Userinfo*:<\/b>. This declares that this entire set of data is an object called <b>Userinfo<\/b>.&rdquo; Ed pointed to the section on the screen.<\/p>\n<p>&ldquo;Here, where it says first, middle, and last,&rdquo; he continued, &ldquo;the engine is told to look at this pattern on the screen and try to find something similar. This could be a line with a bunch of letters, a space after, more squiggly letters, a space, more letters, and a carriage return at the end.&rdquo;<\/p>\n<p style=\"margin-left:30px\">{First:Louise} {Middle:X} {Last:Vader}<\/p>\n<p>&ldquo;Now to help the engine along, we give it more information such as defining as much of the chunks of data as we can. This way if it sees a number, a space, more letters, and things such as that, it might start to learn, &lsquo;Hey, that looks like an address, dude!&rdquo;<\/p>\n<p style=\"margin-left:30px\">{Street:1 Microsoft Way}<\/p>\n<p>&ldquo;The rest of the information is to give the engine something to practice to see if it can learn the patterns. Real data from the actual file works best.&rdquo; Ed paused dramatically before continuing.<\/p>\n<p>&ldquo;Now we can use <b>ConvertFrom-String<\/b> to save this file as dbtemplate.txt or whatever name you desire, and feed in the original file using this template.&rdquo;<\/p>\n<p style=\"margin-left:30px\">Get-Content C:\\Foo\\DB.txt | ConvertFrom-String &ndash;template dbtemplate.txt<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6036.4.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6036.4.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>&ldquo;This is now an object that I can parse in PowerShell like this:&rdquo;<\/p>\n<p style=\"margin-left:30px\">$Info=Get-Content C:\\Foo\\DB.txt | ConvertFrom-String &ndash;template dbtemplate.txt<\/p>\n<p style=\"margin-left:30px\">$Info.UserInfo | Get-Member<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6232.5.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6232.5.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>&ldquo;TaDa! It&rsquo;s now all one giant PowerShell custom object!&rdquo;<\/p>\n<p>Ooooohs and aaahhhhssss could be heard across the room. Rusty piped up, &ldquo;And could I somehow get it to define that an object is an integer?&rdquo;<\/p>\n<p>&ldquo;Oh yes sir, you can! Here&rsquo;s that same dbtemplate.txt file with the <b>[string]<\/b> types added in. It presumes a string, but you can use almost any built-in PowerShell type&mdash;integer, date, time, whatever!&rdquo;<\/p>\n<p style=\"margin-left:30px\">{Userinfo*:{[STRING]First:Louise} {[STRING]Middle:X} {[STRING]Last:Vader}<\/p>\n<p style=\"margin-left:30px\">{[STRING]Street:1 Microsoft Way}<\/p>\n<p style=\"margin-left:30px\">{[STRING]City:Redmond}, {[STRING]State:WA}<\/p>\n<p style=\"margin-left:30px\">{[STRING]Phone:360-555-4467}}<\/p>\n<p>Rusty&rsquo;s mind was churning with ideas. &ldquo;Is there some way to tell PowerShell that when I see a field such as the phone number, that it is actually a phone number?&rdquo;<\/p>\n<p>&ldquo;That there is,&rdquo; smiled Ed, &ldquo;and our class about classes begins soon.&rdquo;<\/p>\n<p>Join us tomorrow when Ed and his friends show Rusty one of the coolest changes in Windows PowerShell&nbsp;5.0.<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney, <\/b>Honorary Scripting Guy, Cloud and Datacenter Management MVP&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Rusty the Red-Eyed Scripter learns how PowerShell can convert raw string data to an object. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts: Rusty the Red-Eyed Scripter, Part 1Rusty discovers compressing files and working with the Clipboard&nbsp; Rusty the Red-Eyed Scripter, Part 2Rusty discovers how Windows PowerShell 5.0 can pull modules [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[642,56,154,45],"class_list":["post-75742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-2015-holiday-series","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Rusty the Red-Eyed Scripter learns how PowerShell can convert raw string data to an object. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts: Rusty the Red-Eyed Scripter, Part 1Rusty discovers compressing files and working with the Clipboard&nbsp; Rusty the Red-Eyed Scripter, Part 2Rusty discovers how Windows PowerShell 5.0 can pull modules [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=75742"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75742\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=75742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}