0

I am new to powershell but I am trying to learn. Right now, I need to set a scheduled script to run every 10 minutes and ping on of our servers while saving that information to a log file. I have a script that does this but the issue I am facing is with the log.txt

Every time the script runs it overwrites the log file. My questions is: how can I tell powershell to update the log file and not overwrite the contents of it when it is run?

My script: Ping.exe <HOSTNAME> | ForEach {"{0} - {1}" -f (Get-Date),$_} > D:\ping.txt

UsrAku
  • 13
  • 1
  • Does this answer your question? [How to redirect the output of a PowerShell to a file during its execution](https://stackoverflow.com/questions/1215260/how-to-redirect-the-output-of-a-powershell-to-a-file-during-its-execution) – zett42 Jan 20 '21 at 08:46

1 Answers1

1

I believe > D:\ping.txt is telling Powershell to overwrite your log file. Have you tried >> D:\ping.txt instead?

EDIT: For future reference, Microsoft can explain this better than I can: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.1#powershell-redirection-operators

Flash_Steel
  • 606
  • 1
  • 6
  • 16