{"id":4000,"date":"2014-09-08T09:33:14","date_gmt":"2014-09-08T13:33:14","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=4000"},"modified":"2014-09-08T09:33:14","modified_gmt":"2014-09-08T13:33:14","slug":"using-optimized-text-files-in-powershell","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/","title":{"rendered":"Using Optimized Text Files in PowerShell"},"content":{"rendered":"<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-4001\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png\" alt=\"document\" width=\"144\" height=\"144\" \/><\/a>If you are like many IT Pros that I know, you often rely on text files in your PowerShell work. How many times have you used a text file of computernames with Get-Content and then piped to other PowerShell commands only to have errors. Text files are convenient, but often messy. Your text file might have blank lines. Your list of computernames might have a extra spaces after each name. These types of issues will break your PowerShell pipeline.<\/p>\n<p>You can use Where-Object and filter out these types of problems. Or you can use a new function I wrote called Optimize-Text.<\/p>\n<pre class=\"lang:ps decode:true \" >#requires -version 2.0\r\n\r\nFunction Optimize-Text {\r\n\r\n&lt;#\r\n.Synopsis\r\nClean and optimize text input.\r\n.Description\r\nUse this command to clean and optimize content from text files. Sometimes text files have blank lines or the content has trailing spaces. These sorts of issues can cause problems when passing the content to other commands.\r\n\r\nThis command will strip out any lines that are blank or have nothing by white space, and trim leading and trailing spaces. The optimized text is then written back to the pipeline.\r\n\r\nOptionally, you can specify a property name. This can be useful when your text file is a list of computernames and you want to take advantage of pipeline binding. See examples.\r\n\r\nIf your text file has commented lines, use the ignore parameter. As long as the character is the first non-whitespace character in the line, the line will be treated as a comment and ignored.\r\n\r\nFinally, you can use the -Filter parameter to specify a regular expression pattern to further filter what text is written to the pipeline. The filter is applied after leading and trailing spaces have been removed and before any text is converted to upper case.\r\n\r\n.Parameter ToUpper\r\nWrite text output as upper case.\r\n.Parameter Ignore\r\nSpecify a character that will be interpreted as a comment character. It must be the first word character in a line. These lines will be ignored. This parameter has an alias of 'comment'.\r\n.Parameter PropertyName\r\nAssign each line of text a property name. This has the effect of turning your text file into an array of objects with a single property.\r\n.Parameter Filter\r\nUse a regular expression pattern to filter. The filtering is applied after leading and trailing spaces have been trimmed and before text can be converted to upper case.\r\n.Example\r\nPS C:\\&gt; get-content c:\\scripts\\computers.txt \r\n win81-ent-01\r\nserenity\r\n quark\r\njdhit-dc01   \r\n\r\nnovo8\r\n  \r\n                   \r\n\t\r\nPS C:&gt; get-content c:\\scripts\\computers.txt | optimize-text\r\nwin81-ent-01\r\nserenity\r\nquark\r\njdhit-dc01\r\nnovo8\r\n\r\nPS C:&gt; get-content c:\\scripts\\computers.txt | optimize-text -property computername\r\n\r\ncomputername\r\n------------\r\nwin81-ent-01\r\nserenity\r\nquark\r\njdhit-dc01\r\nnovo8\r\n\r\n\r\n.Example\r\nPS C:\\scripts&gt; get-content computers.txt | optimize-text -prop computername | where { test-connection $_.computername -count 1 -erroraction silentlycontinue} | get-service bits | select Name,Status,Machinename\r\n\r\nName                                                                     Status MachineName\r\n----                                                                     ------ -----------\r\nbits                                                                    Running win81-ent-01\r\nbits                                                                    Running jdhit-dc01\r\nbits                                                                    Running novo8\r\n\r\nOptimize the computernames in computers.txt and add a Computername property. Test each computer, ignoring those that fail, and get the Bits service on the ones that can be pinged.\r\n.Example\r\nPS C:\\Scripts&gt; get-content .\\ChicagoServers.txt | optimize-text -Ignore \"#\" -Property ComputerName\r\n\r\nComputerName \r\n------------ \r\nchi-fp01\r\nchi-fp02\r\nchi-core01\r\nchi-test\r\nchi-dc01\r\nchi-dc02\r\nchi-dc04\r\nchi-db01\r\n\r\n.Example\r\nPS C:\\scripts&gt; get-content .\\ChicagoServers.txt | optimize-text -filter \"dc\\d{2}\" -ToUpper -PropertyName Computername | test-connection -count 1\r\n\r\nSource        Destination     IPV4Address      IPV6Address      Bytes    Time(ms)\r\n------        -----------     -----------      -----------      -----    --------\r\nWIN81-ENT-01  CHI-DC01        172.16.30.200                     32       0\r\nWIN81-ENT-01  CHI-DC02        172.16.30.201                     32       0\r\nWIN81-ENT-01  CHI-DC04        172.16.30.203                     32       0\r\n\r\nGet names from text file that match the pattern, turn into an object with a property name and pipe to Test-Connection.\r\n.Notes\r\nLast Updated: Sept. 8, 2014\r\nVersion     : 1.0\r\n\r\nLearn more:\r\n PowerShell in Depth: An Administrator's Guide (http:\/\/www.manning.com\/jones6\/)\r\n PowerShell Deep Dives (http:\/\/manning.com\/hicks\/)\r\n Learn PowerShell in a Month of Lunches (http:\/\/manning.com\/jones3\/)\r\n Learn PowerShell Toolmaking in a Month of Lunches (http:\/\/manning.com\/jones4\/)\r\n\r\n   ****************************************************************\r\n   * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *\r\n   * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *\r\n   * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *\r\n   * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *\r\n   ****************************************************************\r\n\r\n.Inputs\r\nSystem.String\r\n\r\n.Link\r\nhttp:\/\/jdhitsolutions.com\/blog\/2014\/09\/using-optimized-text-files-in-powershell\r\n#&gt;\r\n\r\n[cmdletbinding(DefaultParameterSetName=\"Default\")]\r\n[OutputType([string],ParameterSetName=\"Default\")]\r\n[OutputType([psobject],ParameterSetName=\"object\")]\r\n\r\nParam(\r\n[Parameter(Position=0,HelpMessage=\"Enter some text\",ValueFromPipeline=$True)]\r\n[string[]]$Text,\r\n[regex]$Filter,\r\n[Parameter(ParameterSetName=\"object\")]\r\n[string]$PropertyName,\r\n[Alias(\"comment\")]\r\n[string]$Ignore,\r\n[switch]$ToUpper\r\n)\r\n\r\nBegin {\r\n    Write-Verbose \"Starting $($MyInvocation.Mycommand)\"  \r\n    Write-Verbose \"Using parameters: `n $($PSBoundParameters.GetEnumerator() | format-table | out-string)\"\r\n \r\n} #begin\r\n\r\nProcess {\r\n foreach ($item in $text) {\r\n    #filter out items that don't have at least one non-whitespace character\r\n    if ($item -match \"\\S\") {\r\n        #trim spaces\r\n        Write-Verbose \"Trimming: $item\"\r\n        $output = $item.Trim()\r\n\r\n        if ($Filter) {\r\n            Write-Verbose \"Filtering\"\r\n           $output = $output | where {$_ -match $filter}\r\n        }\r\n\r\n        #only continue if there is output\r\n        if ($output) {\r\n            Write-Verbose \"Post processing $output\"\r\n            if ($ToUpper) {\r\n                $output = $output.toUpper()\r\n            } #if to upper\r\n\r\n            #filter out if using the comment character\r\n            if ($Ignore -AND ($output -match \"^[\\s+]?$Ignore\")) {\r\n                Write-Verbose \"Ignoring comment $output\"\r\n            } #if ignore\r\n            else {\r\n                if ($PropertyName) {\r\n                    #create a custom object with the specified property\r\n                    New-Object -TypeName PSObject -Property @{$PropertyName = $Output}\r\n                }\r\n                else {\r\n                    #just write the output to the pipeline\r\n                    $output\r\n                }\r\n            } #else not ignoring\r\n        } #if output\r\n    } #if item matches non-whitespce\r\n } #foreach \r\n\r\n} #process\r\n\r\nEnd {\r\n    Write-Verbose \"Ending $($MyInvocation.Mycommand)\"\r\n} #end\r\n\r\n} #end function\r\n\r\n#define an alias\r\nSet-Alias -Name ot -Value Optimize-Text<\/pre>\n<p>I've tried to add features to this command for what I think are common issues with text files. The core features are to remove any blank lines and trim leading and trailing spaces from each line of text. I'm working under the assumption that you will be using a command like Get-Content with some sort of list that you want to use with another cmdlet. You can insert Optimize-Text, in between. <\/p>\n<p>For example, perhaps you want a quick and dirty ping test for a list of computers:<\/p>\n<pre class=\"lang:ps decode:true \" >get-content .\\computers.txt | optimize-text | where {Test-Connection $_ -count 1 -ea 0}<\/pre>\n<p>In this example, computers.txt is pretty mangled. There are blank lines and some names have leading and\/or trailing spaces. Using Optimize-Text, fixes those issues. But wait...there's more!<\/p>\n<p>The advantage to using PowerShell is that everything is an object. Often, it helps to take advantage of pipeline binding. For example, the Computername parameter for Test-Connection accepts pipeline input by property name. So if the incoming object has a property that matches the parameter name, it will use it. Optimize-Text allows you to specify a property name. When you do, each line is turned into a custom object with a single property name.<\/p>\n<pre class=\"lang:ps decode:true \" >PS C:\\scripts&gt; get-content .\\computers.txt | optimize-text -PropertyName Computername\r\n\r\nComputername\r\n------------\r\nwin81-ent-01\r\nserenity\r\nquark\r\njdhit-dc01\r\nnovo8<\/pre>\n<p>This means I can run a command like: <\/p>\n<pre class=\"lang:ps decode:true \" >PS C:\\scripts&gt; get-content .\\computers.txt | optimize-text -PropertyName Computername | test-connection -count 1 -erroraction SilentlyContinue\r\n\r\nSource        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)\r\n------        -----------     -----------      -----------                              -----    --------\r\nWIN81-ENT-01  win81-ent-01    172.16.30.127    fe80::bd30:b56e:c9fa:87e1%18             32       0\r\nWIN81-ENT-01  jdhit-dc01      172.16.10.1                                               32       0<\/pre>\n<p>And if you download this function today, I'll throw in parameters to convert each line of text to upper case, to ignore lines with a specified comment character and to do additional filtering with a regular expression pattern!<\/p>\n<pre class=\"lang:ps decode:true \" >PS C:\\scripts&gt; get-content .\\ChicagoServers.txt | optimize-Text -ToUpper -Ignore \"#\" -filter \"dc\\d{2}\" -PropertyName Com\r\nputername | get-ciminstance win32_logicaldisk -filter \"deviceid='c:'\" | Select DeviceID,Size,Freespace,PSComputername\r\n\r\nDeviceID                                               Size                     Freespace PSComputerName\r\n--------                                               ----                     --------- --------------\r\nC:                                              15999168512                    1005551616 CHI-DC01\r\nC:                                              12777943040                    2873114624 CHI-DC02\r\nC:                                              42946523136                   25388650496 CHI-DC04<\/pre>\n<p>By the way here's what the text file looks like:<\/p>\n<pre class=\"lang:batch decode:true \" >#Globomantics chicago servers\r\nchi-fp01\r\nchi-fp02 \r\nchi-core01\r\n chi-test \r\n\r\n #these are the DCs\r\n chi-dc01\r\n chi-dc02\r\n chi-dc04\r\n\r\nchi-db01\r\nchi-hvr2\r\n\r\n\r\n#end of file\r\n<\/pre>\n<p>While it would be nice to think that all of the text files you use in PowerShell and neat and tidy, there's no guarantee someone else might not come along and mess it up again. Hopefully, this function will help.<\/p>\n<p>Please let me know how this works for you in the real world, or what other common problems you run into with text files. Be sure to look at full help and examples. Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are like many IT Pros that I know, you often rely on text files in your PowerShell work. How many times have you used a text file of computernames with Get-Content and then piped to other PowerShell commands only to have errors. Text files are convenient, but often messy. Your text file might&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New from the blog: Using Optimized Text Files in #PowerShell http:\/\/bit.ly\/1rtQIa2","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[72,4,8],"tags":[224,534,250,540],"class_list":["post-4000","post","type-post","status-publish","format-standard","hentry","category-commandline","category-powershell","category-scripting","tag-function","tag-powershell","tag-regular-expressions","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using Optimized Text Files in PowerShell &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Optimized Text Files in PowerShell &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"If you are like many IT Pros that I know, you often rely on text files in your PowerShell work. How many times have you used a text file of computernames with Get-Content and then piped to other PowerShell commands only to have errors. Text files are convenient, but often messy. Your text file might...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2014-09-08T13:33:14+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Using Optimized Text Files in PowerShell\",\"datePublished\":\"2014-09-08T13:33:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/\"},\"wordCount\":434,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/document.png\",\"keywords\":[\"Function\",\"PowerShell\",\"Regular Expressions\",\"Scripting\"],\"articleSection\":[\"CommandLine\",\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/\",\"name\":\"Using Optimized Text Files in PowerShell &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/document.png\",\"datePublished\":\"2014-09-08T13:33:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#primaryimage\",\"url\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/document.png\",\"contentUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/document.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4000\\\/using-optimized-text-files-in-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"CommandLine\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/commandline\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Optimized Text Files in PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Optimized Text Files in PowerShell &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/","og_locale":"en_US","og_type":"article","og_title":"Using Optimized Text Files in PowerShell &#8226; The Lonely Administrator","og_description":"If you are like many IT Pros that I know, you often rely on text files in your PowerShell work. How many times have you used a text file of computernames with Get-Content and then piped to other PowerShell commands only to have errors. Text files are convenient, but often messy. Your text file might...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/","og_site_name":"The Lonely Administrator","article_published_time":"2014-09-08T13:33:14+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Using Optimized Text Files in PowerShell","datePublished":"2014-09-08T13:33:14+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/"},"wordCount":434,"commentCount":0,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png","keywords":["Function","PowerShell","Regular Expressions","Scripting"],"articleSection":["CommandLine","PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/","name":"Using Optimized Text Files in PowerShell &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png","datePublished":"2014-09-08T13:33:14+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#primaryimage","url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png","contentUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"CommandLine","item":"https:\/\/jdhitsolutions.com\/blog\/category\/commandline\/"},{"@type":"ListItem","position":2,"name":"Using Optimized Text Files in PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":3025,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3025\/scrub-up-powershell-content\/","url_meta":{"origin":4000,"position":0},"title":"Scrub Up PowerShell Content","author":"Jeffery Hicks","date":"May 14, 2013","format":false,"excerpt":"It is probably a safe bet to say that IT Pros store a lot of information in simple text files. There's nothing with this. Notepad is ubiquitous and text files obviously easy to use. I bet you have text files of computer names, user names, service names, directories and probably\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"scrubbrush","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/05\/scrubbrush-300x214.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":88,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/88\/powershell-parsing\/","url_meta":{"origin":4000,"position":1},"title":"Powershell Parsing","author":"Jeffery Hicks","date":"January 16, 2007","format":false,"excerpt":"In PowerShell, Get-WMIObject is a terrific cmdlet for remotely managing systems. If you have a text list of server or computer names, here's a quick method you could enumerate that list and do something to each server.foreach ($server in (Get-Content s:\\servers.txt)) {#skip blank lines if (($server).length -gt 0) { $server\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4985,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4985\/converting-text-to-html-revised\/","url_meta":{"origin":4000,"position":2},"title":"Converting Text to HTML Revised","author":"Jeffery Hicks","date":"May 9, 2016","format":false,"excerpt":"A few years ago I published a PowerShell function to convert text files into HTML listings. I thought it would be handy to convert scripts to HTML documents with line numbering and some formatting. Turns out someone actually used it! He had some questions about the function which led me\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"convertto-htmllisting2","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/05\/convertto-htmllisting2_thumb.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/05\/convertto-htmllisting2_thumb.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/05\/convertto-htmllisting2_thumb.png?resize=525%2C300 1.5x"},"classes":[]},{"id":5112,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/5112\/creating-git-commit-messages-with-powershell\/","url_meta":{"origin":4000,"position":3},"title":"Creating Git Commit Messages with PowerShell","author":"Jeffery Hicks","date":"June 21, 2016","format":false,"excerpt":"As part of my process of learning an using Git I am trying to get in the habit of using meaningful commit messages. Sure, you can get by with a single line comment which is fine when running git log --oneline. But you can use a multi-line commit message. However,\u2026","rel":"","context":"In &quot;Git&quot;","block_context":{"text":"Git","link":"https:\/\/jdhitsolutions.com\/blog\/category\/git\/"},"img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/06\/image_thumb-19.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/06\/image_thumb-19.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/06\/image_thumb-19.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1220,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1220\/friday-fun-virtual-demos\/","url_meta":{"origin":4000,"position":4},"title":"Friday Fun Virtual Demos","author":"Jeffery Hicks","date":"March 11, 2011","format":false,"excerpt":"I've been prepping my demo scripts for Techmentor using the ubiquitous Start-Demo, and realized I could take things further. I mean, why do I have to do all the talking? Windows 7 has a terrific text to speech engine so why not take advantage of it. With a little work\u2026","rel":"","context":"In &quot;Conferences&quot;","block_context":{"text":"Conferences","link":"https:\/\/jdhitsolutions.com\/blog\/category\/conferences\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":467,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/467\/get-numberedcontent-v2\/","url_meta":{"origin":4000,"position":5},"title":"Get-NumberedContent v2","author":"Jeffery Hicks","date":"October 22, 2009","format":false,"excerpt":"I wasn\u2019t completely satisfied with the updated version of my Get-NumberedContent function. You should still refer to the earlier post for details on how to use the function. But I had some issues with the previous version and realized there were a few bugs. I\u2019ve since updated the Get-NumberedContent function.\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4000"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4000\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}