Skip to main content

Posts

Showing posts from September, 2022

PowerShell add text or string to text file without opening the file

How to add or append a string or any contents to a text file without opening the text file? Reading and writing in PowerShell is quite fundamental but very important. A simple PowerShell snippet code below, that will add or append a string to the text file dynamically. This is quite useful like monitoring a service, or pinging a server and adding the status or the result to a text file that can be checked later. Text file can be easily tampered, so proper permissions should be set who can write and read. So the file can be a source of truth, if ever auditing has to be done. Here's a simple PowerShell snippet that append a string to a text file or dynamically adding a string to a file. $contents_to_add=Get-Date Add-Content   -Path  "c:\temp\xx.rtf" -Value $contents_to_add #This will append a string to the existing content $file_content = get-content -Path  "c:\temp\xx.rtf" Write-Output $file_content  #Read the content of the files Just a short code but does a won...