Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

Export csv file for the powershell cmdlet.

I have a code which is giving output in write-host, I want the same to be exported to csv.

Get-Content -Path $infile | ForEach-Object {
	$empid=$_
	$myself=$employeeInfo[$empid]
	$manempid=$myself.agsupvemployeenumber
	$manager=$employeeInfo[$manempid]
	$manager.preferredmail

	Write-Host ("{0} {1} {2} {3} {4}" -f $myself.personnumber, $myself.givenname, $manager.personnumber, $manager.givenname , $manager.preferredmail)

}  

The output will be 

Empid Name Mangername manageremail
------------------------------------------------------------------

Open in new window

0
AhmedAliShaik
Asked:
AhmedAliShaik
  • 4
  • 3
1 Solution
 
QlemoC++ DeveloperCommented:
I'll assume a simple CSV (no column titles, no double quotes to enclose strings) is sufficient for your purpose:
Get-Content -Path $infile | ForEach-Object {
	$empid=$_
	$myself=$employeeInfo[$empid]
	$manempid=$myself.agsupvemployeenumber
	$manager=$employeeInfo[$manempid]

	Write-Output "{0},{1},{2},{3},{4}" -f $myself.personnumber, $myself.givenname, $manager.personnumber, $manager.givenname , $manager.preferredmail
}  | out-file -encoding ASCII Results.csv

Open in new window

0
 
AhmedAliShaikAuthor Commented:
Thanks Qlemo,

We are getting output in the screen.

There are no errors but the csv file is 0 kb, not having any data.
0
 
QlemoC++ DeveloperCommented:
Certain that you used my code? Write-Output writes strings into the pipe, and Out-File writes the pipe contents to a file. Your description of what happens fits if you still used Write-Host (which will always output to the console).
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
AhmedAliShaikAuthor Commented:
Its perfectly working now after changing write-output. Now I am getting expected output.

But i dont have control over the output.It is in the below format.

E1 abc abcmgr abcmgr@xyz.com
E2 bbc bbcmgr bbcmgr@xyz.com
E3 cbc cbcmgr cbcmgr@xyz.com

All the output in excel file is in single row and column. Can we format to get in row and column wise.
0
 
QlemoC++ DeveloperCommented:
Sorry, we need the parens for write-output - I accidentally eliminated those:
Get-Content -Path $infile | ForEach-Object {
  $empid=$_
  $myself=$employeeInfo[$empid]
  $manempid=$myself.agsupvemployeenumber
  $manager=$employeeInfo[$manempid]

  Write-Output ("{0},{1},{2},{3},{4}" -f $myself.personnumber, $myself.givenname, $manager.personnumber, $manager.givenname , $manager.preferredmail)
}  | out-file -encoding ASCII Results.csv

Open in new window

0
 
AhmedAliShaikAuthor Commented:
Perfect.

Hope will get more extended support from you.

Thanks a lot.
0
 
QlemoC++ DeveloperCommented:
Ask, and you shall receive ;-).
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now