locked
Write-Output all fields on one line RRS feed

  • Question

  • 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

    Thursday, June 7, 2012 2:36 PM

Answers

  • 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
    
    


    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by robMerritt Thursday, June 7, 2012 11:32 PM
    Thursday, June 7, 2012 2:57 PM
  • 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 $( ). )


    Grant Ward, a.k.a. Bigteddy


    • Edited by Bigteddy Thursday, June 7, 2012 3:46 PM
    • Marked as answer by robMerritt Thursday, June 7, 2012 11:33 PM
    Thursday, June 7, 2012 3:45 PM

All replies

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


    Grant Ward, a.k.a. Bigteddy

    Thursday, June 7, 2012 2:48 PM
  • 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
    
    


    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by robMerritt Thursday, June 7, 2012 11:32 PM
    Thursday, June 7, 2012 2:57 PM
  • 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),"
     
     
    • Proposed as answer by philhege2 Friday, November 17, 2017 8:39 PM
    Thursday, June 7, 2012 3:28 PM
  • 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 $( ). )


    Grant Ward, a.k.a. Bigteddy


    • Edited by Bigteddy Thursday, June 7, 2012 3:46 PM
    • Marked as answer by robMerritt Thursday, June 7, 2012 11:33 PM
    Thursday, June 7, 2012 3:45 PM
  • $() = PowerShell subexpression operator
     
     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 $( ). )
    >
     
     
    Thursday, June 7, 2012 11:03 PM
  • hey thats great its working for me now

    Thursday, June 7, 2012 11:33 PM
  • This method works for me. I like to use Write-Output, but the new line prevented me from doing so. :") Thanks.
    Monday, March 9, 2015 3:12 PM
  • how about using write-host instead?

    f.khan

    Wednesday, April 26, 2017 4:35 PM
  • Easy and works.  Thank you.
    Thursday, May 10, 2018 3:58 PM