Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-20
1
Medium Priority
?
271 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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
Loops Section Overview

876 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