Solved

How to create a csv from another csv in powershell ?

Posted on 2013-05-21
8
300 Views
Last Modified: 2013-05-21
Hello,
I have a csv1 file with the following headers: Last Name      First Name      Dept Descrip      Social Security Numbers      Employment Category      Job Title      Work Phone      Room      Clock Number      Date Of Birth      Div Descrip

I would like to create csv2 file from csv1 but excluding the following headers Employment Category, Date of Birth Div Description columns, in other words with the following headers: Last Name      First Name      Dept Descrip      Social Security Numbers      Job Title      Work Phone      Room      Clock Number

Thanks for your help,
0
Comment
Question by:namerg
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:BT15
ID: 39185772
crossing my fingers that i have no typos
#replace file1.csv and file2.csv with the names/paths of your choosing


$file = import-csv file1.csv

$header = "Last Name,First Name,Dept Descrip,Social Security Numbers,Job Title,WorkPhone,Room,Clock Number"

$output = @(
$header
)

foreach ($line in $file){

$outline = $line.("last name") + "," + $line.("First name") + "," +$line.("Dept Descrip") + "," +$line.("Social Security Numbers") + "," +$line.("Job Title") + "," +$line.("WorkPhone") + "," +$line.("Room") + "," +$line.("Clock NUmber")

$output = @(
$ouput
$outline
)
}

$outline | sc file2.csv
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39185779
Import the source CSV, select the properties which you want and then export it to new one..
For example..
Import-csv c:\test.csv | Select "Last Name","First Name","Dept Descrip","Social Security Numbers","Job Title","Work Phone","Room","Clock Number" | Export-csv c:\testnew.csv -NTI

Open in new window

0
 

Author Comment

by:namerg
ID: 39185787
@subsun, I have not tested it is that easy ?
0
 
LVL 7

Expert Comment

by:BT15
ID: 39185793
lol where is my head at...

nicely done, Subsun
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 40

Expert Comment

by:Subsun
ID: 39185795
It should work.. just make sure you are typing the correct header names.
0
 

Author Comment

by:namerg
ID: 39185846
Sweet.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39185885
... and don't use the same file name for input and output ;-).
0
 

Author Comment

by:namerg
ID: 39185919
jajja, come on....I was aware of it. :P
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A short film showing how OnPage and Connectwise integration works.

919 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now