Write-Output all fields on one line

I have a Write-Output command 

Write-Output $start, $end, $title, $location, 

what i get is:

May-29-12 4:30:00 PM
June-25-12 5:45:00 PM
Holy Yoga
Meeting Room B

what I want is:

May-29-12 4:30:00 PM,June-25-12 5:45:00 PM,Holy Yoga,Meeting Room B

so what I am missing is chomp for PS so i dont have crlf after every field and a way to keep the commas there

as I am trying to make a csv

June 7th, 2012 2:36pm

Write-Output "$($start),$($end),$($title),$($location)," 

Free Windows Admin Tool Kit Click here and download it now
June 7th, 2012 2:48pm

Although, I must mention that there is a much better way of building a csv file.  Below is a piece of psuedo-code (It is incomplete).  Here I am trying to demonstrate building an array ($results) of psobjects, with the properties that we set for that iteration of the loop. 

Note, I have not included the loop code because I don't know where you're getting the data, but I hope you get the idea:

$results = @()
#loop here
# do some stuff here to populate your variables:
###
$props = @{
    Start=$start
    End=$end
    Title=$title
    Location=$location
    }
$results += New-Object psobject -Property $props
# end loop here

$results | Export-Csv 'Results.csv' -NoTypeInformation


June 7th, 2012 2:57pm

Lose that trailing comma and consider the alternative   Write-Output "$start,$end,$title,$location"   Your $() wrapped expressions are more generally applicable.   As a CSV, this sort of solution assumes no commas exist in the data.   - Larry   On 6/7/2012 9:48 AM, Bigteddy wrote: > Write-Output "$($start),$($end),$($title),$($location),"    
Free Windows Admin Tool Kit Click here and download it now
June 7th, 2012 3:28pm

Yes, Larry, quite right.  But I was concentrating on the string, not considering it as part of a csv (which, as I went on to post, I don't think it should be).

I just took the OP's string as supplied, and showed how I'd use the variables in a string.  Using the $( ) around variabled in a string I think is good practice, if only to avoid errors like:

"$myVar1And $myVar2"

This can be avoided like this:

"$($myVar1)And $myVar2"

Also, if the variable needs to be accessed my dot notation (e.g. $file.basename), you already are in the habit of using the evaluator (for want of a better name for $

June 7th, 2012 3:45pm

$() = PowerShell subexpression operator   http://ss64.com/ps/syntax-operators.html  On 6/7/2012 10:45 AM, Bigteddy wrote: > ... Also, if the variable needs to be accessed my dot notation  > (e.g. $file.basename), you already are > in the habit of using the evaluator (for want of a better name for $( ). ) >    
Free Windows Admin Tool Kit Click here and download it now
June 7th, 2012 11:03pm

hey thats great its working for me now

June 7th, 2012 11:33pm

This method works for me. I like to use Write-Output, but the new line prevented me from doing so. :") Thanks.
Free Windows Admin Tool Kit Click here and download it now
March 9th, 2015 11:13am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics