TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell ForEach-Object

PowerShell ForEach-Object with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

PowerShell ForEach-Object

The ForEach-Object in PowerShell executes a block of statements against each item in a collection of input objects. These objects are passed through the pipeline or specified by using the -InputObject parameter.

With the Starting of PowerShell version 3.0, following are the two different ways for constructing the ForEach-Object command:

  1. Operation Statement
  2. Script Block

Syntax

ForEach-Object 
[-MemberName] <String> 
[-ArgumentList <Object[]>] 
[-Confirm] 
[-InputObject <PSObject>] 
[-WhatIf]
[<CommonParameters>]

ForEach-Object 
[-Process] <ScriptBlock[]> 
[-Begin <ScriptBlock>] 
[-Confirm] 
[-End <ScriptBlock>] 
[-InputObject <PSObject>] 
[-RemainingScripts <ScriptBlock[]>] 
[-WhatIf] 
[<CommonParameters>]

Parameters

Following are the different parameters used in the ForEach-Object cmdlet:

-MemberName

This parameter is used to specify the method for calling and the property to get.

-ArgumentList

This parameter is used to specify the array of arguments for calling a method.

-Confirm

This parameter prompts you a confirmation before executing the cmdlet.

-InputObject

This parameter is used to specify the input objects. The cmdlet executes the block of script or the operation statement on each input object. When we use this parameter in the ForEach-Object, the value of this parameter is treated as a single object.

-WhatIf

This parameter is used to display what would happen if the cmdlet executes. The cmdlet will not execute.

-Process

This parameter is used to specify the operation, which is performed on each input object. Enter the script block, which describes the operation.

-Begin

This parameter is used to specify the script block, which executes before the cmdlet processes any input object.

-End

This parameter is used to specify the script block, which executes after the cmdlet processes all the input objects.

-RemainingScripts

This parameter is used to specify all those script blocks, which are not taken by the -Process parameter.

Example: Access the files and folders from the D: drive

PowerShell ForEach-Object

In this example, we use the pipe (|) sign, which is used to pass the output of get-childitem to the ForEach-Object command. And the write-host is used to display that values.

In the command, $_ is a special variable that processes any value which is passed through the pipe.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf