IT Questions and Answers :)

Tuesday, February 25, 2020

In PowerShell, a built-in method of performing iteration is foreach. You can also use foreach later in the pipeline (e.g. Get-Process | foreach {...}). When it appears in the pipeline like this, which of the following is foreach?

In PowerShell, a built-in method of performing iteration is foreach. You can also use foreach later in the pipeline (e.g. Get-Process | foreach {...}). When it appears in the pipeline like this, which of the following is foreach?

  • An alias
  • A cmdlet
  • A method
  • A function 

 
In PowerShell, a built-in method of performing iteration is foreach. You can also use foreach later in the pipeline (e.g. Get-Process | foreach {...}). When it appears in the pipeline like this, which of the following is foreach?

EXPLANATION

When called at the beginning of the line, foreach is a language command built in to PowerShell that allows you to iterate though items in a collection.  When it appears later in the pipeline, however, such as with the example above, it behaves differently in a few very important ways, such as how it works with the collection.
This is because the language command "foreach" only works at the beginning of the pipeline; anywhere else, and you are actually using the built in alias "foreach" which is defined as the cmdlet "ForEach-Object".
about_Foreach on MSDN: https://msdn.microsoft.com/en-us/powershell/reference/4.0/microsoft.powershell.core/about/about_fore...
ForEach-Object on MSDN: https://msdn.microsoft.com/powershell/reference/4.0/microsoft.powershell.core/ForEach-Object
Share:

0 comments:

Post a Comment

Popular Posts