diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index a04a7edd4..eb46b6a7e 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -173,28 +173,29 @@ namespace Microsoft.PowerShell private const int MaxPipePathLengthMacOS = 104; internal static string[] validParameters = { - "version", - "nologo", - "noexit", #if STAMODE "sta", "mta", #endif - "noprofile", - "noninteractive", - "inputformat", - "outputformat", - "windowstyle", - "encodedcommand", - "configurationname", - "file", - "executionpolicy", "command", - "settingsfile", + "configurationname", + "custompipename", + "encodedcommand", + "executionpolicy", + "file", "help", - "workingdirectory", + "inputformat", + "loadprofile", + "noexit", + "nologo", + "noninteractive", + "noprofile", + "outputformat", "removeworkingdirectorytrailingcharacter", - "custompipename" + "settingsfile", + "version", + "windowstyle", + "workingdirectory" }; internal CommandLineParameterParser(PSHostUserInterface hostUI, string bannerText, string helpText) @@ -742,6 +743,10 @@ namespace Microsoft.PowerShell _noExit = true; noexitSeen = true; } + else if (MatchSwitch(switchKey, "loadprofile", "l")) + { + _skipUserInit = false; + } else if (MatchSwitch(switchKey, "noprofile", "nop")) { _skipUserInit = true; diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx index 7ffba9269..25a7219a4 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx @@ -131,7 +131,7 @@ Type 'help' to get help. [-ConfigurationName <string>] [-CustomPipeName <string>] [-EncodedCommand <Base64EncodedCommand>] [-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}] - [-Interactive] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile] + [-Interactive] [-LoadProfile] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile] [-OutputFormat {Text | XML}] [-Version] [-WindowStyle <style>] [-WorkingDirectory <directoryPath>] @@ -176,11 +176,11 @@ All parameters are case-insensitive. Example: pwsh -ConfigurationName AdminRoles -CustomPipeName - Specifies the name to use for an additional IPC server (named pipe) used for debugging + Specifies the name to use for an additional IPC server (named pipe) used for debugging and other cross-process communication. This offers a predictable mechanism for connecting to other PowerShell instances. Typically used with the CustomPipeName parameter on Enter-PSHostProcess. - Example: + Example: # PowerShell instance 1 pwsh -CustomPipeName mydebugpipe # PowerShell instance 2 @@ -224,6 +224,9 @@ All parameters are case-insensitive. -Interactive | -i Present an interactive prompt to the user. Inverse for NonInteractive parameter. +-LoadProfile | -l + Load the PowerShell profiles. This is the default behavior even if this is not specified. + -NoExit | -noe Does not exit after running startup commands. diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index f7d1c39dd..4e62fb462 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -245,6 +245,42 @@ Describe "ConsoleHost unit tests" -tags "Feature" { } } + Context "-LoadProfile Commandline switch" { + BeforeAll { + if (Test-Path $profile) { + Remove-Item -Path "$profile.backup" -ErrorAction SilentlyContinue + Rename-Item -Path $profile -NewName "$profile.backup" + } + + Set-Content -Path $profile -Value "'profile-loaded'" -Force + } + + AfterAll { + Remove-Item -Path $profile -ErrorAction SilentlyContinue + + if (Test-Path "$profile.backup") { + Rename-Item -Path "$profile.backup" -NewName $profile + } + } + + It "Verifies pwsh will accept switch" -TestCases @( + @{ switch = "-l"}, + @{ switch = "-loadprofile"} + ){ + param($switch) + + if (Test-Path $profile) { + & pwsh $switch -command exit | Should -BeExactly "profile-loaded" + } + else { + # In CI, may not be able to write to $profile location, so just verify that the switch is accepted + # and no error message is in the output + & pwsh $switch -command exit *>&1 | Should -BeNullOrEmpty + } + } + } + + Context "-SettingsFile Commandline switch" { BeforeAll {