Foreach powershell

In this article let’s discuss about Foreach powershell. Let’s go through the following methods without any delay.

Method 1:

ForEach($user in $users){
	Write-Output $user
}

If you are facing any issues with the above approach then try the alternative that is given below. It may look similar but have a try!

Method 2 :

$Items = "A", "B", "C"
#There are 2 ways to do this.
#The Foreach Keyword
foreach ($item in $items) {
	Write-host $Item
}
#OR By Piping 
#$_ is a dedicated varible for the object in the Pipe
$Items | Foreach-Object {
	Write-host $_
}

Hope the above solution works.

Realted Posts : How to see number of documents in mongodb collection