How to arrange values of different arrays in a tabular form?

123 views Asked by At

I have three arrays such as Sender, Recipient and Subject; I need to arrange them in tabular form as below.

Sender              Recipient             Subject
[email protected]        [email protected]           xxx

Here is my code

dir|ForEach-Object { 
  $Name=$_.Name 
  foreach ($N in $Name) { Import-Csv $N|` 
    Foreach { 
      $Sender+=$_.SenderAddress 
      $Recipient+=$_.RecipientAddress 
      $Subject+=$_.Subject 
    } 
  } 
}

Can anyone look into this?

Regards, Kiran

2

There are 2 answers

7
Mathias R. Jessen On BEST ANSWER

If you have multiple perfectly aligned arrays, you can easily zip them together with a for loop:

$Sender = @('[email protected]','[email protected]')
$Recipient = @('[email protected]','[email protected]')
$Subject = @('Overdue invoices','Receipts')

$mailObjects = for($i = 0; $i -lt $Sender.Count; $i++){
    New-Object psobject -Property @{
        Sender = $Sender[$i]
        Recipient = $Recipient[$i]
        Subject = $Subject[$i]
    }
}

Now you can use Format-Table:

PS C:\> $mailObjects |Format-Table
Sender               Recipient             Subject
------               ---------             -------
[email protected] [email protected] Overdue invoices
[email protected] [email protected]   Receipts
0
AudioBubble On

If I'm interpreting your code right, you are referring to columns of csv files not arrays. A select of the wished properties should suffice after importing, the new shorter property names could be achieved with a [pscustomobject].

Suppose there are:

> Get-Content first.csv
"othercol","SenderAddress","RecipientAddress","Subject"
"","[email protected]","[email protected]","Overdue invoices"
"","[email protected]","[email protected]","Receipts"

and:

>Get-Content second.csv:
"SenderAddress","RecipientAddress","Subject","othercol"
"[email protected]","[email protected]","Overdue deliveries","xyz"
"[email protected]","[email protected]","Orders","abc"

This one liner:

foreach ($File in (gci *.csv)){Import-Csv $File|Select SenderAddress,Recipientaddress,Subject}

will output:

SenderAddress        RecipientAddress      Subject
-------------        ----------------      -------
[email protected] [email protected] Overdue invoices
[email protected] [email protected]   Receipts
[email protected] [email protected]  Overdue deliveries
[email protected]  [email protected] Orders

This more verbose version:

ForEach ($File in (Get-ChildItem *.csv)){
    Import-Csv $File | Select-Object SenderAddress,Recipientaddress,Subject |
        ForEach-Object{[pscustomobject] @{Sender    = $_.SenderAddress
                                          Recipient = $_.Recipientaddress
                                          Subject   = $_.Subject}
        }
}

yields this output:

Sender               Recipient             Subject
------               ---------             -------
[email protected] [email protected] Overdue invoices
[email protected] [email protected]   Receipts
[email protected] [email protected]  Overdue deliveries
[email protected]  [email protected] Orders