Advertisements

PowerShell Foreach-Object: Syntax, Parameters, Examples

-

|

Introduction

PowerShell Foreach-Object is used to iterate through a collection of items to perform a specified action on each item.

The input of Foreach-Object can be piped to the cmdlet. It may also be specified by using the InputObject parameter.

ForEach-Object Cmdlet is similar to PowerShell ForEach but it may be more convenient to use one instead of the other depending on the scripting situation.

This guide discusses the syntax and parameters of PowerShell Foreach-Object Cmdlet. It also offers some examples.

Advertisements


PowerShell Foreach-Object Syntax and Parameters

PowerShell Foreach-Object Syntax

The syntax of PowerShell Foreach-Object is:

<input objects> | Foreach-Object {

<Perform specified actions on each of the input objects>

}

<input objects> represents the objects fed into the Foreach-Object Cmdlet.
<Perform specified actions on each of the input objects> is an action to be performed on each input object.

Foreach-Object Examples

Here are some examples and applications of PowerShell Foreach-Object Cmdlet.

PowerShell Foreach-Object Example 1

In the first example, I will use Foreach-Object to divide a set of numbers. To do this I will pipe the numbers (the input objects) to Foreach-Object. Then divide the input from pipeline variable ($_) with the value I want to divide the numbers with.

Here is the example in PowerShell

10, 20, 30 | ForEach-Object  {$_/10}

This is a very basic example but it demonstrates how ForEach-Object works.

powershell foreach-object

The numbers 10, 20 and 30 separated by commas (,) are input objects. When you pipe these numbers (objects) into ForEach-Object, it iterates through each value and perform the task within {} block.

I this example, the pipeline input variable ($_) replaces the 10, 20 and 30. The result are 1, 2 and 3…

PowerShell Foreach-Object Example 2

The second example is more complex than the first. This example will determine the size of files in a folder.

To determine the size all all files in the path C:\O365, use this command:

Get-ChildItem "C:\O365" | ForEach-Object {
If (!$_.PSIsContainer)
{$_.Name; $_.Length /1024; " "

}
}

The command Get-ChildItem “C:\O365” is similar DIR “C:\O365” command.

powershell foreach-object


The result of Get-ChildItem “C:\O365” is then piped into PowerShell Foreach-Object command.

The second requirement is to return non-folders. To do this we introduce the IF Statement into the Foreach-Object block.

Here is the IF statement part of the command…

   If (!$_.PSIsContainer) 
{$_.Name; $_.Length /1024; " "

}

The command says – If and object is NOT (NOT is represented by “!”) a folder (represented by “$_.PSIsContainer”), return the object in the IF block ({}) then perform the following actions:

  1. Return the name of the object (file name) – $_.Name
  2. Divide the value of the $_.Length (Size) by 1024 to convert it to KB. Then display the value
  3. To add a space between the results, add a space – represented by ” “

Here is the result of the command in PowerShell..

To round the values of the size, you can modify the script as shown below…

Get-ChildItem "C:\O365" | ForEach-Object {
  $Size =  $_.Length/1024 
   If (!$_.PSIsContainer) 
  {$_.Name; [math]::Round($Size); " "   }
 }   

The last command removes the decimals in the size of the files..

Conclusion

PowerShell Foreach-Object is very important Cmdlet to add to your scripting toolkit. I hope this guide simplified the syntax and applications of this important Cmdlet.

If you have a question, feedback or comment use the “Leave a Reply” form found at the end of this page.

For more Windows guides, visit our Windows How To page.


LEAVE A REPLY

Please enter your comment!
Please enter your name here

LATEST DEALS

Exclusive Student Offer_Save 10% on selected Surface devices

FEATURED POSTS

How to Change Administrator Name in Windows 10 (4 Methods)

How to Change Administrator Name in Windows 10

Introduction This guide demos how to change administrator name in Windows 10. There could be...
How to Setup WhatsApp Auto Reply in an Android Phone

How to Setup WhatsApp Auto Reply in an Android Phone

Introduction This guide demos how to setup Whatsapp auto reply in an Android phone. Steps...
How to Set Out of Office Auto Reply in Outlook, Outlook.com and Gmail

How to Set Out of Office Auto Reply in Outlook, Outlook.com and Gmail

Introduction This guide demos how to set out of office auto reply in Outlook, Outlook.com and Gmail.
How to Take Ownership of Folder in Windows 10

How to Take Ownership of Folder in Windows 10 (2 Methods)

Introduction This guide demos 2 methods to take ownership of folder in Windows 10. Options to...
How to Install Windows Server 2019 from USB

How to Install Windows Server 2019 from USB

Introduction This guide demos how to install Windows Server 2019 from USB. Steps to Install...

ADVERTISEMENTS

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...

BEST OF ITECHGUIDES

Chrome ADMX

How to Download and Install Google Chrome Administrative Template

Introduction To configure Google Chrome settings via Group Policy you require Chrome ADMX. However, as you would expect by...
avast vpn

Avast VPN: How to Install, Setup and Stay Safe Online

What is Avast Secureline VPN? Avast VPN is a VPN (Virtual Private Network) software that allows you to...
Active Directory Domain Services

Active Directory Concepts & Administration

Introduction Active Directory (AD) is a Microsoft directory service that stores information about objects in a network. AD also...
how to make Pivot Table

How to Make a Pivot Table in Excel

Introduction A Pivot Table allows you to analyze, summarize and calculate large data to help find relationships. With a...
How to Set Out of Office Auto Reply in Outlook, Outlook.com and Gmail

How to Set Out of Office Auto Reply in Outlook, Outlook.com and Gmail

Introduction This guide demos how to set out of office auto reply in Outlook, Outlook.com and Gmail.

RECENT POSTS

C:\G-Drive\Work Tools\Products Portal\1. New Business\2. Content Sites\1. iTechGuides.com\Posts\1. HOW TO\Microsoft\MS Office\Microsoft Word\how to add more rows to a table in word

How to Add More Rows to a Table in Word and Google Docs

Introduction This guide demos how to add more rows to a table in Word. It also has a...
How to Create a Bar Chart in Excel and Google Sheets

How to Create a Bar Chart in Excel and Google Sheets

Introduction This guide demos how to create a bar chart in Excel and Google Sheets. A bar chart is...
How to Change Administrator Name in Windows 10 (4 Methods)

How to Change Administrator Name in Windows 10

Introduction This guide demos how to change administrator name in Windows 10. There could be...
How to Create a Pie Chart in Excel and Google Sheets

How to Create a Pie Chart in Excel and Google Sheets

Introduction This guide demos how to create a pie chart in Excel and Google Sheets. A pie chart is...

iTechGuides’ Top 10 Online Games

The gaming world is getting bigger day by day. Many people are getting engaged to play online games worldwide.

How to Open Local Security Policy in Windows 10 (5 Methods)

Introduction This guide demos how to open local security policy in Windows 10. Options to...

How to Change Network from Public to Private in Windows 10

Introduction This guide demos 2 methods to change network from public to private in Windows 10.
How to Disable Touchpad in Windows 10 for Dell or HP Laptop

How to Disable Touchpad in Windows 10 for Dell or HP Laptop

Introduction This guide demos how to Disable Touchpad in Windows 10 for a Dell or HP laptop.

How to Change Account Picture in Windows 10 (2 Methods)

Introduction This guide demos 2 methods to change account picture in Windows 10. Windows 10...
How to Create Xbox Live Account

How to Create Xbox Live Account (2 Methods)

Introduction This guide demos 2 methods to create Xbox Live account. Options to Create Xbox...

ADVERTISEMENTS

MUST READ

How to Fix "not recognized as an internal or external command" in Windows 10

How to Fix “not recognized as an internal or external command” in Windows 10

Introduction Recently I ran a "NETDOM JOIN" command but received "NETDOM is not recognized as an internal or external...
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...
system restore windows 10

How to Enable and Use System Restore in Windows 10

Introduction System restore in windows 10 is a very important tool that is probably ignored by most users. But...
Disable Windows Defender in Server 2016 from Server Manager

How to Disable Windows Defender in Windows Server 2016 (3 Methods)

Introduction This guide demos 3 methods to disable Windows Defender in Server 2016. Options to...

How to Set Homepage in Microsoft Edge (Edge Settings or Group Policy)

Introduction You can set homepage in Edge (Microsoft Edge) directly from Settings. This method is suitable for individual or...

LATEST DEALS

Exclusive Student Offer_Save 10% on selected Surface devices

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