How do I add content to the TOP of a text file from PowerShell?

Hello,

I have a simple log function written that takes a string as an argument ($Msg), prepends it with the current date/time (if $LogTime is passed) and adds it to the bottom of a given txt file.  What I would like instead is a way to add the argument prepended with the date/time to the TOP of the given txt file.  How would I go about doing this??  Here is my current function:

function Write-Log($Msg, [System.Boolean]$LogTime=$true){  
 
    if($LogTime){  
        $date = Get-Date -format dd.MM.yyyy  
        $time = Get-Date -format HH:mm:ss
       Add-Content -Path $LogFile -Value ($date + " " + $time + "   " + $Msg)  
    }  
    else{  
        Add-Content -Path $LogFile -Value $Msg  
    }  
}

Open in new window

electricd7Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You have to replace the file, as there is no way to insert something into a file (no matter if using VBA, PS or anything else).
function Write-Log($Msg, [System.Boolean]$LogTime=$true){  
    if ($LogTime){  
        $date = Get-Date -format dd.MM.yyyy  
        $time = Get-Date -format HH:mm:ss
        $Msg = "$date $time    $Msg"  
    }  
    (Write-Output $Msg; Get-Content $LogFile) | Set-Content $LogFile
}

Open in new window

The parens are important - they force the file to be read in completely before replacing it. Without the file would first be created empty, then read in, because that is the way it works in a pipeline in PS.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
electricd7Author Commented:
Looks like that would work, but what happens if $LogFile gets very large (which is possible in this app)?  Wouldn't this get problematic using Get-Content to pull the entire file into memory each and everytime the function is called?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Of course. But would it be wise to place the log entry at the top every 10 milliseconds? No. Logfiles use append for this reason (because it is an atomic, low-profile action). Tools are very effective in reading the tail of a text file for the same reason - optimized for that case.

Usually, if you want to have a reversed logging file, management is done different: The logfile is cut off at a certain size, moving out older entries. in that case the logging function is deterministic in its timing, and logfiles are fixed or almost fixed size.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.