Solved

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

Posted on 2013-11-20
1
263 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
[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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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ā€¦

749 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