How to Iterate the Content of a Text File in Powershell

Photo of author

By Victor Ashiedu

Published

There are many situations where, as a Sysadmin you need to perform the same task on the content of a text file. In this guide, I’ll discuss how to iterate the content of a text file using PowerShell.

Overview

Before you can loop through the content of a text file, you must import what is in the text file with the Get-Content command. Once you have the information, you can use the available PowerShell loop construct to iterate the content and perform tasks on each of them.

In PowerShell, there are three main constructs you can use for this – the ForEach Loop, ForEach-Object Cmdlet, and ForEach() Method.

Let’s see how to use the Get-Content command with these constructs in a PowerShell script.

Step 1: Use Get-Content to List the Content of a File

Before you can iterate the content of a text file with the PowerShell ForEach, you have to list the content with the Get-Content command.

The general syntax of the the Get-Content command is…

Get-Content -Path <full path to text file>

The Get-Content command reads the content of a text file line by line. Then, it returns a collection of objects.

Moreover, an object in the collection of objects represents a line of the content in the text file. This point is important because it means that we can access the individual objects returned by the Get-Content command.

This is what gives us the ability to use PowerShell ForEach to iterate the content of a text file – line by line.

Another important feature of the Get-Content command is that it can read a specified number of lines from the beginning or end of a text file.

To kick off examples in this guide, run the command below:

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt

Here is the content of the text file used in the command. The command lists the contents of the file and saves it in a variable called files.

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt

Finally, to illustrate my previous point about reading a specified number of lines at the beginning or the end of the file – run the command below:

Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt -Head 3

The command reads the first three lines of the specified text file. This is specified by the Head parameter.

Here is the result of the command…

Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt -Head 3

Alternatively, you can return the last “N” lines with the Tail parameter of the Get-Content command.

Here is an example command that returns the last 4 lines of a file…

Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt -Tail 4

And here is the result of the command…

Step 2 (Example 1): Combine Get-Content with ForEach Loop

In my introduction to this guide, I mentioned that the first step to iterating the content of a text file with PowerShell is to list its content.

I also mentioned that after listing the content of a text file with the Get-Content command, you can use PowerShell ForEach to iterate the content of the text file.

Finally, I did mention that you can iterate a text file with the PowerShell ForEach loop, ForEach-Object Cmdlet, or the ForEach Method.

In the first section of this guide, I showed you how to use the Get-Content command to list the content of a text file. Moreover, I ran this command that saved the content of the specified file in the files variable.

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt

In this section, I will teach you the syntax of the ForEach loop. Then, I will show you an example of Powershell ForEach $file In $files – essentially, using ForEach loop to iterate files stored in our files variable.

Let’s roll!

Syntax of PowerShell ForEach Loop

The syntax of the PowerShell ForEach Loop is…

ForEach ($file in $files) {
<Run the command in this block>
}

In the syntax, $files represents a variable with a list of items. In this example, the $files variable is the output of the Get-Content command shown below…

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt

Back to the syntax of the PowerShell ForEach Loop, the variable $file represents each item saved in $files variable.

Furthermore, each time ForEach Loop goes through a cycle, it saves one of the $files variable in the temporal $file variable. Then, it uses this temporarily saved variable in the command bracket {}, of the ForEach.

Powershell ForEach $file in $files Example

In the last sub-section, I showed you the syntax of ForEach Loop as…

ForEach ($file in $files) {
<Run the command in this block>
}

I also explained that in each cycle of the Powershell ForEach loop, it saves one of the objects saved in the $files variable in a temporal variable called $file.

In this section, I will use this principle to iterate the content of the text file saved in $files variable. Then, in the command part of the ForEach loop, I will use the New-Item command to create a folder with the items saved in the temporal variable, $file.

Here is the command that does the job…

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt
ForEach ($file in $files) {
New-Item -Name $file -Path "D:\PS-Tutorial\ForEachEx" -ItemType "directory" | Out-Null } 
}

To run the script, copy it to a new document in the PowerShell console and click enter. Here is a screenshot of the content of D:\PS-Tutorial\ForEachEx.

In case you had forgotten, these are the same items listed in my original text file!

In essence, the PowerShell ForEach loop iterated the content of the text file (returned by the Get-Content command). Then, used the results to create a new folder…

Step 2 (Example 2): Combine Get-Content with ForEach-Object Cmdlet

In the last section, I showed you how to use the PowerShell ForEach loop to iterate the content of the text file returned by the Get-Content command.

In this section, I will show you how to use the ForEach-Object Cmdlet to perform the same task. But first, let’s start with the syntax of the ForEach-Object Command.

Syntax Of PowerShell ForEach-Object Command

For practical purposes, the syntax of the PowerShell ForEach-Object command is…

<collection of input objects> | ForEach-Object {
<Run the command in this block>
}

PowerShell ForEach-Object takes the values of objects from a pipeline and runs the command specified in the command block {} of ForEach-Object.

Unlike in the PowerShell ForEach Loop where, in each cycle, an object is saved in a specified temporal variable – in PowerShell ForEach-Object, the object is saved in a temporal automatic “object in the current pipeline” variable, $_.

Finally, PowerShell ForEach-Object uses the automatic variable, $_ as input to run the command in the command {} block.

More in the next sub-section…

Powershell ForEach-Object $_ in $files Example

In this example, I will use the content of the file saved in the $files variable, to create folders. The example is similar to what we did in the ForEach Loop section.

However, instead of using ForEach Loop, we will use ForEach-Object Cmdlet.

Before I proceed though, let’s look at the screenshot of the original text file.

Powershell ForEach-Object $_ in $files Example

Also, here is the script that saved the content of the file to the $files variable.

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt

Now that we have refreshed our minds about the original text file, let’s see how to use the file in the PowerShell ForEach-Object command.

Here is the script that creates folders with the content of the text file:

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt
$files | ForEach-Object {
   New-Item -Name $_ -Path "D:\PS-Tutorial\ForEach-ObjectEx" -ItemType "directory" | Out-Null 
}
In this script, I want to create the folders in a different folder, D:\PS-Tutorial\ForEach-ObjectEx. To confirm that the folder is empty before I run the script, here is the screenshot…

Now, run the script

After running the script, new folders (with the names of the files in the text file) are now created in D:\PS-Tutorial\ForEach-ObjectEx.

Step 2 (Example 3): Combine Get-Content with ForEach Method

So far this guide has covered how to list the content of a text file with the Get-Content Command. I have also covered how to iterate the content of a text file with the PowerShell ForEach loop and the ForEach-Object Cdlet.

In this section, you will learn how to iterate the content of a text file with the PowerShell ForEach Method. As with the other sections, I will start this section with the syntax of the PowerShell ForEach Method.

Syntax Of PowerShell ForEach Method Command

The syntax of the PowerShell ForEach() Method Command is…

<collection of input objects>.ForEach( {
<Run the command in this block>
} )

Like all PowerShell Methods, to access the ForEach Method, enter a period after the object, followed by the word ForEach.

Then, after ForEach, enter an opening bracket, (. Next, you enter a command block {} – your commands are executed within this block.

Finally, close the ForEach Method block with a closing bracket, ).

Like the ForEach-Object command, the ForEach Method uses the temporal automatic “object in the current pipeline” variable, $_.

In the next sub-section, you will see how to use the ForEach Method.

Powershell ForEach() $_ in $files Example

The script in this section accesses the ForEach Method in the $files variable – the variable created with this command…

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt

Here is the script that uses ForEach Method to iterate the content of the text file, saved in $files variable. Then, create a folder with each item.

$files = Get-Content -Path D:\PS-Tutorial\file-with-numbers.txt
$files.ForEach( {
   New-Item -Name $_ -Path "D:\PS-Tutorial\ForEachMethodEx" -ItemType "directory" | Out-Null 
} )

In this example, I will be creating the folders in the path, D:\PS-Tutorial\ForEachMethodEx. Once again, to confirm that the folder is empty before I run the above script, here is a screenshot of the folder…

Now, run the script.

Once I ran the script, it created the folders using the information from the text file.

Conclusion

PowerShell has numerous tools you can use to iterate the content of a text file. In this article, I discussed – with examples – three constructs you can use in your PowerShell scripts.

I am confident that you now know how to use PowerShell to loop through the content of a text file.

As usual, I like to know what you think about this article. The easiest way to provide feedback to us is by responding to the “Was this page helpful?” question below.

Alternatively, you can share your thoughts with our community by leaving a comment using the “Leave a Reply” form located at the bottom of this page.

Before you go, for more PowerShell tech Itechguides, visit our Windows PowerShell How-To guide page.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

2 thoughts on “How to Iterate the Content of a Text File in Powershell”

  1. Nice work, Victor!

    I, for one, totally appreciate the time you’ve put into providing this resource for people like me–people who are looking for simple answers to complex PS problems. This helped me understand the cmdlet.

    Thank you!

    -.-jc

    Reply

Leave a comment

Send this to a friend