not able to out put content to a text file with line breaks












1















I am using out-file to send content to a text file, even though it usually takes care of putting line breaks in between one line after another, it is not happening in this case.



Output is something like below:




appleballcountry


I was expecting it to be as below




apple
ball
country


Below is the code snippet that I am using, please ignore the variables (as this is just a work in progress).



foreach ($li in $bin) {
$list2 = $list1 | Where-Object {$_ -like $li}
#$list2 #| out-file info2.txt
foreach ($li2 in $list2) {
$li3 += $li2 | where-object {$_ -like $li -and $_ -like "*Error*"}
$li3 | out-file info2.txt # it's this part that I am not able to get to
#the text file with line breaks
break
}
}


I am just beginning out with powershell and stack overflow in general and am aware that I might not be giving entire information here.



Also, I am unable to give powershell version as of now as I am not sure how to.










share|improve this question

























  • You should better explain what you are after (and what $bin/$list1 contain). 1st the code is redundant, $li2 only contains items like $li - so the second where can be where-object {$_ -like "*Error*". 2nd the out-file info2.txt will overwrite on every iteration. 3rd read Get-Help about_break

    – LotPings
    Jan 2 at 13:44











  • Just run $PSVersionTable to get your PS Version :)

    – James C.
    Jan 2 at 13:53
















1















I am using out-file to send content to a text file, even though it usually takes care of putting line breaks in between one line after another, it is not happening in this case.



Output is something like below:




appleballcountry


I was expecting it to be as below




apple
ball
country


Below is the code snippet that I am using, please ignore the variables (as this is just a work in progress).



foreach ($li in $bin) {
$list2 = $list1 | Where-Object {$_ -like $li}
#$list2 #| out-file info2.txt
foreach ($li2 in $list2) {
$li3 += $li2 | where-object {$_ -like $li -and $_ -like "*Error*"}
$li3 | out-file info2.txt # it's this part that I am not able to get to
#the text file with line breaks
break
}
}


I am just beginning out with powershell and stack overflow in general and am aware that I might not be giving entire information here.



Also, I am unable to give powershell version as of now as I am not sure how to.










share|improve this question

























  • You should better explain what you are after (and what $bin/$list1 contain). 1st the code is redundant, $li2 only contains items like $li - so the second where can be where-object {$_ -like "*Error*". 2nd the out-file info2.txt will overwrite on every iteration. 3rd read Get-Help about_break

    – LotPings
    Jan 2 at 13:44











  • Just run $PSVersionTable to get your PS Version :)

    – James C.
    Jan 2 at 13:53














1












1








1








I am using out-file to send content to a text file, even though it usually takes care of putting line breaks in between one line after another, it is not happening in this case.



Output is something like below:




appleballcountry


I was expecting it to be as below




apple
ball
country


Below is the code snippet that I am using, please ignore the variables (as this is just a work in progress).



foreach ($li in $bin) {
$list2 = $list1 | Where-Object {$_ -like $li}
#$list2 #| out-file info2.txt
foreach ($li2 in $list2) {
$li3 += $li2 | where-object {$_ -like $li -and $_ -like "*Error*"}
$li3 | out-file info2.txt # it's this part that I am not able to get to
#the text file with line breaks
break
}
}


I am just beginning out with powershell and stack overflow in general and am aware that I might not be giving entire information here.



Also, I am unable to give powershell version as of now as I am not sure how to.










share|improve this question
















I am using out-file to send content to a text file, even though it usually takes care of putting line breaks in between one line after another, it is not happening in this case.



Output is something like below:




appleballcountry


I was expecting it to be as below




apple
ball
country


Below is the code snippet that I am using, please ignore the variables (as this is just a work in progress).



foreach ($li in $bin) {
$list2 = $list1 | Where-Object {$_ -like $li}
#$list2 #| out-file info2.txt
foreach ($li2 in $list2) {
$li3 += $li2 | where-object {$_ -like $li -and $_ -like "*Error*"}
$li3 | out-file info2.txt # it's this part that I am not able to get to
#the text file with line breaks
break
}
}


I am just beginning out with powershell and stack overflow in general and am aware that I might not be giving entire information here.



Also, I am unable to give powershell version as of now as I am not sure how to.







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:52









James C.

9,10622133




9,10622133










asked Jan 2 at 13:03









Jericho162Jericho162

61




61













  • You should better explain what you are after (and what $bin/$list1 contain). 1st the code is redundant, $li2 only contains items like $li - so the second where can be where-object {$_ -like "*Error*". 2nd the out-file info2.txt will overwrite on every iteration. 3rd read Get-Help about_break

    – LotPings
    Jan 2 at 13:44











  • Just run $PSVersionTable to get your PS Version :)

    – James C.
    Jan 2 at 13:53



















  • You should better explain what you are after (and what $bin/$list1 contain). 1st the code is redundant, $li2 only contains items like $li - so the second where can be where-object {$_ -like "*Error*". 2nd the out-file info2.txt will overwrite on every iteration. 3rd read Get-Help about_break

    – LotPings
    Jan 2 at 13:44











  • Just run $PSVersionTable to get your PS Version :)

    – James C.
    Jan 2 at 13:53

















You should better explain what you are after (and what $bin/$list1 contain). 1st the code is redundant, $li2 only contains items like $li - so the second where can be where-object {$_ -like "*Error*". 2nd the out-file info2.txt will overwrite on every iteration. 3rd read Get-Help about_break

– LotPings
Jan 2 at 13:44





You should better explain what you are after (and what $bin/$list1 contain). 1st the code is redundant, $li2 only contains items like $li - so the second where can be where-object {$_ -like "*Error*". 2nd the out-file info2.txt will overwrite on every iteration. 3rd read Get-Help about_break

– LotPings
Jan 2 at 13:44













Just run $PSVersionTable to get your PS Version :)

– James C.
Jan 2 at 13:53





Just run $PSVersionTable to get your PS Version :)

– James C.
Jan 2 at 13:53












2 Answers
2






active

oldest

votes


















0














Your problem is not with out-file but your $li3 string.



When you do the += PowerShell will just mush the words together, and not regard them as seperate objects.



What you could do is add the where-object to your foreach statement, and manually assign a new line.



foreach ($li in $bin)
{
$list2 = $list1 | Where-Object {$_ -like $li}
foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
{
$li3 += $li2 + "`r`n"
$li3 | out-file info2.txt
break
}
}


Might also be a good idea to move the out-file outside of the loop.






share|improve this answer


























  • That worked !! thanks for pointing it out, seems natural now that you have pointed it out. I just put the appending to out-file part inside the loop out of habit..

    – Jericho162
    Jan 2 at 13:44











  • Glad I could help!

    – jossefanten
    Jan 2 at 13:47



















0














When working with the += operator you are concatenating both strings $li3 and $li2.



PS C:temp> $string1 = "new"
PS C:temp> $string2 = "year"
PS C:temp> $string1 += $string2
PS C:temp> $string1
newyear


I see two alternatives for your foreach loop here:



Option1 you simply append to your text file:



foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
{
$li2 | Out-File -Append info2.txt
}


Option2 if you need an object:



$li3 = New-Object System.Collections.Generic.List[System.String]
foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
{
$li3.Add($li2)
}
$li3 | Out-File info2.txt


Your code logic can probably be improved, you're filtering two times on $li:



$list2 = $list1 | Where-Object {$_ -like $li}
foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006880%2fnot-able-to-out-put-content-to-a-text-file-with-line-breaks%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your problem is not with out-file but your $li3 string.



    When you do the += PowerShell will just mush the words together, and not regard them as seperate objects.



    What you could do is add the where-object to your foreach statement, and manually assign a new line.



    foreach ($li in $bin)
    {
    $list2 = $list1 | Where-Object {$_ -like $li}
    foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
    {
    $li3 += $li2 + "`r`n"
    $li3 | out-file info2.txt
    break
    }
    }


    Might also be a good idea to move the out-file outside of the loop.






    share|improve this answer


























    • That worked !! thanks for pointing it out, seems natural now that you have pointed it out. I just put the appending to out-file part inside the loop out of habit..

      – Jericho162
      Jan 2 at 13:44











    • Glad I could help!

      – jossefanten
      Jan 2 at 13:47
















    0














    Your problem is not with out-file but your $li3 string.



    When you do the += PowerShell will just mush the words together, and not regard them as seperate objects.



    What you could do is add the where-object to your foreach statement, and manually assign a new line.



    foreach ($li in $bin)
    {
    $list2 = $list1 | Where-Object {$_ -like $li}
    foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
    {
    $li3 += $li2 + "`r`n"
    $li3 | out-file info2.txt
    break
    }
    }


    Might also be a good idea to move the out-file outside of the loop.






    share|improve this answer


























    • That worked !! thanks for pointing it out, seems natural now that you have pointed it out. I just put the appending to out-file part inside the loop out of habit..

      – Jericho162
      Jan 2 at 13:44











    • Glad I could help!

      – jossefanten
      Jan 2 at 13:47














    0












    0








    0







    Your problem is not with out-file but your $li3 string.



    When you do the += PowerShell will just mush the words together, and not regard them as seperate objects.



    What you could do is add the where-object to your foreach statement, and manually assign a new line.



    foreach ($li in $bin)
    {
    $list2 = $list1 | Where-Object {$_ -like $li}
    foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
    {
    $li3 += $li2 + "`r`n"
    $li3 | out-file info2.txt
    break
    }
    }


    Might also be a good idea to move the out-file outside of the loop.






    share|improve this answer















    Your problem is not with out-file but your $li3 string.



    When you do the += PowerShell will just mush the words together, and not regard them as seperate objects.



    What you could do is add the where-object to your foreach statement, and manually assign a new line.



    foreach ($li in $bin)
    {
    $list2 = $list1 | Where-Object {$_ -like $li}
    foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
    {
    $li3 += $li2 + "`r`n"
    $li3 | out-file info2.txt
    break
    }
    }


    Might also be a good idea to move the out-file outside of the loop.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 2 at 13:28

























    answered Jan 2 at 13:20









    jossefantenjossefanten

    366




    366













    • That worked !! thanks for pointing it out, seems natural now that you have pointed it out. I just put the appending to out-file part inside the loop out of habit..

      – Jericho162
      Jan 2 at 13:44











    • Glad I could help!

      – jossefanten
      Jan 2 at 13:47



















    • That worked !! thanks for pointing it out, seems natural now that you have pointed it out. I just put the appending to out-file part inside the loop out of habit..

      – Jericho162
      Jan 2 at 13:44











    • Glad I could help!

      – jossefanten
      Jan 2 at 13:47

















    That worked !! thanks for pointing it out, seems natural now that you have pointed it out. I just put the appending to out-file part inside the loop out of habit..

    – Jericho162
    Jan 2 at 13:44





    That worked !! thanks for pointing it out, seems natural now that you have pointed it out. I just put the appending to out-file part inside the loop out of habit..

    – Jericho162
    Jan 2 at 13:44













    Glad I could help!

    – jossefanten
    Jan 2 at 13:47





    Glad I could help!

    – jossefanten
    Jan 2 at 13:47













    0














    When working with the += operator you are concatenating both strings $li3 and $li2.



    PS C:temp> $string1 = "new"
    PS C:temp> $string2 = "year"
    PS C:temp> $string1 += $string2
    PS C:temp> $string1
    newyear


    I see two alternatives for your foreach loop here:



    Option1 you simply append to your text file:



    foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
    {
    $li2 | Out-File -Append info2.txt
    }


    Option2 if you need an object:



    $li3 = New-Object System.Collections.Generic.List[System.String]
    foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
    {
    $li3.Add($li2)
    }
    $li3 | Out-File info2.txt


    Your code logic can probably be improved, you're filtering two times on $li:



    $list2 = $list1 | Where-Object {$_ -like $li}
    foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})





    share|improve this answer






























      0














      When working with the += operator you are concatenating both strings $li3 and $li2.



      PS C:temp> $string1 = "new"
      PS C:temp> $string2 = "year"
      PS C:temp> $string1 += $string2
      PS C:temp> $string1
      newyear


      I see two alternatives for your foreach loop here:



      Option1 you simply append to your text file:



      foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
      {
      $li2 | Out-File -Append info2.txt
      }


      Option2 if you need an object:



      $li3 = New-Object System.Collections.Generic.List[System.String]
      foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
      {
      $li3.Add($li2)
      }
      $li3 | Out-File info2.txt


      Your code logic can probably be improved, you're filtering two times on $li:



      $list2 = $list1 | Where-Object {$_ -like $li}
      foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})





      share|improve this answer




























        0












        0








        0







        When working with the += operator you are concatenating both strings $li3 and $li2.



        PS C:temp> $string1 = "new"
        PS C:temp> $string2 = "year"
        PS C:temp> $string1 += $string2
        PS C:temp> $string1
        newyear


        I see two alternatives for your foreach loop here:



        Option1 you simply append to your text file:



        foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
        {
        $li2 | Out-File -Append info2.txt
        }


        Option2 if you need an object:



        $li3 = New-Object System.Collections.Generic.List[System.String]
        foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
        {
        $li3.Add($li2)
        }
        $li3 | Out-File info2.txt


        Your code logic can probably be improved, you're filtering two times on $li:



        $list2 = $list1 | Where-Object {$_ -like $li}
        foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})





        share|improve this answer















        When working with the += operator you are concatenating both strings $li3 and $li2.



        PS C:temp> $string1 = "new"
        PS C:temp> $string2 = "year"
        PS C:temp> $string1 += $string2
        PS C:temp> $string1
        newyear


        I see two alternatives for your foreach loop here:



        Option1 you simply append to your text file:



        foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
        {
        $li2 | Out-File -Append info2.txt
        }


        Option2 if you need an object:



        $li3 = New-Object System.Collections.Generic.List[System.String]
        foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})
        {
        $li3.Add($li2)
        }
        $li3 | Out-File info2.txt


        Your code logic can probably be improved, you're filtering two times on $li:



        $list2 = $list1 | Where-Object {$_ -like $li}
        foreach ($li2 in $list2 | Where-Object {$_ -like $li -and $_ -notlike "Error"})






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 13:56









        James C.

        9,10622133




        9,10622133










        answered Jan 2 at 13:47









        seedieseedie

        364




        364






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006880%2fnot-able-to-out-put-content-to-a-text-file-with-line-breaks%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas