Jason Vickers Jason Vickers - 1 month ago 5x
PowerShell Question

Get-Date inside Where or Where-Object

I have this

$web = Get-SPWeb "http://sharepoint/mysubsite"
$list=$web.Lists["Scanned Documents"]
foreach ($item in $list.Items | where {$_.Indicator -ne "Expired"}) {
write-host $item[“Expiry Date”];
$olddate = Get-Date -Date $item[“Expiry Date”]
$Now = Get-Date
if ($olddate -lt $Now) {
$item[“Indicator”] = "Expired";
} else {
$item[“Indicator”] = " ";

which works fine - but the list could be really really big, so wanted something a bit more like this.

foreach ($item in $list.Items | Where-Object { ($_.Indicator -ne "Expired") -and (Get-Date -Date $_."Expiry Date" -lt $Now) }) {
#you get the idea

But I cannot get it working - complains about using
inside the

I tried

foreach ($item in $list.Items | Where-Object { ($_.Indicator -ne "Expired") -and $_."Expiry Date" -lt $Now }) {
#you get the idea

which executes, but brings back all the items not just the expired dates.


In the fragment

 ... (Get-Date -Date $_."Expiry Date" -lt $Now) ...

the -lt is considered a parameter to the Get-Date cmdlet. To make it explicit where the cmdlet's parameter list ends just wrap it in another pair or parentheses like this

 ... ((Get-Date -Date $_."Expiry Date") -lt $Now) ...