Output Multiple Variables to CSV file.

curtis591
curtis591 used Ask the Experts™
on
I am new to Powershell and there seems to be an easy way to do everything, I just can't find this.   I have a loop that it is calculating some values and each time through the loop I would like to append those variables to a comma delimited file so the results can be easily viewed.  

Below is the example loop that I came up with andI would like to write $i and $a and $b to file each way.
for ( $i = 1; $i -le 5; $i+=1 ) 
{ 
$i 
$A ="Result $i"
$B = 2 * $i
}

Open in new window


Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
PowerShell deals with objects. In your example $i, $a, $b are just separate values. But we can make objects out of those with the new-object cmdlet. The syntax is very easy, is you know hashtables. In the -property parameter I set a parameter with "i" as a name, and the value of it is $i, another parameter with the name of "a", with a value of $a, etc.

In my code I just created the CSV format onto the output, not into a file, but you can direct it with a:
 | out-file c:\somwhere\somefile.csv -append

statement to a file. You can also change the delimiter, if you specify a -delimiter parameter for the convertto-csv cmdlet.
I hope that helped.

Another solution for that is here:
1..5 | ForEach-Object {new-object -TypeName psobject -Property @{
      i=($i++);
      a=($A ="Result $i");
      b=($B = 2 * $i)}
} | ConvertTo-Csv -NoTypeInformation

(A bit condensed, but after a while you'll see that it is more "PowerShell-like" :-)
for ( $i = 1; $i -le 5; $i+=1 )  
{  
$A ="Result $i"
$B = 2 * $i

New-Object -TypeName psobject -Property @{i=$i; a=$a; b=$b} | ConvertTo-Csv -NoTypeInformation
}

Open in new window

Commented:
Sorry, I made a little mistake in both code. Here are the correct ones.
Version 1 (Programming-like)

$(for ( $i = 1; $i -le 5; $i+=1 )  
{  
$A ="Result $i"
$B = 2 * $i
New-Object -TypeName psobject -Property @{i=$i; a=$a; b=$b} 
}) | ConvertTo-Csv -NoTypeInformation

Version 2 (PowerShell-like)
1..5 | ForEach-Object {
	new-object -TypeName psobject -Property @{
	i=($i++); 
	a=($A ="Result $_"); 
	b=($B = 2 * $_)}
} | ConvertTo-Csv -NoTypeInformation

Open in new window

Commented:
Oh, it's Friday afternoon... Once more.
#Version 1 (Programming-like)

$(for ( $i = 1; $i -le 5; $i+=1 )  
{  
$A ="Result $i"
$B = 2 * $i
New-Object -TypeName psobject -Property @{i=$i; a=$a; b=$b} 
}) | ConvertTo-Csv -NoTypeInformation

#Version 2 (PowerShell-like)
1..5 | ForEach-Object {
	new-object -TypeName psobject -Property @{
	i=($_); 
	a=($A ="Result $_"); 
	b=($B = 2 * $_)}
} | ConvertTo-Csv -NoTypeInformation

Open in new window

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Commented:
And if you do not need the variables $a and $b, you can use that:

#Version 2 (PowerShell-like)
1..5 | ForEach-Object {
      new-object -TypeName psobject -Property @{
      i=$_;
      a="Result $_";
      b=2 * $_}
} | ConvertTo-Csv -NoTypeInformation
Thank-you again for the help.  The real script is a little more complicated than my example because it has nested loops and it is collecting data for 3 different reports as it loops.  Using your code I was able to create an array of the new objects and then export the array which does exactly what I was looking for.

I have only worked with Powershell for a couple of weeks.  Do you have any reccomendations of either a good book or site where one can learn of the possible commands or different things you can do.   I have heard lots of people just say you gotta use it but that takes a lot of google seaches to find the answer if you haven't learned about something like the new-object comamnd.

Commented:
I think the best for PowerShell theory is Bruce's PowerShell in Action: http://www.manning.com/payette2. For practice there are more good ones: PowerShell TFM (http://www.sapienpress.com/powershell3.asp) or PowerShell in Practice (http://www.manning.com/siddaway).  Or if you can speak Hungarian, I can recommend my book :-) http://download.microsoft.com/download/5/8/8/5886EEB0-BFB5-4854-9D17-AAEDE66825D3/konyvek/PowerShell_v2/Microsoft_Powershell_2_konyv.pdf 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial