diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs index 3f413df76ef..a58f3fae297 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs @@ -228,7 +228,7 @@ protected override void BeginProcessing() // If they didn't specify -Append, empty the file if (!_shouldAppend) { - System.IO.File.WriteAllText(effectiveFilePath, string.Empty); + System.IO.File.WriteAllText(effectiveFilePath, string.Empty, Utils.utf8NoBom); } } diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index d350c44d80c..00b773608d7 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -47,6 +47,17 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { } ## function ends here + $defaultEncoding = [System.Text.UTF8Encoding]::new($true) + function GetFileEncoding { + param ( + [string] $filePath + ) + + $reader = [System.IO.StreamReader]::new($filePath, $defaultEncoding, $true) + $reader.Read() | Out-Null + return $reader.CurrentEncoding.BodyName + } + $transcriptFilePath = Join-Path $TestDrive "transcriptdata.txt" Remove-Item $transcriptFilePath -Force -ErrorAction SilentlyContinue } @@ -129,6 +140,31 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { # Nothing should have been returned by the FileSystemWatcher Receive-Job $job | Should -Be $null } + It "Should keep the existing file's encoding if the 'Append' parameter is used"{ + "Text" | Out-File -Encoding unicode $transcriptFilePath + + Start-Transcript -Append -Path $transcriptFilePath + Get-Date | Out-Null + Stop-Transcript + + GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-16' + } + It "Should default to utf8 with 'Append' parameter and file doesn't exist"{ + Start-Transcript -Append -Path $transcriptFilePath + Get-Date | Out-Null + Stop-Transcript + + GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-8' + } + It "Should create a new utf8 encoded file if no 'Append' parameter is set regardless of existing file's encoding"{ + "Text" | Out-File -Encoding unicode $transcriptFilePath + + Start-Transcript -Path $transcriptFilePath + Get-Date | Out-Null + Stop-Transcript + + GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-8' + } It "Transcription should remain active if other runspace in the host get closed" { try { $ps = [powershell]::Create()