?
Solved

import csv to use with powershell

Posted on 2014-09-12
12
Medium Priority
?
287 Views
Last Modified: 2014-09-13
hi folks,

how can i import csv with converting to powershell understanding format?
 
Import-Csv -Path c:\test.csv -Delimiter ';'

source data
0
Comment
Question by:Mandy_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40319846
Here is the full technet
http://technet.microsoft.com/en-us/library/hh849891.aspx

the delimiter you specify is anticipated to be the separator for each column of data

e.g

Server;Database;LastAccessed
SERVER1;MYSQLBD;10/01/2013
SERVER2;MYSQLBD2;10/02/2013
SERVER3;MYSQLBD3;10/0/2013

import-csv c:\file.csv -delimiter ";" |  %{write-host $_.server $_.Database $_.LAstAccessed}
what the line above does, is it tells import csv ";" represents a new column and then writes the values for each column in the foreach loop, that matches the header I specify.
0
 
LVL 40

Expert Comment

by:footech
ID: 40319851
A rather vague question...
Typically you'll do one of two things with Import-CSV, assign the results to a variable, or send the results to the pipeline for further processing.
$data = Import-CSV somefile.csv
Import-CSV somefile.csv | % { "do something" }

Open in new window


Is there a problem you're encountering?
0
 
LVL 2

Author Comment

by:Mandy_
ID: 40319864
I would like to import data in colums like that

Userid     dispalyname    
12345      jones, mike
 
to use them they should looks like that:

"UserId","Name"
"12345", "jones, mike"
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 29

Expert Comment

by:becraig
ID: 40319879
My post gives you a clear example of what to do.

Is there something specific about the process you do not get ?

I gave an example of the input and how to read the input and output it to screen.
0
 
LVL 2

Author Comment

by:Mandy_
ID: 40319886
see my post above
0
 
LVL 2

Author Comment

by:Mandy_
ID: 40319894
(import-csv -delimiter ';' c:\export_9.csv | sort produkt, version -descending | convertto-csv -notype -delimiter ',') -replace '"'

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40319905
Can you give a sample of your input file ?

It helps to be able to repro the exact circumstance so the solution is exactly what you need.
0
 
LVL 2

Author Comment

by:Mandy_
ID: 40319942
Source
Userid     dispalyname    
12345      jones, mike
 
DEST:

"UserId","Name"
"12345", "jones, mike"
0
 
LVL 29

Expert Comment

by:becraig
ID: 40319950
For this I would suggest a simple get-content and use a split create a hash then export to csv (since your source does not have a valid delimiter).

Footech may have another approach.

I will put a quick script together.
0
 
LVL 29

Assisted Solution

by:becraig
becraig earned 800 total points
ID: 40319974
$nucsv = @()
 gc c:\data\file.csv | select -skip 1 | % {
            $cols = $_ -split "\s+"
            $item = New-Object PSObject
            $item | Add-Member -type NoteProperty -Name 'USERID' -Value $cols[0].trim()
            $item | Add-Member -type NoteProperty -Name 'DISPLAYNAME' -Value $cols[1].trim()
            $nucsv += $item
            }

            $nucsv | Export-Csv -Delimiter ";" -nti c:\data\report.csv

Open in new window

0
 
LVL 40

Accepted Solution

by:
footech earned 1200 total points
ID: 40319996
One issue I see is that it looks like a field can contain spaces like "jones, mike".  You could adjust becraig's code with the following (only the split is changed).
$nucsv = @()
gc c:\test.csv | select -skip 1 | % {
    $cols = $_ -split "\s{2,}"
    $item = New-Object PSObject
    $item | Add-Member -type NoteProperty -Name 'USERID' -Value $cols[0].trim()
    $item | Add-Member -type NoteProperty -Name 'DISPLAYNAME' -Value $cols[1].trim() -PassThru
    $nucsv += $item
    }

Open in new window


Another method would be like
Get-Content input.csv | % { $_ -replace "\s{2,}",";" } | ConvertFrom-Csv -Delimiter ";" | Export-CSV output.csv -notype

Open in new window

0
 
LVL 2

Author Closing Comment

by:Mandy_
ID: 40320606
Thank you so much
0

Featured Post

WatchGuard's M Series Appliances - Miecom Approved

WatchGuard's newest M series appliances were put to the test by Miercom.  We had great results and outperformed all of our competitors in both stateless and stateful traffic throghput scenarios! Ready to see how your UTM appliance stacked up? Download the Miercom Report!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question