1

I am trying to display unique values from each column in an array that is generated from a CSV file.

CSV file contains:

ENV,RELEASE,SERVERNAME
DEVL,2008,winhost01
DEVL,2008,winhost02
DEVL,2014,winhost03
DEVL,2016,winhost04
TEST,2008,winhost05
TEST,2014,winhost06
PROD,2008,winhost07
PROD,2014,winhost08
PROD,2014,winhost09
PROD,2014,winhost10

Expected output:

ENV 
--- 
DEVL
TEST
PROD

RELEASE
---
2008
2014
2016

SERVERNAME
---
winhost01
winhost02
..etc..
winhost10

I go through each property in the array printing off the values. I get the first column's values in the first loop, but every column after that just prints an empty line (16 empty lines to be exact).

$arrayin = Import-Csv "Documents\Powershell\test.csv";
$cols = $arrayin[0].psobject.properties.name;
$i = 0;
do {
    $arrayin | Select-Object -Property $cols[$i] -Unique;
    $i++;
} until ($i -eq $cols.count)

Actual output:

ENV 
--- 
DEVL
TEST
PROD
...16 empty lines follow...

How can I display the other array properties (columns?) instead of empty lines?

Joey
  • 433
  • 2
  • 6
  • 1
    @PetSerAl, +1 as this is the correct answer but requires an explanation: In Joey's function all objects are appended to the pipeline without output them, like you do with using `FT` (`Format-Table`) or `Write-Host`. At the end, PowerShell releases the whole pipeline but only cares about the property (`ENV`) defined in the first object of the pipeline, see also: https://stackoverflow.com/questions/44428189/not-all-properties-displayed/44429084#44429084 – iRon Apr 28 '18 at 10:50
  • 1
    @PetSerAl I suggest expanding your comment into an answer, for the sake of future visitors. – Walter Mitty Apr 28 '18 at 11:25
  • Thank you! PetSerAl's answer was even shorter than i expected. iRon, thank you for the explanation, that really helps. – Joey Apr 28 '18 at 15:05

0 Answers0