How to Iterate the Content of a Text File in Powershell in 2 Easy Ways

-

|

Introduction

If you have a text file with data you wish to use, you can use PowerShell Get-Content to list the contents of the file. Then use PowerShell ForEach to loop through the file line by line.

An example could be a list with Active Directory user names in a text file. You can use the usernames from the text file to search for the users in AD, then update other user information in AD.

Another simple application of PowerShell ForEach is to loop through the content of a file and append a text to each line. More in examples section of this guide.

In this quick guide, I present 2 easy ways you can do this using Get-Content, ForEach loop, and ForEach-Object.

Advertisement

How to Use PowerShell ForEach Loop and Get-Content to Iterate Through a File

Before discuss how to use ForEach and Get-Content to loop through a file in Powershell, let me show you the Syntax of both commands:

Syntax of ForEach:

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

In the context of this guide, here is what each parameter means:

$files are the contents of the text file.
$file is used to take one line at a time and perform the command in the block, {}.
{} contains the command that is run on each line in the file.

Here is the simplified Syntax of Get-Content cmdlet:

Get-Content -Path <path to text file>

We can combine both command to product the syntax of powershell foreach $file in $files below:

ForEach ($file in (Get-Content -Path <path to text file>)) {Run the command in this block}

Powershell ForEach $file in $files Example

The first step in iterating through a text file with Powershell ForEach is to get the contents of the file with Get-Content. Here is the command:

Get-Content "C:\PS\file with numbers.txt"

And the result in PowerShell:

foreach in file powershell - Get-Content "C:\PS\file with numbers.txt"

The next step is to add Get-Content “C:\PS\file with numbers.txt” into the ForEach $file in $files loop. Here is the command:

ForEach ($file in (Get-Content "C:\PS\file with numbers.txt")) {Run the command in this block}

Finally, I will add a command into the command block, {}. In this example, I want PowerShell to display “This is line xx”.

Here is the command I will include in the {} block.

Write-Host "This is line $file in the text file"

And now the final ForEach ($file in $files) command:

 ForEach ($file in (Get-Content "C:\PS\file with numbers.txt")) {

Write-Host "This is line $file"

}

Here is the result in PowerShell ISE:

To make it easy to understand the above result, here is the result of the Get-content part.

So, what the ForEach ($file in $files) command command did was to append “This is line ” at the beginning of each $file. $file being each line item in the text file.

How to Use PowerShell ForEach-Object and Get-Content to Iterate Through a File

Another way you can loop through the content of a text file is to use the ForEach-Object cmdlet. This time, you will pipe the output of Get-Content to ForEach-Object cmdlet.

Continuing with the previous example, here is the command:

Get-Content "C:\PS\file with numbers.txt" | ForEach-Object {
Write-Host "This is line $_"
}

This is the result of the above command in PowerShell ISE:

foreach in file powershell - ForEach-Object

There is one difference between the above command and the ForEach ($file in $files) command. While the ForEach command assigns each content of the text file a variable, $file during the iteration, the ForEach-Object command assigns a variable, $_

In PowerShell “$_” is the variable for the current value in the pipe line. In this example, the output of Get-Content is stored in the variable, $_
Advertisement

Conclusion

You arrived at this guide because you likely searched for “foreach in file powershell” or its variant. I hope I have been able to answer your question on how to iterate through the content of a file with either ForEach ($file in $files) in PowerShell or ForEach-Object.

If you have any question use the “Leave a Reply” form at the end of the page. Alternatively, you could share your experience using PowerShell ForEach to work with files.

Other Helpful Guides

Additional Resources and References

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 19H2 Preview Build

Introduction If you are a member of Windows 10 Insider Program you can install Windows 10 19H2 Preview Build....

How to Sign in to Windows 10 with a Microsoft Account

Introduction When you installed Windows 10 you may have created and signed in with a local account. You can...
Disable IPv6 in Windows 10

How to Disable IPv6 in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to disable IPv6 in Windows 10: Disable IPv6 from...

Advertisement

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

Advertisement

BEST OF ITECHGUIDES

powershell replace

PowerShell Replace “Method” and “Operator”: Syntax, Examples

Introduction PowerShell Replace can be used to replace character strings, texts or special characters. PowerShell has a Replace...

Excel Substring: How to Get (Extract) Substring in Excel

Introduction An Excel substring is a portion of a text in a cell. For instance, if a cell contains...
How to Make a Pivot Table in Google Sheets

How to Make a Pivot Table in Google Sheets

Introduction You can make a Pivot Table in Google Sheets to simplify analysis of complex data. A Pivot Table...
Websites Like Craigslist - featured

Top 5 Websites Like Craigslist to Buy and Sell Stuff Online

Looking for websites like Craigslist? In this guide I offer my top 5 Craigslist alternative websites where you can buy and sell...
ForEach-Powershell

PowerShell ForEach: Syntax, Parameters, Examples

What is PowerShell ForEach? PowerShell ForEach (ForEach PowerShell) is a PowerShell construct used in iterating through values in a...

RECENT POSTS

How to Enable Hyper-V in Windows 10 (3 Methods)

How to Enable Hyper-V in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Hyper-V in Windows 10. To install Hyper-V...
RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

Introduction Starting from October 2018 (1809) update, RSAT Tools became part of Windows 10. From this version of Windows...

How to Enable RSAT for Active Directory in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Active Directory in Windows 10. It is not exactly enabling "Active...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 1909 (19H2) Preview Build

Introduction Windows 10 1909 Preview is available for Windows Insiders. Made available early September, 2019 you have to be...
How to Install RSAT in Windows 10 (3 Methods)

How to Install RSAT in Windows 10 (3 Methods)

Introduction This guide demos how to Install RSAT in Windows 10. Starting with Windows 10...

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...
Configure Map Network Drive with Group Policy

Map Network Drive in Windows 10 with Group Policy

Introduction This guide demos how to map network drive with group policy. This guide is...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Download Windows 10 ISO with Media Creation Tool

How to Download Windows 10 ISO with Media Creation Tool

Introduction This guide demos the steps to download Windows 10 ISO. You can download Windows 10 ISO with Media...

How to Install Windows 10 from Network Boot (Via WDS Server)

Introduction This guide demos how to install Windows 10 from network boot. The steps discussed in...

Advertisement

MUST READ

How to Make a Pivot Table in Google Sheets

How to Make a Pivot Table in Google Sheets

Introduction You can make a Pivot Table in Google Sheets to simplify analysis of complex data. A Pivot Table...
Group Policy, GPResult, Group Policy Object and RSoP

Group Policy, Group Policy Object and RSoP Explained

Introduction This guide gives an overview of Group Policy, RSoP (Resultant Set of Policy) and Group Policy Objects.

Gmail Search by Date: How to Search Gmail by Date

Introduction You can search Gmail by date for emails sent during certain time period. You could also search Gmail...
server 2016 not downloading updates from wsus

Fix for Server 2016 not Downloading Updates from WSUS

Introduction If you experience Server 2016 not downloading updates from WSUS, it is likely caused by a known bug....
Configure Map Network Drive with Group Policy

Map Network Drive in Windows 10 with Group Policy

Introduction This guide demos how to map network drive with group policy. This guide is...

By using this website you agree to accept our Privacy Policy and Terms & Conditions