Out file to New Line in Text File

Welcome Forums General PowerShell Q&A Out file to New Line in Text File

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #72178

    Participant
    Topics: 6
    Replies: 5
    Points: 0
    Rank: Member

    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

    Participant
    Topics: 6
    Replies: 652
    Points: 11
    Rank: Member

    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

      Participant
      Topics: 1
      Replies: 1330
      Points: 1,674
      Helping Hand
      Rank: Community Hero

      ... 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

The topic ‘Out file to New Line in Text File’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort