How to Use Powershell Get-Content and ForEach With Examples

-

|

Introduction

You can combine Powershell Get-Content and ForEach Loop in so many Windows automation tasks.

Here is how it works: list the contents of the text file with Get-Content. Then perform a specific task on each item in the text file by passing them through the ForEach statement loop.

This guide demos how to use Powershell Get-Content and ForEach to iterate the contents of a file.

The guide is broken down into 4 sections:

  1. Get-Content Syntax and Parameters
  2. ForEach Syntax and Parameters
  3. How to Use Powershell Get-Content and ForEach
  4. Powershell Get-Content and ForEach Example
Advertisement

1
Get-Content: Syntax and Parameters

The Get-Content Cmdlet gets the content of the item at the specified location.

Here is the simplified syntax of this Cmdlet:

Get-Content [-Path] <String[]> 

Get-Content has a lot more parameters. I specified the commonly used parameters.

Use the -Path parameter to specify the full path (<String[]>) to the text file, including the extension.

You do not have to specify the -Path parameter before you enter the path to the text file.

As an example to list the contents of the text file in this path “C:\PS\file with UserNames.txt”, use the command below:

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

The command returns the contents of the text file:

To add the result to a variable, use this command

$UserNames = Get-Content "C:\PS\file with UserNames.txt"

The result is not displayed on the console because it is stored in the $UserNames variable.

Advertisement

2
ForEach Statement: Syntax and Parameters

A PowerShell ForEach Statement performs an operation against each item in a collection of input objects. For the purpose of this guide, the operation will be performed against the items in a text file.

The syntax of ForEach Statement is:

ForEach ($item in $collection) {Perform a task based on a powershell command}  

As expected, the syntax will begin with ‘ForEach’. It is then followed by a by brackets ‘()’; and finally ‘{}’ block.

To continue with the Get-Content example, to use ForEach to loop through the contents stored in the $UserNames use this command:

ForEach ($User in  $UserNames ) {

#Perform an operation

}

I will continue with this example in section 3rd and 4th sections of this guide.

We have a full guide covering ForEach. To learn more about this powerful PS statement, click ForEach Statement.

3
How to Use Powershell Get-Content With ForEach

In section 2, I gave this command as an example:

ForEach ($User in $UserNames) {

#Perform an operation

}

If you recollect, $UserNames variable contains the result of this command:

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

Which is:

powershell get-content foreach

To confirm that $UserNames saved the values, type the variable in PowerShell. Then press enter:

$UserNames 

Here they are!

powershell get-content foreach

Now lets go back to the command that combined Powershell Get-Content and ForEach

ForEach ($User in $UserNames) {

#Perform an operation

}

Here is this command simplified in plain English:

“ForEach ($User in $UserNames)” simply tells PowerShell to take the first item in the list ($UserNames) and make it the variable $User. Then perform the operation in the {} block on the item stored in the $User variable.

It then circles back and takes the next item in $UserNames and stores it in $User variable. Then perform the operation within the {} block again.

ForEach will keep doing this until the last item in the $UserNames variable. Then it will terminate.

4
Powershell Get-Content and ForEach Example

Advertisement

In the last section, I explained how ForEach takes each result of Get-Content and performs an operation on the item.

Here is the command:

ForEach ($User in $UserNames) {

#Perform an operation

}

Your next question may be “what type of operation can I perform in the {} part of ForEach”. The answer is: any task that PowerShell can perform!

Here are some examples:

  • Delete the contents of the file that meet a particular criteria
  • Replace the contents of the file
  • Create or modify AD Users with the contents of the file

In my final example in this guide, I will simply write a sentence with each item in the text file. The statement will be ‘$User is in the text file’.

Here is the script

$UserNames = Get-Content "C:\PS\file with UserNames.txt"

ForEach ($User in $UserNames) {

Write-Host "$User is in the text file"

}

…and the result in PowerShell ISE:

Conclusion

The examples in this guide are intentionally over simplified. The aim is to make it easy for you to understand how to use Powershell Get-Content and ForEach. I hope I succeeded!

Have a question or comment? Use the “Leave a Reply” form found at the end of this page.

Want more PowerShell guides? Visit our PowerShell Windows PowerShell page.

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

what model iphone do i have

What iPhone Do I Have: How to Check Your iPhone Model

Introduction Looking for your iPhone model? It is fairly straightforward. This guide will take you through the steps. To...
Configure Group Policy for Windows Updates Server 2016 (WSUS Server 2016)

How to Configure Group Policy for WSUS in Windows Server 2016

Introduction This guide demos how to configure Group Policy for Windows Updates Server 2016 (WSUS Server 2016).
routing and remote access windows server 2016

Routing and Remote Access in Windows Server 2016 (Part 1): Install Remote Access Role

Introduction This is the first of a 4-part "Routing and Remote Access in Windows Server 2016" series.
windows deployment services

How to Setup Windows Deployment Services in Windows Server 2016

Introduction This guide demos how to setup WDS Server 2016. WDS (Windows Deployment Services) is a...
RAID 5 vs RAID 6

RAID 5 vs RAID 6: Differences, Benefits and Disadvantages

What is RAID 5 vs RAID 6? RAID 5 and RAID 6 uses striping with distributed parity technique. However,...

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

rdp an internal error has occurred server 2016

How to Fix “RDP an Internal Error has Occurred” in Server 2016

Introduction "RDP an internal error has occurred server 2016" may be fixed by: Performing physical...
RAID 5 vs RAID 6

RAID 5 vs RAID 6: Differences, Benefits and Disadvantages

What is RAID 5 vs RAID 6? RAID 5 and RAID 6 uses striping with distributed parity technique. However,...
Powershell Function

PowerShell Function: Syntax, Parameters, Examples

Introduction There are two types of PowerShell Functions, basic and advanced. A basic PowerShell Function is a list of...
windows update 0x8024401c

How to Fix Windows Update Error 0x8024401c in Windows 10

Introduction Received Windows Update 0x8024401c error in Windows 10? You have done this and done that yet, the error...
NTFS Permissions with Share Permissions

NTFS Permissions with Share Permissions Compared

Introduction Combing NTFS permissions with share Permissions increases folder security within a network. Every Windows Administrator should have a...

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