I have tried below but not getting any result back
Not sure if i’m doing this well.
Can i filter in the foreach or in my if statement
Thanks in advance
[DateTime] $CreatedDate = $item["Created"]
$convertedCreatedDate = $CreatedDate.ToString("yyyy-MM-dd")
$today = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
foreach ($item in $list.items | where {$convertedCreatedDate -eq $today}) {
if ($list.items | where {$convertedCreatedDate -eq $today})
{
Write-Host $item["Created"]
}
Write-Host $item["Created"]
}
You can use a complex expression in your
foreachas you’re doing above. I would wrap it in a@()to make the code a bit more readable and to ensure the result is an array (either length 0, 1 or n) e.g.:You can also simplify you’re date testing by using the
Dateproperty on a DateTime e.g.:You can also put a complex expression within an
ifstatement condition but PowerShell will only evaluate whether the statement is$trueor$false. PowerShell does lots of coercion to try to take something like:And convert that to a boolean. Essentially if the pipeline evaluates to a non-empty result, the result is
$trueotherwise it is$false. This is probably not what you intended.