Read Contents of Text File Into Array Powershell

Text files are ubiquitous and yous'll accept to read through them, regardless of your role or job responsibilities. In fact, as an It professional, you volition have to create, read, or work with text files more oftentimes than you may recollect because text files come in a wide range of formats. This flexibility has as well made text files the most user-friendly fashion to define scripts, store configuration details, and more. PowerShell understands this importance of text files and makes it piece of cake to retrieve text and read from them easily. In this article, we'll run across how you tin read a text file in PowerShell.

Reading the entire contents

linux text editors

Shutterstock

When you desire to read the entire contents of a text file, the easiest way is to use the built-in Get-Content part.

Here is the code that allows yous to do this:

Get-Content C:\logs\log01012020.txt

When yous execute this command, the contents of this file volition be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute information technology.

You tin also movement all the content to a variable and use that variable for further processing if that'southward something that you want your code to practise.

$file_data = Get-Content C:\logs\log01012020.txt

Yous can at present use this variable $file_data for parsing or farther processing.

Reading partial content

powershell-text-files

Many times, y'all may only have to read a certain part of a file to go the data you desire. Similar to a SQL query, you can choose the lines you desire to read and the code for that is:

$file_data = Get-Content C:\logs\log01012020.txt $file_data | Select-Object -Showtime 10        

As the code suggests, the first 10 lines of the content will be stored in the variable and not the unabridged content. You can display the contents of this variable or utilize it for more processing.

Likewise, we can read the last few lines too:

$file_data = Get-Content C:\logs\log01012020.txt $file_data | Select-Object -Last 10        

Reading line-past-line

When yous want to read the file to understand its contents, you'd have to do so one line at a fourth dimension and the good news is, this is possible with PowerShell.

In fact, there are two ways to do it.

Using Get-Content

The Get-Content office reads every line in the text and stores them every bit an assortment, where each line is an array element. In the above example, nosotros used a variable for reading all the content.

$file_data = Get-Content C:\logs\log01012020.txt

If y'all print the type of this variable, y'all tin see that it an array.

This means you can pick out specific lines using the array index. For example, if you want to read the first line, you lot can only say:

$file_data[0]

And this will display the first line for you.

$file_data[i]

This volition brandish the second line, and and then on.

Using StreamReader class

The 2d option is to use a .Cyberspace course called StreamReader.

$stream_reader = New-Object System.IO.StreamReader{C:\logs\log01012020.txt}

At present you accept the contents of the log file in this $stream_reader variable and since it belongs to the StreamReader class, you can use many built-in methods to get the text you want.

When y'all say…

$stream_reader.ReadToEnd()

…this volition output all the contents on your screen, like to Get-Content.

To read the current line, you tin use the following method:

$stream_reader.ReadLine()

Only this alone may not be helpful, so you'll have to run it in a loop and read the contents of a text file, line by line.

$stream_reader = New-Object Organisation.IO.StreamReader{C:\logs\log01012020.txt} $line_number = i while (($current_line =$stream_reader.ReadLine()) -ne $null) { Write-Host "$line_number  $current_line" $line_number++ }        

The above code snippet will start with the first line and volition print out each line with its line number for easy reading. Yous can even use whatever of the available cord methods on the $current_line variable to parse it further.

This method is ideal for reading big files because when you store the contents in a variable, it tin can have up besides much retention and tin impact functioning as well. And so, this is a more efficient way to become to the content you want.

Finding specific text

Many times, you'd want to observe a specific text in a file and the best to do that is to employ the Where-Object cmdlet that filters the content to give you what you want.

$file_data = Get-Content C:\logs\log01012020.txt | Where-Object {$_ -similar '*error*'}

The above lawmaking will output those lines that take the word "error" in them. Hither, the $_ is a pipeline variable that represents the current line from the contents that come from Go-Content.

Besides Where-Object, you can also use match and regex operators to find the exact text you lot want.

More options with Go-Content

Go-Content is a highly versatile cmdlet that comes loaded with many options. Here are some things you can do with it.

Count the number of lines in the file

You may desire to know the number of lines available in the file, and so y'all can parse it appropriately. The code for that is:

$file_data = Become-Content C:\logs\log01012020.txt | Mensurate-Object

Specific number of lines at the start and the end

Earlier, nosotros saw how to choose the offset few or last few lines using the Select-Object cmdlet. Y'all can also get the same results using some of the built-in methods of Go-Content.

To get the first few lines, the method is:

$file_data = Get-Content C:\logs\log01012020.txt -TotalCount 3

This will return the offset three lines from the file.

$file_data = Get-Content C:\logs\log01012020.txt -Tail 3

This command will return the last three lines from the file.

Continuously updated file

Let's say your log file is continuously updated and yous want to see the finish of the log file to read the last updated values. For that, you can add together the Expect parameter, similar this:

$file_data = Get-Content C:\logs\log01012020.txt -Tail 3 -Wait

This command will continuously monitor the log file for the new lines that are getting added to it and will display the same to you.

Thus, these are some of the unlike ways to read a text file in PowerShell. Practice let u.s. know if yous know more ways to read the contents of a file in PowerShell.

Featured epitome: Freerange Stock

More than PowerShell Basics articles
  • How to Bank check Your Windows Server Uptime with PowerShell
  • How to Uninstall Software Using PowerShell
  • Practise You Like Your PowerShell Wet or Dry?
  • Scripting help from the Windows Admin Center
  • Getting Started with PowerShell for Microsoft Teams


sansompomity.blogspot.com

Source: https://techgenix.com/read-text-file-powershell/

0 Response to "Read Contents of Text File Into Array Powershell"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel