Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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