Solved

When writing to csv file how to change the order of the columns

Posted on 2013-11-20
1
260 Views
Last Modified: 2013-11-21
I have the following code to write last names and first names to a csv file:

$TempArray = New-Object -TypeName PSObject -Property @{            
            LastName = $lname            
            FirstName = $fname                 
}  

$TempArray | Export-CSV C:\MyList.csv -NoType

Open in new window


When I open my csv, the columns are listed in the following order:

FirstName         LastName

Is there a way to programmatically ensure they are written:

LastName  FirstName

I do not want ot copy paste, I want it to be written this way programmatically
0
Comment
Question by:YZlat
1 Comment
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 250 total points
ID: 39664327
Hi,
you have to use the select-object property in the right order:
$TempArray = New-Object -TypeName PSObject -Property @{            
            LastName = $lname            
            FirstName = $fname                 
}  

$TempArray | select-object -property LastName,FirstName | Export-CSV C:\MyList.csv -NoType 

Open in new window


HTH
Rainer
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Set OWA language and time zone in Exchange for individuals, all users or per database.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
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: …

760 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

17 Experts available now in Live!

Get 1:1 Help Now