diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 7c57d9532b4..d15d239cb30 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -1790,11 +1790,18 @@ private string ReadLineFromConsole(bool endOnTab, string initialContent, bool ca continue; } + // Handle case where terminal gets reset and the index is outside of the buffer + if (index > s.Length) + { + index = s.Length; + } + // Modify string - if (!insertMode) // then overwrite mode + if (!insertMode && index < s.Length) // then overwrite mode { s = s.Remove(index, 1); } + s = s.Insert(index, keyInfo.KeyChar.ToString()); index++;