How to get Import-CSV to work for me

Hi,

I have a csv file with 5 columns, the first 3 columns are IPV4 IPs with the 4-6 columns just text.  My goal is to Import the CSV, convert to strings, then perform string comparisons using the first 3 columns against other variables.

The issue I'm seeing is an Import-Csv natively creates a PSCustomObject, so I perform the following to create the first column as a string, however this technique doesn't work for the other columns.

$header = "H1", "H2", "H3", "H4", "H5", "H6"

$Data=Import-Csv C:\Data.csv -Header $header | Select -Expand H1

I need the whole row or all columns to be string so I can use in my script for comparisons.  How can I do

April 29th, 2015 4:28pm

Not a  very clear question.  Try just saying what you want the end product to be.  Forget about how you want to do it.

Posting the first few lines of your file would also be helpful.

Using -Expand on a column does nothing.

Try this:

Import-Csv C:\Data.csv -Header H1,H2,H3,H4,H5,H6 | Select H1,H2,H3

Now tell us what you want as output.

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 4:51pm

Hello,

I'd use [System.Net.IPAddress]::Equals() like so:

$IP1 = "10.192.1.1"
$IP2 = "10.192.1.31"
[System.Net.IPAddress]::Equals($ip1, $IP2)
Karl

April 29th, 2015 5:17pm

Just a simple note - CV files are strings and the fields as loaded are still strings.  Why do you think you need to convert anything.

Again, as I posted above,  You need to clarify what you are trying to do.

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 5:54pm

Karl - "compare" does not mean equal.  It can mean many other things.  IPs can be in a subnet or not.  IPs can be in a network or not.  Many other possibilities.

Two IP strings are either equal or not.  No need to use the type conversion.

PS> '192.168.1.10' -eq '192.168.1.11'
False
PS> '192.168.1.10' -eq '192.168.1.10
True

It is simple as that.

April 29th, 2015 5:56pm

That works great Karl.  Thanks.
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 6:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics