diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
index f51a6c9441..55b0f1182c 100644
--- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
+++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
@@ -712,6 +712,14 @@ namespace Microsoft.PowerShell
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 9703420c25..ab893b91a0 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 0e15659bf3..6644b5daef 100644
--- a/test/xUnit/csharp/test_CommandLineParser.cs
+++ b/test/xUnit/csharp/test_CommandLineParser.cs
@@ -65,6 +65,15 @@ namespace PSTests.Parallel
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)