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!
$numbers = 1..10; ForEach($number in $numbers.Where{$_ -eq 2})Get-ChildItem -Path "C:\" | Where-Object { $_.CreationTime -ge $end -and $_.CreationTime -le $start } | ForEach-Object { Write-Host $_.Name }Where-ObjectwithGet-ChildItemrather than put it in the loop.$fileList = Get-ChildItem -Path "C:\" | Where-Object {$_.CreationTime -ge $end -and $_.CreationTime -le $start}andforeach ($file in $fileList) {. Or go the other way and use plainifs:foreach ($file in $sourcePath) { if ($file.CreationTime -ge $end -and $file.CreationTime -le $start) { Write-Host $file.Name } }. Just use whichever seems more appropriate.