Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

2 styles of foreach syntax, each of them makes something different

$
0
0

Good afternoon, 

I'm PS beginner and I'm just getting familiar with it. There is one thing I can't figure out. I supposed those two snippets will do exactly do same, but I've never been so wrong: 

<#1st: #>

Get-Process | % { if($_.WorkingSet -gt 10MB) {Write-Host $_.Name -ForegroundColor Green} else {Write-Host $_.Name -ForegroundColor Green}}

<#2nd#> $prc = Get-Process foreach ($proc1 in $prc) { if($proc1.WorkingSet -gt 10MB) { Write-Host $proc1.Name -ForegroundColor Red } else { Write-Host $proc1.Name -ForegroundColor green } }

1st one writes all processes (despite if their working set size) in Green, 2nd one writes all processes which have WS above 10MB in red, all other processes are written in green. 

Could anyone please point out difference between these two scripts? I thought they'll do exactly the same thing. 

Thank you in advance.

Stanley


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>