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.

  • Was this post Helpful?
  • YesNo

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Create a Business Facebook Page without a Personal Account

How to Create a Business Facebook Page without a Personal Account

This Itechguide teaches how to create a business Facebook page without a personal account. There could be a...
How to Wrap Text in Google Sheets

How to Wrap Text in Google Sheets (from a PC or the Apps)

This Itechguide teaches you how to wrap text in Google Sheets. The guide offers steps to wrap text in Google Sheets from...
How to Change Birthday on Facebook

How to Change Birthday on Facebook (from the App or PC)

This Itechguide teaches you how to change birthday on Facebook. The guide contains steps to change birthday on Facebook from the Facebook...
Best Thrillers on Netflix

The 10 Best Thrillers on Netflix

The thrillers on Netflix is quite a large number of movies. Knowing the best thrillers to watch can be a challenge.

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

Skype can't connect

How to Fix Skype Can’t Connect Error on Windows 10

Introduction If you receive "Skype Can't Connect" Error on Windows 10, it may be caused by a number of...
the-user-profile-service-failed-the-logon-featured

How to Fix “The User Profile Service Failed the Logon” in Windows 10

Introduction "The user profile service failed the logon Windows" error is likely to occur after an upgrade to Windows...
Windows defender blocked by Group Policy

Windows Defender Blocked by Group Policy [Fixed]

Introduction If you receive the error message Windows Defender is blocked by group policy, your computer may have been...
Cortana Search not Working in Windows 10 [Fixed]

Cortana Search not Working in Windows 10 [Fixed]

Introduction This guide offers the fix for Cortana search not working in Windows 10. The reason for Cortana search...
How to Block People on Facebook

How to Block People on Facebook (from Facebook App or PC)

Introduction This guide demonstrates how to block people on Facebook. When you block someone, that...

ADVERTISEMENTS

RECENT POSTS

How to Logout of Amazon App

How to Logout of Amazon App (from Android, iPhone or Windows 10)

This Itechguide teaches you how to logout of Amazon app. The guide offers steps to logout of Amazon app for Android, for...
How to Turn off YouTube Notifications

How to Turn off YouTube Notifications (PC or YouTube Apps)

This Itechguide teaches you how to turn off YouTube notifications. The guide offers steps to turn off YouTube notifications from a PC...
How to Change YouTube Thumbnail

How to Change YouTube Thumbnail (PC or YouTube Studio App)

This Itechguide teaches you how to change YouTube thumbnail. The guide offers steps to change YouTube thumbnail from a PC or...
How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Change Shipping Address on Amazon

How to Change Shipping Address on Amazon

This Itechguide teaches you how to change shipping address on Amazon. The guide offers steps to change shipping address on Amazon:

MUST READ

iPad Won't Charge

iPad Won’t Charge? How to Fix It

Why Won't iPad Charge? If your iPad won't charge it could be for a number of reasons. Here are...
this accessory may not be supported

This Accessory May Not be Supported [Fix]

What Does This Accessory May Not be Supported Mean? You may receive "this accessory may bot be supported" error...
How to Block People on Facebook

How to Block People on Facebook (from Facebook App or PC)

Introduction This guide demonstrates how to block people on Facebook. When you block someone, that...
How to Create a Business Facebook Page without a Personal Account

How to Create a Business Facebook Page without a Personal Account

This Itechguide teaches how to create a business Facebook page without a personal account. There could be a...
Absolute Reference vs Relative Reference Excel

Absolute Reference vs Relative Reference Excel: Quick Guide

What is Absolute Reference in Excel? Absolute reference in excel allows you to reference a particular row or column...

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