Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 97
Reply with quote  #1 
I have an array that I want to output to the console.

When I run:

Write-Host $array

The array outputs on the same line


When I run just the name of array, $array, the output prints on different line.


Ideally I want the best of both worlds - print the array on different lines using Write-Host.  I want to use Write-Host because of the -ForegroundColor paramater.

Is there a way to make that happen?
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 377
Reply with quote  #2 
Quote:
Originally Posted by meloao
I have an array that I want to output to the console.

When I run:

Write-Host $array

The array outputs on the same line


When I run just the name of array, $array, the output prints on different line.


Ideally I want the best of both worlds - print the array on different lines using Write-Host.  I want to use Write-Host because of the -ForegroundColor paramater.

Is there a way to make that happen?

In PowerShell, the answer to that question is always "yes" [biggrin]

$array | Foreach-Object { Write-Host $_ -ForegroundColor Green }

Or, should the array be extremely large, this will be faster:

$array.Foreach({ Write-Host $_ -ForegroundColor Green })

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 97
Reply with quote  #3 
$array | Foreach-Object { Write-Host $_ -ForegroundColor Green }


That comes up on the same line for me :-P
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 377
Reply with quote  #4 
Can't be. Your array may not be an array then but already concatenated.
Care to post a screenshot of

$array.Count
$array.GetType()
$array | Foreach-Object { Write-Host $_ -ForegroundColor Green }

?

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 97
Reply with quote  #5 
Hey! You're right!  I was using the wrong variable.  when I used the code you provided above, it works!!! Thanks so much!
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 97
Reply with quote  #6 
Ok, so it does display the array vertically, but I would like the array to include the header information from the CSV.  So for example,

EMPLOYEE_ID: 000
EMP_DEP_ : dept
.
.
.
.
.
.


I have the header names in the CSV file defined  

 Import-Csv $File -Delimiter $fileDelimiter -Header $Header 
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 97
Reply with quote  #7 
Ok, getting closer.

I have 2 arrays that I want to output in table format

$header = $first_name, $last_name, $department, $location, $title

$person = $tom, $brady, $finance, $homeoffice, $CIO


I would like to output the arrays like so:

$first_name:   $tom
$last_name:   $brady
$department:  $finance
$location:       $homeoffice
$title:              $CIO

In doing some research, it looks like this is called multi-dimensional arrays
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 377
Reply with quote  #8 
OK, so you have a CSV that's looking like this:

First;Last;Department;Location;Title
Tom;Brady;Management;Houston;CIO
Jack;Shitt;Accounting;Anchorage;Bean Counter
...

and you would like to read it via Import-CSV and then output in a Format-List like manner but using Write-Host for colouring, correct?

In this case, the structure describing one single employee is NOT an array, but a PSCustomObject (as could be seen on you screenshot). Seeing as you define the header yourself at import time, you can do something like

$header = @("First","Last","Department","Location","Title")
$employees = Import-CSV C:\temp\test.txt -Delimiter ";" -Header $header
foreach ($employee in $employees) {
    foreach ($field in $header) {
        Write-Host $employee.$field -ForegroundColor Green
    }
}


If the field names were to come from the CSV itself, you could do something like

$header = ($employees[0] | Get-Member -MemberType NoteProperty).Name

after reading the CSV, the problem being that you can't really affect the order of the fields. (for reference: it's a hashtable which is sorted by key hash, not by anything human-readable)

If you need them in the same order as in the CSV, your best bet is reading the header line from the CSV and splitting it by the Delimiter.

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.