locked
Creating Log File by Exporting Console Message to a TXT File RRS feed

  • Question

  • Hi Folks,

    I need help in creating a log.txt file that would store a particular ps1's console output messages. Can you please help how I can go by doing so ?

    Get-ADUser -Identity ABC | Out-Null
    Write-Host "User ABC found"
    Write-Host "User exists in Active Directory"

    Above is just an example. The real code is pretty lengthy so I didn't use it here.

    Sunday, November 18, 2018 8:49 AM

All replies

  • With WMF 5 and later we can do this:

    Get-ADUser -Identity ABC | Out-Null
    Write-Host "User ABC found" 6>logfile.txt
    Write-Host "User exists in Active Directory" 6>logfile.txt
    


    \_(ツ)_/

    Sunday, November 18, 2018 9:14 AM
  • For both console and file output you can also do this:

    Get-ADUser -Identity ABC | Out-Null
    "User ABC found" | Tee-Object logfile.txt -Append | Write-Host
    "User exists in Active Directory" | Tee-Object logfile.txt -Append | Write-Host


    \_(ツ)_/


    • Edited by jrv Sunday, November 18, 2018 9:19 AM
    Sunday, November 18, 2018 9:17 AM
  • This is making my code too bulky since it uses a lot of Write-Hosts.

    Can there be a way to copy all there is on Console at the end and paste it into a File that every run will create ?

    Sunday, November 18, 2018 3:37 PM
  • Create a function to replace all Write-Host statements and use the code in the function.

    If you are calling a PS1 script then redirect the whole script.

    . .\myscript.ps1 6>logfile.txt


    \_(ツ)_/

    Sunday, November 18, 2018 3:42 PM
  • So should I do something like - 

    Get-ADUser -Identity ABC | Out-Null
    Write-Host "User ABC found"
    Write-Host "User exists in Active Directory"
    
    \test.ps1 6>logfile.txt

    Sunday, November 18, 2018 4:36 PM
  • Start-transcript  ... no?  
    • Proposed as answer by A'ziz Thursday, November 22, 2018 7:23 AM
    Sunday, November 18, 2018 5:09 PM
  • 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.

    Thursday, November 22, 2018 6:34 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, November 27, 2018 2:46 AM