Edit:
Turns out, my original answer is not helpful for the issue the question is about. The following screenshot shows weird behavior in Windows PowerShell 5.1.

I also have to mention that I defined the view to wrap text in columns. It still messes up the color formatting but it does manage to reset the coloring before reaching the next prompt. So that might be a valuable insight.
If you're using a format file, add a Wrap tag in front of the TableColumnItems.
A quickfix for OP's example would be to send the output through Format-Table and specify the "-Wrap" switch like so:
Note that the first prompt is red from when I just sent the object to the console without wrapping. After I output the object with wrapping activated the colors actually get reset.
Original answer:
If you are ok with not color coding your input inline you could define a custom view with a format.ps1xml file.
See here how to create your own view.
For example, for one of my modules I have defined a view like this:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Kra.TimeTrackerTime</Name>
<ViewSelectedBy>
<TypeName>TimeTrackerTime</TypeName>
</ViewSelectedBy>
<GroupBy>
<!--<PropertyName>Date</PropertyName>-->
<ScriptBlock>"$($_.Date.DayOfWeek), $($_.Date.ToString('dd.MM.yyyy'))"</ScriptBlock>
<Label>Group</Label>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>TaskName</Label>
<Width>20</Width>
<Alignment>Left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>BookingCode</Label>
<Width>14</Width>
<Alignment>Left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Status</Label>
<Width>16</Width>
<Alignment>Left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Date</Label>
<Width>12</Width>
<Alignment>Left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>StartTime</Label>
<Width>5</Width>
<Alignment>Right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>EndTime</Label>
<Width>7</Width>
<Alignment>Right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Duration</Label>
<Width>8</Width>
<Alignment>Right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Comment</Label>
<Width>68</Width>
<Alignment>Left</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap />
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>
$Value = $_.TaskName
$Esc = [char]27
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"
}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$Value = $_.BookingCode
$Esc = [char]27
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$Value = $_.Status -join ";`n"
$Esc = [char]27
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if('Pikett' -in $_.Status) {
$Color = '38;2;255;150;60'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$Value = $_.Date.ToString('dd.MM.yyyy')
$Esc = [char]27
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"
}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$Value = $_.StartTime.ToString('HH:mm')
$Esc = [char]27
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"
}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$Value = $_.EndTime.ToString('HH:mm')
$Esc = [char]27
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"
}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$Value = $_.Duration.ToString('hh\hmm')
$Esc = [char]27
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"
}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$Value = $_.Comment
$Esc = [char]27
if($_.IsBreakTime) {
$Color = '38;2;80;130;255'
$Format = $true
}
if($_.IsRunning) {
$Color = '38;2;66;224;22'
$Format = $true
}
if($_.IsArchived) {
$Color = '38;2;120;120;120'
$Format = $true
}
if($Format) {
"$Esc[$($Color)m$($Value)$Esc[0m"
}
else {
$Value
}
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Mind the "ViewSelectedBy" tag. It has a TypeName specified. In your code, you will have to add that TypeName to any object you want to display in this view like this:
#create the object and store it in a variable
$Obj = [PsCustomObject]@{
TaskName = $_.TaskName
BookingCode = $Task.BookingCode
Status = $Status
IsBreakTime = $Task.IsBreakTime
Date = $ObjDate
StartTime = $_.StartTime
EndTime = $_.EndTime
Duration = $Duration
Comment = $_.Comment
IsRunning = $null -eq $_.EndTime
IsArchived = $_.IsArchived
}
#the object has a list of TypeNames
#insert the TypeName specified in the view
$Obj.PsTypeNames.Insert(0,'TimeTrackerTime')
Write-Output $Obj
As long as you have the view loaded as specified in the linked article (you could put that into your PowerShell profile script), any object with the specified TypeName will automatically be presented with that view in the console.
If your code is in a PowerShell module you can add the format.ps1xml file to the module directory and specify the path in the module manifest under "FormatsToProcess".
promptfunction to include your ANSI color string?$function:promptprepend$Esc[39m?$function:prompt = {"$([char]0x1B)[39mPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "}