Out file to New Line in Text File

This topic contains 2 replies, has 3 voices, and was last updated by  Olaf Soyk 2 months, 1 week ago.

  • Author
    Posts
  • #72178

    Dr3w
    Participant

    Hi guys,

    Trying to log the CPU Temp, Usage and date/time to a TXT file – all is well except I cannot get the output to print to a new line. It makes my log unreadable

    I added `r`n but this doesn't make any difference.

    function Get-Temperature {
    $t = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
    $returntemp = @()

    foreach ($temp in $t.CurrentTemperature)
    {

    $currentTempKelvin = $temp / 10
    $currentTempCelsius = $currentTempKelvin – 273.15

    $currentTempFahrenheit = (9/5) * $currentTempCelsius + 32

    $returntemp += $currentTempCelsius.ToString() + " C : " + $currentTempFahrenheit.ToString() + " F : " + $currentTempKelvin + "K"
    }
    return $returntemp
    }

    $temp = Get-Temperature
    $time = get-date -Format g
    $procesor = wmic cpu get loadpercentage

    $OFS = "`r`n"
    "$processor$temp" + $OFS + "$date" + $OFS | Out-File -Encoding Ascii -append C:\Test.html

    #echo $procesor$temp$time c:\test2.html

  • #72179

    Curtis Smith
    Participant

    It's because you have your file extension set to .html which opens in a web browser. Web Browsers to not do new lines the same way as text files. Set your extension to .txt since it is a text file, and the file will open with a text file editor like notepad.

    • #72182

      Olaf Soyk
      Participant

      ... and on top of that you have a lot of typos in your code ... procesor vs. processor / time vs. date ... something like this could be a start for you to play with:

      function Get-CPUTemp {
          $CurrentTempKelvin = (Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi").CurrentTemperature / 10
          $currentTempCelsius = $currentTempKelvin – 273.15
          $currentTempFahrenheit = (9/5) * $currentTempCelsius + 32
          $currentTempCelsius.ToString() + " C / " + $currentTempFahrenheit.ToString() + " F / " + $currentTempKelvin + " K"
      }
      
      $temp = Get-CPUTemp
      $time = get-date -Format g
      $processor = wmic cpu get loadpercentage
      
      $processor,$temp ,$time | Out-File  -FilePath C:\Test.txt -Encoding Ascii -Append 

      And BTW: you don't need to explicitly add line breaks – out-file does it for you.

      Edit: the output of wmic does not look nice – you should try to find something better

You must be logged in to reply to this topic.