0

Right now I'm importing a large .CSV file into powershell and currently just putting all the data into one variable. My question is how do I make it so I split each column into a different variable?

I'm attempting to use ForEach-Object for this, but I'm still rather new at Powershell and could use some help.

Current Code

$file1 = Import-Csv -Path C:\Users\lin\Documents\EndpointScript.csv | select 'System Name', 'Product Version'| where 'Product Version' -eq '5.4.6.220'
$file2 = Import-Csv -Path C:\Users\lin\Documents\EndpointScript.csv | select 'Product Version'| where 'Product Version' -ne '5.4.6.220'
Write-Output $file1
"Up to Date: "; $file1.Count
"Out of Date: "; $file2.Count
"Todays Date: "; Get-Date -Format g

$file1 | export-csv C:\Users\lin\Documents\AgentReport.csv -NoTypeInformation

Attemping to do something like this


$objectArray = Import-Csv -Path C:\Users\tlines\Documents\EndpointScript.csv
$file1 | ForEach-Object {
Attemping to do something like this

I hope for each columns data to be in a different variable so it it's easier to do things with just one of the columns.

0

2 Answers 2

1

You can access the columns separately without putting them in dedicated variables. here's the csv:

column1;column2;column3
1a;2a;3a
1b;2b;3b
1c;2c;3c

Now import it and access the columns separately by using the dot notation or pipe it to select object:

$csv = Import-Csv -Path .\test.csv -Delimiter ";"

Write-Output "pipe to select-object to only retrieve a single property"
$csv | select column1 

Write-Output "access the properties with dot notation"
$csv.column1

Piping it to select-object will return you array of psobjects.

($csv | select column3 -first 1).GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

Dot notation will give you an array of strings.

$csv.column3[0].GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
Sign up to request clarification or add additional context in comments.

Comments

0

If I guess right, this should do:

$objectArray = Import-Csv -Path C:\Users\tlines\Documents\EndpointScript.csv

$objectArray | Group-Object 'Product Version' -NoElement 

This will return two columns:

Count Name
----- ----

Where Name contains the version numbers present.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.