Loop using Where-Object

Christopher Jack 1,611 Reputation points
2021-03-29T13:31:01.447+00:00

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++)
    {
        $Temp = (Get-date).AddDays(-$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?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,387 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,361 Reputation points Microsoft Vendor
    2021-03-30T06:46:12.397+00:00

    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++)  
        {  
            $Datearray += (Get-date).AddDays(-$i).ToString('yyyy-MM-dd')  
        }  
    }  
    else   
    {  
        $Datearray += Get-Date -Format yyyy-MM-dd  
    }  
    $OrderInfo = $file | Where-Object { $_."Issue Date" -in $Datearray } | Select-Object "Order Number"  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-03-29T15:18:54.377+00:00

    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++)
         {
             $Temp = (Get-date).AddDays(-$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"
    
    1 person found this answer helpful.
    0 comments No comments