title: Powershell - Write to Text File
body: <p>Basically I am extracting some log entries from an Event Log, formatting it, and then want to write it to a text file I will then email over. This is for Windows Server Backup monitoring purposes. </p>
  
  <pre><code>New-Item -ItemType directory -Path C:\WSBReports\
  New-Item -ItemType file -Path C:\WSBReports\DailyReport.txt
  
  $yestDate = (Get-Date) - (New-TimeSpan -day 1)
  # echo $yestDate
  Get-WinEvent -logname "Microsoft-Windows-Backup" |
              where {$_.timecreated -ge $yesterday} |
  
  Format-Table TimeCreated, ID, ProviderName, Message -AutoSize -Wrap &gt; C:\WSB_Reports\DailyReport.txt
  </code></pre>
  
  <p>Firstly - it says it cannot write to the file, because it doesn't exist? Even though I created it above.</p>
  
  <p>And also - I think the logic is wrong as I need to always overwrite this file each time the script runs, is this possible?</p>