diff --git a/src/System.Management.Automation/namespaces/EnvironmentProvider.cs b/src/System.Management.Automation/namespaces/EnvironmentProvider.cs index 6be3c301435..fc5952b6459 100644 --- a/src/System.Management.Automation/namespaces/EnvironmentProvider.cs +++ b/src/System.Management.Automation/namespaces/EnvironmentProvider.cs @@ -198,7 +198,15 @@ internal override IDictionary GetSessionStateTable() IDictionary environmentTable = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry entry in environmentTable) { - providerTable.Add((string)entry.Key, entry); + // Windows only: duplicate key (variable name that differs only in case) + // NOTE: Even though this shouldn't happen, it can, e.g. when npm + // creates duplicate environment variables that differ only in case - + // see https://github.com/PowerShell/PowerShell/issues/6305. + // However, because retrieval *by name* later is invariably + // case-Insensitive, in effect only a *single* variable exists. + // We simply ask Environment.GetEnvironmentVariable() which value is + // the effective one, and use that. + providerTable.TryAdd((string)entry.Key, entry); } return providerTable; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 index e34271cc4b9..c1a8bce69cc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Item.Tests.ps1 @@ -116,4 +116,30 @@ Describe "Get-Item" -Tags "CI" { ${result} | Should BeOfType "Microsoft.Win32.RegistryKey" } } + + Context "Environment provider" -tag "CI" { + BeforeAll { + $env:testvar="b" + $env:testVar="a" + } + + AfterAll { + Clear-Item -Path env:testvar -ErrorAction SilentlyContinue + Clear-Item -Path env:testVar -ErrorAction SilentlyContinue + } + + It "get-item testVar" { + (get-item env:\testVar).Value | Should -BeExactly "a" + } + + It "get-item is case-sensitive/insensitive as appropriate" { + $expectedValue = "b" + if($IsWindows) + { + $expectedValue = "a" + } + + (get-item env:\testvar).Value | Should -BeExactly $expectedValue + } + } }