Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell convert to CSV formatted file

Status
Not open for further replies.

santhas

Technical User
Aug 27, 2003
35
AU
Need help on Powershell

I have file with data like this:

++++++++++++++++Input file +++++++++++++++++++++++++++++
User Name: Power DBA
Description:
Role: Power DBA
Last Login Date: Jul 1, 2011 4:54:18 PM

User Name: Power User
Description: Admin Team
Role: Power User
Last Login Date: Mar 15, 2011 8:16:19 AM

User Name: guest
Description: Monitoring
Role: Operator
Last Login Date: Dec 14, 2011 3:45:29 PM

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==

I need to convert this to CSV formatted file using Powershell

+++++++++++++++++++++ file output should like this ++++++++++++++++++
User Name,Description, Role, Last Login Date

Power DBA,, Power DBA, Jul 1, 2011 4:54:18 PM

Power User,Admin Team,Power User, Mar 15, 2011 8:16:19 AM

guest,Monitoring,Operator,Dec 14, 2011 3:45:29 PM

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=====


Thanks

San
 
I don't have time to mock it up in full, but you should be able to read in the file (Get-Content), then split it on the blank lines between each group, then split again by line break. Essentially a multi-dimensional array. Then dump to a single array, and Export-Csv to the file.

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.
 
Not easy in PS.
I had one of those last week with about 50,000 array members. Dumped to Excel, cell b1=a2 then c1=a3, d1-a4 and so on to get the first row reading right.
Copy columns B onwards from row 1 to the end.
Copy and paste the formulae to values.
Sort the whole data cube by column A
Delete all ROWS where column A doesn't start Username.

There's your data in correct forum.
Find and replace column A to replace "Username: " with blank
Repeat for other columns.

Robert is most definitely your Mother's Brother.
 
What is generating your input file?

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top