Solved

How to create a csv from another csv in powershell ?

Posted on 2013-05-21
8
301 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 7

Expert Comment

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

nicely done, Subsun
0
 
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 69

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

12 Experts available now in Live!

Get 1:1 Help Now