locked
Why out-file doesn't work in ELSE statement RRS feed

  • Question

  • I have a script that as an IF and ELSE.

    The IF part "write-output" ..... | out-file C:\example.html -Append work fine
    For the one that reach the ELSE it doesn't add anything to the out-file. What should I do.
    I have even tried to out-file to a different file name but it still didn't work

    Wednesday, January 2, 2019 7:37 PM

All replies

  • You should probably post your code using the code posting tool.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, January 2, 2019 7:43 PM
  • Works for me.  You couldn't pipe after the last curly bracket.


    $a
    = 1
    if
    ($a -eq 2) { echo 2 | out-file file.txt } else { echo 1 | out-file file.txt }

    • Edited by JS2010 Friday, January 4, 2019 8:59 PM
    Wednesday, January 2, 2019 7:58 PM
  • Hi,

    Thanks for your question.

    What command did you put in the else part? Also, what is your If condition?

    I think nothing wrong with the out-file cmdlet.

    Best Regards,

    Lee


    Just do it.

    Thursday, January 3, 2019 2:51 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, January 8, 2019 8:29 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Friday, January 11, 2019 2:18 AM