Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty value powershell array

I have a strange issue, this is my CSV:

Serveur;Carte;Cordon;IP;Mac;Vmnic ;Vmnic mac;Connect;Port
Dexter;eth1;405;172.16.5.117;00:24:e8:36:36:df;Vmnic0;00:50:56:56:36:df;sw-front-1;A1
Dexter;eth2;14;192.168.140.17;00:24:e8:36:36:e1;Vmnic1;00:50:56:56:36:e1; sw_eq_ds_1;3
;;;;;;;;
Gordon;eth1;404;172.16.5.124;b8:ac:6f:8d:ac:b4;Vmnic0;00:50:56:5d:ac:b4;;
Gordon;eth2;35;192.168.140.114;b8:ac:6f:8d:ac:b6;Vmnic1;00:50:56:5d:ac:b6;;
Gordon;eth3;254;192.168.33.10;b8:ac:6f:8d:ac:b8;Vmnic2;00:50:56:5d:ac:b8;;

So I imported it into an array with the following code:

$Serveur = @()

Import-Csv C:\Users\aasif\Desktop\myfile.csv -Delimiter ";" |`
    ForEach-Object {
        $Serveur += $_.Serveur
    }

And to remove duplicate values I did this :

$Serveur = $Serveur | sort -uniq

So when I display my Array, I obtain these two values : Dexter and Gordon and a third null value

But I also get an empty value

The following code return 3

$Serveur.count

Why?

Thanks for your help

like image 472
Adeel ASIF Avatar asked Nov 19 '25 18:11

Adeel ASIF


2 Answers

If you want exclude empty values you can do like this

$Serveur = $Serveur |  ? { $_ } | sort -uniq
like image 173
CB. Avatar answered Nov 22 '25 06:11

CB.


In case someone (like me) needs to remove empty elements from array, but without sorting:

$Serveur = $Serveur | Where-Object { $_ } | Select -Unique
like image 44
Rod Avatar answered Nov 22 '25 06:11

Rod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!