{"componentChunkName":"component---src-templates-articles-index-jsx","path":"/articles/powershell-read-text-files/","result":{"data":{"props":{"adsense":true,"adsenseMobile":null,"byLine":"","canonical":"https://staging.business.com/articles/powershell-read-text-files/","content":"\n<p>Use PowerShell cmdlets to learn the basics: how to retrieve text from text files and how to find text in text files.</p>\r\n<p><picture class=\"aligncenter full\">One of the most prevalent tasks an IT pro must do is either create, read or change text files in some way. Text files are represented in thousands of different formats from simple TXT files to XML to various proprietary formats. Since text files can be structured in such a huge number of ways vendors have adopted the simple text file as means of defining scripts, storing configuration values and SQL queries and so much more. The only structural difference in a TXT file and a SQL file, for example, is just the file extension. The contents are just text structured in a different, pre-defined manner.</picture><br><picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/a2a5d1039ebe9718b45c8/1200_-900- 1200w, /images/content/5b6/a2a5d1039ebe9718b45c8/668_-501- 668w, /images/content/5b6/a2a5d1039ebe9718b45c8/320_-240- 320w\" sizes=\"668px, 100%\"></source></picture></p>\r\n<div id=\"in-article-vca-1\"> </div>\r\n<p>PowerShell has many different ways to manage all of these text file types but in this article we're going to stick with the most generic approach: We'll be focusing on reading and unstructured text. This means no XML files, no SQL query files, not even PowerShell scripts themselves. We'll be focusing on a two topics:</p>\r\n<ol>\n<li>How to retrieve text from text files.</li>\r\n<li>How to find text in text files.</li>\r\n</ol>\n<h2>How To Retrieve Text From Text Files</h2>\r\n<p>One of the easiest tasks is retrieving all text from an existing text file. For most text files, a PowerShell scripter can use the <strong>Get-Content</strong> cmdlet. The Get-Content cmdlet is a very popular PowerShell cmdlet that will retrieve all text from a text file specified by the Path parameter.</p>\r\n<p>At it's simplest, you can pass the Path parameter with the file path to a text file as the argument to the Get-Content cmdlet. Let's say I have a file called MyText.txt. In this text file I have the three line shown below and I'd like to get the contents of MyText.txt with a PowerShell script.</p>\r\n<p><picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/a2a9892a3a46c438b4580/1200_-171- 1200w, /images/content/5b6/a2a9892a3a46c438b4580/668_-95- 668w, /images/content/5b6/a2a9892a3a46c438b4580/320_-45- 320w\" sizes=\"668px, 100%\"></source><img src=\"/images/content/5b6/a2a9892a3a46c438b4580/777-111-\" alt=\"\" width=\"777\" height=\"111\" data-content-img-id=\"5b6a2a9892a3a46c438b4580\"></picture></p>\r\n<p>To do this, I can use Get-Content with the Path parameter.</p>\r\n<p><picture class=\"aligncenter med\"><source srcset=\"/images/content/5b6/a2b6551ee474c438b458a/742_-159- 742w, /images/content/5b6/a2b6551ee474c438b458a/412_-88- 412w, /images/content/5b6/a2b6551ee474c438b458a/206_-44- 206w\" sizes=\"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%\"></source><img src=\"/images/content/5b6/a2b6551ee474c438b458a/396-85-\" alt=\"\" width=\"396\" height=\"85\" data-content-img-id=\"5b6a2b6551ee474c438b458a\"></picture></p>\r\n<p>You'll see that the result was the entire contents of MyText.txt. But what if you just wanted to see a particular line number? By default, Get-Content reads every line in a text file and creates an array as its output with each line of the text as an element in that array. This means I can easily pick out different elements in that array by specifying the array index number. In this instance, the array index number is equal to the text file line number.</p>\r\n<p><picture class=\"aligncenter med\"><source srcset=\"/images/content/5b6/a2c121039ebca718b45de/742_-373- 742w, /images/content/5b6/a2c121039ebca718b45de/412_-207- 412w, /images/content/5b6/a2c121039ebca718b45de/206_-103- 206w\" sizes=\"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%\"></source><img src=\"/images/content/5b6/a2c121039ebca718b45de/393-198-\" alt=\"\" width=\"393\" height=\"198\" data-content-img-id=\"5b6a2c121039ebca718b45de\"></picture></p>\r\n<p>In the example above, I'm showing you that the result of Get-Content is a type of System.Array and that by using index numbers in brackets I can pick and choose which lines I want output to my console. One thing to note is that if you're unfamiliar with arrays and their numbering scheme, the very first element in an array starts with a zero. This is why I'm able to use the number one to pick the second line and the number two to pick the third line. This is important to remember.</p>\r\n<h2>How To Find Text In Text Files</h2>\r\n<p>Once you're able to figure out how to find all text in text files or certain line numbers, another popular tasks is finding specific text inside of the files themselves. There are a few different ways to do this but the easiest is by using the <strong>Where-Object</strong> cmdlet. The Where-Object cmdlet is a popular cmdlet that allows you to \"filter out\" various information from the output of other cmdlets; in this example, it is filtering info from the Get-Content cmdlet.</p>\r\n<p>Using our previous text file as an example, let's say I want to see if that text file contains the words \"text file\" together in it. To do this, I'll use Where-Object.</p>\r\n<p><picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/a2cc351ee4799788b4569/1200_-113- 1200w, /images/content/5b6/a2cc351ee4799788b4569/668_-63- 668w, /images/content/5b6/a2cc351ee4799788b4569/320_-30- 320w\" sizes=\"668px, 100%\"></source><img src=\"/images/content/5b6/a2cc351ee4799788b4569/644-61-\" alt=\"\" width=\"644\" height=\"61\" data-content-img-id=\"5b6a2cc351ee4799788b4569\"></picture></p>\r\n<p>When using Where-Object I must use the <strong>$_</strong> variable. This is a special variable in PowerShell known as the pipeline variable. In this example, it represents each line of the text file as it comes from Get-Content. I'm then using the like operator and using wildcards to see if the string \"text file\" exists in any of the lines of the text file. It looks like it found two lines matching the string.</p>\r\n<p>If you'd like to learn more about finding text in text files I suggest you look into PowerShell's comparison operators. Using the <strong>match</strong> operator, for example, uses regex, which is much more powerful than the <strong>like </strong>operator and allows you to find just about any pattern you can think of in a text file.</p>\r\n<p>We just barely scratched the surface as to what can be done with reading text files with PowerShell. I highly encourage you to reference the content on <a href=\"https://technet.microsoft.com/en-us/library/ee176843.aspx\" target=\"_blank\">TechNet on using the Get-Content cmdlet</a> and explore the various parameters you can use.</p>\r\n<div id=\"in-article-vca-2\"> </div>\n","docType":"article","guid":"articles_91d4e2ab1dc36752839effc2cf833134","lastModified":"2018-09-05T10:00:00.000Z","published":"2018-09-05T10:00:00.000Z","structuredData":null,"subtitle":"Learn the basics: how to retrieve text from text files and how to find text in text files.","template":"v1","title":"Introduction To Using Text Files With PowerShell","type":"article","author":{"avatar":null,"bio":"Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.","name":"Adam Bertram","image":null,"slug":null,"social":{"facebook":"","linkedin":"https://www.linkedin.com/in/adambertram/","twitter":"https://twitter.com/adbertram","website":"https://www.adamtheautomator.com/"},"status":"enabled","title":null,"type":"Contributing Writer","user":{"imagePath":"https://staging.business.com/images/undefined","profile":{"company":"eGain Communications","jobTitle":"Product Management Analyst","website":""}}},"breadcrumbs":null,"channels":{"primary":{"name":"Technology","slug":"technology"},"sub":{"name":"Technology Solutions","slug":"technology-solutions"}},"meta":{"robots":"index, follow","description":"PowerShell offers many ways to manage text files. Learn the basics: how to retrieve text from text files and how to find text in text files.","title":"How to read text in text files with PowerShell - business.com"},"parsedContent":{"toc":[{"id":"how-to-retrieve-text-from-text-files","title":"How To Retrieve Text From Text Files"},{"id":"how-to-find-text-in-text-files","title":"How To Find Text In Text Files"}],"json":[{"component":"p","children":["Use PowerShell cmdlets to learn the basics: how to retrieve text from text files and how to find text in text files."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":["One of the most prevalent tasks an IT pro must do is either create, read or change text files in some way. Text files are represented in thousands of different formats from simple TXT files to XML to various proprietary formats. Since text files can be structured in such a huge number of ways vendors have adopted the simple text file as means of defining scripts, storing configuration values and SQL queries and so much more. The only structural difference in a TXT file and a SQL file, for example, is just the file extension. The contents are just text structured in a different, pre-defined manner."]},{"component":"br"},{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a2a5d1039ebe9718b45c8/1200_-900- 1200w, /images/content/5b6/a2a5d1039ebe9718b45c8/668_-501- 668w, /images/content/5b6/a2a5d1039ebe9718b45c8/320_-240- 320w","sizes":"668px, 100%"}}]}]},{"component":"div","props":{"id":"in-article-vca-1"},"children":[" "]},{"component":"p","children":["PowerShell has many different ways to manage all of these text file types but in this article we're going to stick with the most generic approach: We'll be focusing on reading and unstructured text. This means no XML files, no SQL query files, not even PowerShell scripts themselves. We'll be focusing on a two topics:"]},{"component":"ol","children":[{"component":"li","children":["How to retrieve text from text files."]},{"component":"li","children":["How to find text in text files."]}]},{"component":"h2","props":{"id":"how-to-retrieve-text-from-text-files"},"children":["How To Retrieve Text From Text Files"]},{"component":"p","children":["One of the easiest tasks is retrieving all text from an existing text file. For most text files, a PowerShell scripter can use the ",{"component":"strong","children":["Get-Content"]}," cmdlet. The Get-Content cmdlet is a very popular PowerShell cmdlet that will retrieve all text from a text file specified by the Path parameter."]},{"component":"p","children":["At it's simplest, you can pass the Path parameter with the file path to a text file as the argument to the Get-Content cmdlet. Let's say I have a file called MyText.txt. In this text file I have the three line shown below and I'd like to get the contents of MyText.txt with a PowerShell script."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a2a9892a3a46c438b4580/1200_-171- 1200w, /images/content/5b6/a2a9892a3a46c438b4580/668_-95- 668w, /images/content/5b6/a2a9892a3a46c438b4580/320_-45- 320w","sizes":"668px, 100%"}},{"component":"img","props":{"src":"/images/content/5b6/a2a9892a3a46c438b4580/777-111-","alt":"","width":"777","height":"111","data-content-img-id":"5b6a2a9892a3a46c438b4580"}}]}]},{"component":"p","children":["To do this, I can use Get-Content with the Path parameter."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter med"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a2b6551ee474c438b458a/742_-159- 742w, /images/content/5b6/a2b6551ee474c438b458a/412_-88- 412w, /images/content/5b6/a2b6551ee474c438b458a/206_-44- 206w","sizes":"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%"}},{"component":"img","props":{"src":"/images/content/5b6/a2b6551ee474c438b458a/396-85-","alt":"","width":"396","height":"85","data-content-img-id":"5b6a2b6551ee474c438b458a"}}]}]},{"component":"p","children":["You'll see that the result was the entire contents of MyText.txt. But what if you just wanted to see a particular line number? By default, Get-Content reads every line in a text file and creates an array as its output with each line of the text as an element in that array. This means I can easily pick out different elements in that array by specifying the array index number. In this instance, the array index number is equal to the text file line number."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter med"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a2c121039ebca718b45de/742_-373- 742w, /images/content/5b6/a2c121039ebca718b45de/412_-207- 412w, /images/content/5b6/a2c121039ebca718b45de/206_-103- 206w","sizes":"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%"}},{"component":"img","props":{"src":"/images/content/5b6/a2c121039ebca718b45de/393-198-","alt":"","width":"393","height":"198","data-content-img-id":"5b6a2c121039ebca718b45de"}}]}]},{"component":"p","children":["In the example above, I'm showing you that the result of Get-Content is a type of System.Array and that by using index numbers in brackets I can pick and choose which lines I want output to my console. One thing to note is that if you're unfamiliar with arrays and their numbering scheme, the very first element in an array starts with a zero. This is why I'm able to use the number one to pick the second line and the number two to pick the third line. This is important to remember."]},{"component":"h2","props":{"id":"how-to-find-text-in-text-files"},"children":["How To Find Text In Text Files"]},{"component":"p","children":["Once you're able to figure out how to find all text in text files or certain line numbers, another popular tasks is finding specific text inside of the files themselves. There are a few different ways to do this but the easiest is by using the ",{"component":"strong","children":["Where-Object"]}," cmdlet. The Where-Object cmdlet is a popular cmdlet that allows you to \"filter out\" various information from the output of other cmdlets; in this example, it is filtering info from the Get-Content cmdlet."]},{"component":"p","children":["Using our previous text file as an example, let's say I want to see if that text file contains the words \"text file\" together in it. To do this, I'll use Where-Object."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a2cc351ee4799788b4569/1200_-113- 1200w, /images/content/5b6/a2cc351ee4799788b4569/668_-63- 668w, /images/content/5b6/a2cc351ee4799788b4569/320_-30- 320w","sizes":"668px, 100%"}},{"component":"img","props":{"src":"/images/content/5b6/a2cc351ee4799788b4569/644-61-","alt":"","width":"644","height":"61","data-content-img-id":"5b6a2cc351ee4799788b4569"}}]}]},{"component":"p","children":["When using Where-Object I must use the ",{"component":"strong","children":["$_"]}," variable. This is a special variable in PowerShell known as the pipeline variable. In this example, it represents each line of the text file as it comes from Get-Content. I'm then using the like operator and using wildcards to see if the string \"text file\" exists in any of the lines of the text file. It looks like it found two lines matching the string."]},{"component":"p","children":["If you'd like to learn more about finding text in text files I suggest you look into PowerShell's comparison operators. Using the ",{"component":"strong","children":["match"]}," operator, for example, uses regex, which is much more powerful than the ",{"component":"strong","children":["like "]},"operator and allows you to find just about any pattern you can think of in a text file."]},{"component":"p","children":["We just barely scratched the surface as to what can be done with reading text files with PowerShell. I highly encourage you to reference the content on ",{"component":"a","props":{"href":"https://technet.microsoft.com/en-us/library/ee176843.aspx","target":"_blank"},"children":["TechNet on using the Get-Content cmdlet"]}," and explore the various parameters you can use."]},{"component":"div","props":{"id":"in-article-vca-2"},"children":[" "]}]},"resource":null,"sponsorship":null,"thumbnail":{"path":"https://staging.business.com/images/content/5db/f21fe5a215edb4f8b4571/1500-0-","caption":"scyther5/Getty","isBlurred":false},"bestPicks":null},"related":{"relatedArticles":[{"title":"Introduction To Using Text Files With PowerShell","canonical":"https://www.business.com/articles/powershell-read-text-files/","slug":"powershell-read-text-files","thumbnail":"https://www.business.com/images/content/5db/f21fe5a215edb4f8b4571/1500-0-","dek":"Learn the basics: how to retrieve text from text files and how to find text in text files.","lastModified":"2018-09-05T10:00:00Z","published":"2018-09-05T10:00:00Z","sponsored":false},{"title":"PowerShell: Everything You Need to Know","canonical":"https://www.business.com/articles/powershell-everything-you-need-to-know/","slug":"powershell-everything-you-need-to-know","thumbnail":"https://www.business.com/images/content/5b8/998581039eb8f738b457b/1500-0-","dek":"What you need to know to unlock Microsoft PowerShell's full capabilities.","lastModified":"2018-09-05T17:00:00Z","published":"2018-09-05T17:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Managing Active Directory","canonical":"https://www.business.com/articles/powershell-active-directory-cmdlets/","slug":"powershell-active-directory-cmdlets","thumbnail":"https://www.business.com/images/content/5cf/90cab7b4374ab068b456f/1500-0-","dek":"Administering Active Directory is one of the most critical roles in any Windows network.","lastModified":"2018-09-04T08:00:00Z","published":"2018-09-04T08:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Auditing And Maintaining Storage","canonical":"https://www.business.com/articles/powershell-for-storage/","slug":"powershell-for-storage","thumbnail":"https://www.business.com/images/content/5d0/18e065a215ea2498b4588/1500-0-","dek":"PowerShell v5 offers some new features for managing storage.","lastModified":"2018-09-04T11:00:00Z","published":"2018-09-04T11:00:00Z","sponsored":false},{"title":"Introduction To Using XML Files With PowerShell","canonical":"https://www.business.com/articles/powershell-read-xml-files/","slug":"powershell-read-xml-files","thumbnail":"https://www.business.com/images/content/5db/f21da9c658a05188b4577/1500-0-","dek":"PowerShell offers several ways to read XML documents, without writing a lot of code.","lastModified":"2018-09-05T12:01:00Z","published":"2018-09-05T12:00:00Z","sponsored":false},{"title":"Introduction To Using XML Files With PowerShell","canonical":"https://staging.business.com/articles/powershell-read-xml-files/","slug":"powershell-read-xml-files","thumbnail":"https://staging.business.com/images/content/5c5/b039b54d26d9e348b4574/1500-0-","dek":"PowerShell offers several ways to read XML documents, without writing a lot of code.","lastModified":"2019-02-03T08:00:00Z","published":"2018-09-05T12:00:00Z","sponsored":null},{"title":"Using Lean Strategies to Achieve Team Synergy and Efficiency","canonical":"https://www.business.com/articles/lean-strategies-team-efficiency/","slug":"lean-strategies-team-efficiency","thumbnail":"https://www.business.com/images/content/5be/480e49c658a51108b458c/1500-0-","dek":"Learn more about lean manufacturing strategies and how to implement them in your business.","lastModified":"2018-11-08T08:00:00Z","published":"2018-11-08T08:00:00Z","sponsored":false},{"title":"How To Sync Folders With PowerShell","canonical":"https://www.business.com/articles/powershell-sync-folders/","slug":"powershell-sync-folders","thumbnail":"https://www.business.com/images/content/5cf/f157d9c658a99338b4568/1500-0-","dek":"There is software that will sync files and folders for you, and PowerShell can help.","lastModified":"2018-09-06T09:00:00Z","published":"2018-09-06T09:00:00Z","sponsored":false},{"title":"How to Create a Web Scraping Tool in PowerShell","canonical":"https://www.business.com/articles/create-web-scraping-tool-in-powershell/","slug":"create-web-scraping-tool-in-powershell","thumbnail":"https://www.business.com/images/content/5d0/06c899c658acc288b458a/1500-0-","dek":"Write a tool in PowerShell that will gather all the data from a web page.","lastModified":"2018-09-05T09:00:00Z","published":"2018-09-05T09:00:00Z","sponsored":false},{"title":"From COVID-19 to Hurricane Season: Disaster Preparedness for Small Business","canonical":"https://www.business.com/articles/business-disaster-prep/","slug":"business-disaster-prep","thumbnail":"https://www.business.com/images/content/5e7/a495e5a215eb11d8b4569/1500-0-","dek":"Natural disasters – whether a hurricane, tornado or outbreak like COVID-19 – can have a disastrous effect on small businesses, but they don't have to. Here's how to weather a disaster successfully.","lastModified":"2020-03-25T15:19:00Z","published":"2018-09-26T07:00:00Z","sponsored":false},{"title":"Your Guide to Creating a Small Business Marketing Plan","canonical":"https://www.business.com/articles/sample-marketing-plan-outline-and-template/","slug":"sample-marketing-plan-outline-and-template","thumbnail":"https://www.business.com/images/content/5f3/3e1765a215eb62d8b4567/1500-0-","dek":"Follow these templates and guidelines to get started on creating your business's marketing plan.","lastModified":"2020-08-12T12:28:00Z","published":"2018-03-02T13:00:00Z","sponsored":false},{"title":"Understanding Functions in PowerShell","canonical":"https://www.business.com/articles/functions-in-powershell/","slug":"functions-in-powershell","thumbnail":"https://www.business.com/images/content/5d0/272227b4374087d8b4568/1500-0-","dek":"Instead of copying and pasting the same code over and over again, create a PowerShell func","lastModified":"2018-09-04T09:00:00Z","published":"2018-09-04T09:00:00Z","sponsored":false}],"relatedReviews":[{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df908cc5a215e8c4a8b4568/conferencecall_Antonio_Diaz_Getty.jpg","canonical":"https://staging.business.com/categories/best-conference-call-services/","title":"The Best Conference Call Services of 2021","dek":"Which conference call service is right for your business in 2021? We review the top-rated services.","slug":"best-conference-call-services","lastModified":"2021-03-17T18:17:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df3d8695a215e58428b4570/POS_GETTY_DERO2084.jpg","canonical":"https://staging.business.com/categories/best-pos-systems/","title":"The Best POS Systems of 2021","dek":"Before you choose a POS system, check out our reviews of the best picks for 2021. ","slug":"best-pos-systems","lastModified":"2021-03-17T17:33:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5e00d4787b43745d6a8b456e/Laptop_undrey_getty.jpg","canonical":"https://staging.business.com/categories/employee-monitoring-software/","title":"The Best Employee Monitoring Software of 2021","dek":"We've narrowed down the best employee monitoring software for businesses. Check out our best picks and reviews.","slug":"employee-monitoring-software","lastModified":"2021-03-05T15:40:00Z"}],"relatedSponsoredArticles":null}},"pageContext":{"displayModified":"2018-09-05T10:00:00.000Z","template":"v1","title":"Introduction To Using Text Files With PowerShell","meta":{"robots":"index, follow"},"docType":"articles","slug":"powershell-read-text-files","canonical":"https://staging.business.com/articles/powershell-read-text-files/","skipTOC":false}},"staticQueryHashes":[]}