0

Just curious if it is possible to use the Where Method in combination with the ForEach loop?

$numbers = @(1..10)

ForEach($number in $numbers.Where({$number = 2})){

    Write-Host "the number is $number"

}

Ok figured it out

Turns out you can also perform this with Get-ChildItem using the following

$start = Get-Date
$end = (Get-Date).AddDays(-1)
$sourcePath = Get-ChildItem -path "C:\"

ForEach($file in ($sourcePath.where({$_.CreationTime -ge $end -and 
$_.CreationTime -le $start}))){

    Write-Host $file.Name

}

Thanks!

6
  • -eq not =, $_ not $number $numbers = 1..10; ForEach($number in $numbers.Where{$_ -eq 2}) Commented Dec 11, 2020 at 20:34
  • Is there any reason not to use pipes? Get-ChildItem -Path "C:\" | Where-Object { $_.CreationTime -ge $end -and $_.CreationTime -le $start } | ForEach-Object { Write-Host $_.Name } Commented Dec 11, 2020 at 22:35
  • You can get slightly better performance by avoiding the pipeline. Probably immaterial in this case. Commented Dec 12, 2020 at 10:48
  • @Dabombber the reason that I can’t use the pipe is because the actual code that this goes in is in a nested loop where I need to use BREAK to get out of that inner loop. However, when using break with pipes, apparently it stops the entire pipe which is why I have to use ForEach so that the outer loop will continue. I was just providing a super super basic example so I could grasp the syntax for the concept to apply to my actual code. Commented Dec 12, 2020 at 13:03
  • @N.Ha Yea, that's an annoying way to get caught out. It may make your code more readable if you combine the Where-Object with Get-ChildItem rather than put it in the loop. $fileList = Get-ChildItem -Path "C:\" | Where-Object {$_.CreationTime -ge $end -and $_.CreationTime -le $start} and foreach ($file in $fileList) {. Or go the other way and use plain ifs: foreach ($file in $sourcePath) { if ($file.CreationTime -ge $end -and $file.CreationTime -le $start) { Write-Host $file.Name } }. Just use whichever seems more appropriate. Commented Dec 13, 2020 at 2:40

1 Answer 1

1

It's possible, you do get a nasty level of nesting compared with doing it on the same line as $numbers though.

$numbers = @(1..10)

ForEach($number in ($numbers.Where({$_ -eq 2}))){

    Write-Host "the number is $number"

}
Sign up to request clarification or add additional context in comments.

3 Comments

so follow up question. Will the Where method work if trying to use it with the Get-ChildItem? For instance $path = "C:\" ForEach($file in $path.Where($_.CreationTime -ge $end -and $_.CreationTime -le $start){ Write-Host "something" }
Yes you can use the .Where method on the results of Get-Childitem, or any other array. It makes no sense to use it on a string like "C:\" in your example.
Kind of a weird situation I was in but I was able to figure out the syntax. Thank you so much! Edited original question :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.