# question

## Loop using Where-Object

Hi I am using the following code to build a date array

`````` ##CHECK FOR DATES
if(\$dayofweek -eq "Monday")
{
for(\$i=0;\$i -le 3; \$i++)
{
\$Datearray += Get-Date \$Temp -Format yyyy-MM-dd
}
}
else
{
\$Datearray += Get-Date \$Temp -Format yyyy-MM-dd
}
``````

I then have

`````` \$OrderInfo = \$file | Where-Object {\$_."Issue Date" -eq \$td } |Select-Object "Order Number"
``````

Can I add a loop into the Where-Object clause? I only want to pull the order numbers based on the datearray

Thanks for help?

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Vote"

Hi,

If \$file is imported from a csv file you can do it like this

`````` \$dayofweek = (Get-Date).DayOfWeek
\$Datearray = @()
if(\$dayofweek -eq "Monday")
{
for(\$i=0;\$i -le 3; \$i++)
{
}
}
else
{
\$Datearray += Get-Date -Format yyyy-MM-dd
}
\$OrderInfo = \$file | Where-Object { \$_."Issue Date" -in \$Datearray } | Select-Object "Order Number"
``````

Best Regards,
Ian Xue
============================================

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Vote"

No, you can't. But it isn't necessary, either.

First, your 1st code example needs a bit of modification (declaring DateArray so line 12 creates an array, removing \$Temp from line 12 because it isn't in scope). The 2nd code example uses \$td which isn't present -- is it safe to assume that should be "\$DateArray"?

Try this (it uses a PSCustomObject in place of your file, just for testing purposes):

`````` \$dayofweek = 'Friday'
[array]\$Datearray = @()
if(\$dayofweek -eq "Monday")
{
for(\$i=0;\$i -le 3; \$i++)
{
\$Datearray += Get-Date \$Temp -Format yyyy-MM-dd
}
}
else
{
\$Datearray += Get-Date -Format yyyy-MM-dd
}
\$file = [PSCustomObject]@{
"Issue Date" = "2021-03-29"
"Order Number" = 1
}
\$OrderInfo = \$file |
Where-Object {\$dateArray -contains \$_."Issue Date"} |
Select-Object "Order Number"
``````

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.