Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs (C#)

Code before:
            sd.Keys.CopyTo(array, 0);
            string[] strArray2 = new string[sd.Count];
            sd.Values.CopyTo(strArray2, 0);
            Array.Sort(array, strArray2, StringComparer.OrdinalIgnoreCase);
            StringBuilder builder = new();
            for (int i = 0; i < sd.Count; i++)
            {
                builder.Append(array[i]);
                builder.Append('=');
                builder.Append(strArray2[i]);
                builder.Append('\0');
            }

            builder.Append('\0');

            // Use Unicode encoding
            bytes = Encoding.Unicode.GetBytes(builder.ToString());

            return bytes;
        }

        private void SetStartupInfo(ProcessStartInfo startinfo, ref ProcessNativeMethods.STARTUPINFO lpStartupInfo, ref int creationFlags)
        {
            // If we are starting a process using the current console window, we need to set its standard handlers
            // explicitly when they are not redirected because otherwise they won't be set and the new process will
            // fail with the "invalid handler" error.
            //
            // However, if we are starting a process with a new console window, we should not explicitly set those
            // standard handlers when they are not redirected, but instead let Windows figure out the default to use
            // when creating the process. Otherwise, the standard input handlers of the current window and the new
            // window will get weirdly tied together and cause problems.
            bool hasRedirection = startinfo.CreateNoWindow
                || _redirectstandardinput is not null
                || _redirectstandardoutput is not null
                || _redirectstandarderror is not null;

            // RedirectionStandardInput
            if (_redirectstandardinput != null)
            {
                startinfo.RedirectStandardInput = true;
                _redirectstandardinput = ResolveFilePath(_redirectstandardinput);
                lpStartupInfo.hStdInput = GetSafeFileHandleForRedirection(_redirectstandardinput, FileMode.Open);
            }
            else if (startinfo.CreateNoWindow)
            {
                lpStartupInfo.hStdInput = new SafeFileHandle(
                    ProcessNativeMethods.GetStdHandle(-10),
                    ownsHandle: false);
            }

            // RedirectionStandardOutput

Review comment (bug):
```suggestion
            // If we are starting a process using the current console window, we need to set its standard handles
            // explicitly when they are not redirected because otherwise they won't be set and the new process will
            // fail with the "invalid handle" error.
```

Code after:
            sd.Keys.CopyTo(array, 0);
            string[] strArray2 = new string[sd.Count];
            sd.Values.CopyTo(strArray2, 0);
            Array.Sort(array, strArray2, StringComparer.OrdinalIgnoreCase);
            StringBuilder builder = new();
            for (int i = 0; i < sd.Count; i++)
            {
                builder.Append(array[i]);
                builder.Append('=');
                builder.Append(strArray2[i]);
                builder.Append('\0');
            }

            builder.Append('\0');

            // Use Unicode encoding
            bytes = Encoding.Unicode.GetBytes(builder.ToString());

            return bytes;
        }

        private void SetStartupInfo(ProcessStartInfo startinfo, ref ProcessNativeMethods.STARTUPINFO lpStartupInfo, ref int creationFlags)
        {
            // If we are starting a process using the current console window, we need to set its standard handles
            // explicitly when they are not redirected because otherwise they won't be set and the new process will
            // fail with the "invalid handle" error.
            //
            // However, if we are starting a process with a new console window, we should not explicitly set those
            // standard handles when they are not redirected, but instead let Windows figure out the default to use
            // when creating the process. Otherwise, the standard input handles of the current window and the new
            // window will get weirdly tied together and cause problems.
            bool hasRedirection = startinfo.CreateNoWindow
                || _redirectstandardinput is not null
                || _redirectstandardoutput is not null
                || _redirectstandarderror is not null;

            // RedirectionStandardInput
            if (_redirectstandardinput != null)
            {
                startinfo.RedirectStandardInput = true;
                _redirectstandardinput = ResolveFilePath(_redirectstandardinput);
                lpStartupInfo.hStdInput = GetSafeFileHandleForRedirection(_redirectstandardinput, FileMode.Open);
            }
            else if (startinfo.CreateNoWindow)
            {
                lpStartupInfo.hStdInput = new SafeFileHandle(
                    ProcessNativeMethods.GetStdHandle(-10),
                    ownsHandle: false);
            }

            // RedirectionStandardOutput