'prepending a timestamp for note taking that outputs to a text file

Trying to make a note-taking script that gives a timestamp and takes a note then prints it to a file. This script should also not exit until it reads wq! from the last line kinda like vim. I'm having trouble getting the timestamp in the right place. Here is what I have so far. Eventually, I'll get it so that wq! doesn't write or erases when complete... thanks for the help and time of anyone who responds. I prefer PowerShell but don't mind Python.

function Get-TimeStamp {
    
    $timenow = get-date
    return get-date -f $TimeNow.ToUniversalTime().ToString("HH:mm'Z'") #prints UTC timestamp
}

do
{
    #write-output "$(Get-TimeStamp)" | C:\users\vider\Temp\Notes.txt ??? Maybe indexing? 
    #Hashtable? But how to get the time when the note is taken...
    read-host "Notes" | out-file -FilePath C:\users\vider\Temp\Notes.txt -Append
    Start-sleep -milliseconds 250

    $choice = Get-Item -Path C:\users\vider\Temp\Notes.txt | Get-Content -Tail 1
} until ($choice -eq 'wq!') 


Solution 1:[1]

Store the input from Read-Host in variable before outputting it to a file, this way you can add conditional statements to understand if the script should exit the loop or output to the file.

As for getting the timestamp in the right place, capturing the input in a variable as mentioned above, would also make it easier to concatenate the timestamp:

"[$(Get-TimeStamp)] $notes"

See Subexpression operator $( ) for more info on why it is needed in this case.

Here is one example on how you can do it:

function Get-TimeStamp {
    (Get-Date).ToUniversalTime().ToString("HH:mm'Z'")
}

do
{
    $notes = Read-Host "Notes"
    if([string]::IsNullOrWhiteSpace($notes) -or $notes -eq 'wq!') {
        # if this was just pressing ENTER or spaces or `wq!`, go to `until`
        continue
    }

    # if we are here we can assume the input was NOT `wq!` or $null \ empty space
    "[$(Get-TimeStamp)] $notes" | Out-File -FilePath C:\users\vider\Temp\Notes.txt -Append
    Start-sleep -Milliseconds 250

} until ($notes -eq 'wq!')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1