diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs index 36b7d3f1450..a271f185f85 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs @@ -20,18 +20,18 @@ public class NewTemporaryFileCommand : Cmdlet protected override void EndProcessing() { string filePath = null; - string tempPath = System.Environment.GetEnvironmentVariable("TEMP"); + string tempPath = Path.GetTempPath(); if (ShouldProcess(tempPath)) { try { filePath = Path.GetTempFileName(); } - catch (Exception e) + catch (IOException ioException) { - WriteError( + ThrowTerminatingError( new ErrorRecord( - e, + ioException, "NewTemporaryFileWriteError", ErrorCategory.WriteError, tempPath)); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index 82a57e53126..aa73e1ccfea 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -14,16 +14,16 @@ A FileInfo object for the temporary file is returned. #> -Describe "NewTemporaryFile" -Tags "CI" { +Describe "New-TemporaryFile" -Tags "CI" { It "creates a new temporary file" { $tempFile = New-TemporaryFile - Test-Path $tempFile | Should be $true + $tempFile | Should Exist $tempFile | Should BeOfType System.IO.FileInfo + $tempFile | Should BeLikeExactly "$([System.IO.Path]::GetTempPath())*" - if(Test-Path $tempFile) - { + if (Test-Path $tempFile) { Remove-Item $tempFile -ErrorAction SilentlyContinue -Force } }