diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
index 11de41ce95f..279d182292d 100644
--- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
+++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
@@ -700,6 +700,14 @@ internal void Parse(string[] args)
throw new InvalidOperationException("This instance has already been used. Create a new instance.");
}
+ for (int i = 0; i < args.Length; i++)
+ {
+ if (args[i] is null)
+ {
+ throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs);
+ }
+ }
+
// Indicates that we've called this method on this instance, and that when it's done, the state variables
// will reflect the parse.
_dirty = true;
diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx
index 9703420c254..ab893b91a0a 100644
--- a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx
+++ b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx
@@ -219,4 +219,7 @@ Valid formats are:
Parameter -STA is not supported on this platform.
+
+ The specified arguments must not contain null elements.
+
diff --git a/test/xUnit/csharp/test_CommandLineParser.cs b/test/xUnit/csharp/test_CommandLineParser.cs
index d4b722b7f4a..3e25c84c1bc 100644
--- a/test/xUnit/csharp/test_CommandLineParser.cs
+++ b/test/xUnit/csharp/test_CommandLineParser.cs
@@ -65,6 +65,15 @@ public static void Test_Throws_On_Reuse()
Assert.Throws(() => cpp.Parse(new string[0]));
}
+ [Theory]
+ [InlineData("arg1", null, "arg3")]
+ public static void Test_ARGS_With_Null(params string[] commandLine)
+ {
+ var cpp = new CommandLineParameterParser();
+
+ Assert.Throws(() => cpp.Parse(commandLine));
+ }
+
[Theory]
[InlineData("noexistfilename")]
public static void TestDefaultParameterIsFileName_Not_Exist(params string[] commandLine)