@@ -1677,7 +1677,7 @@ public SwitchParameter LoadUserProfile
16771677private SwitchParameter _loaduserprofile = SwitchParameter . Present ;
16781678
16791679/// <summary>
1680- /// Starts process ina new window.
1680+ /// Starts process inthe current console window.
16811681/// </summary>
16821682[ Parameter ( ParameterSetName = "Default" ) ]
16831683[ Alias ( "nnw" ) ]
@@ -1965,7 +1965,9 @@ protected override void BeginProcessing()
19651965
19661966startInfo . WindowStyle = _windowstyle ;
19671967
1968- if ( _nonewwindow )
1968+ // When starting a process as another user, the 'CreateNoWindow' property value is ignored and a new window is created.
1969+ // See details at https://learn.microsoft.com/dotnet/api/system.diagnostics.processstartinfo.createnowindow?view=net-9.0#remarks
1970+ if ( _nonewwindow && _credential is null )
19691971{
19701972startInfo . CreateNoWindow = _nonewwindow ;
19711973}
@@ -2413,33 +2415,60 @@ private static byte[] ConvertEnvVarsToByteArray(StringDictionary sd)
24132415
24142416private void SetStartupInfo ( ProcessStartInfo startinfo , ref ProcessNativeMethods . STARTUPINFO lpStartupInfo , ref int creationFlags )
24152417{
2416- bool hasRedirection = false ;
2418+ // If we are starting a process using the current console window, we need to set its standard handles
2419+ // explicitly when they are not redirected because otherwise they won't be set and the new process will
2420+ // fail with the "invalid handle" error.
2421+ //
2422+ // However, if we are starting a process with a new console window, we should not explicitly set those
2423+ // standard handles when they are not redirected, but instead let Windows figure out the default to use
2424+ // when creating the process. Otherwise, the standard input handles of the current window and the new
2425+ // window will get weirdly tied together and cause problems.
2426+ bool hasRedirection = startinfo . CreateNoWindow
2427+ || _redirectstandardinput is notnull
2428+ || _redirectstandardoutput is notnull
2429+ || _redirectstandarderror is notnull ;
2430+
24172431// RedirectionStandardInput
24182432if ( _redirectstandardinput != null )
24192433{
2420- hasRedirection = true ;
24212434startinfo . RedirectStandardInput = true ;
24222435_redirectstandardinput = ResolveFilePath ( _redirectstandardinput ) ;
24232436lpStartupInfo . hStdInput = GetSafeFileHandleForRedirection ( _redirectstandardinput , FileMode . Open ) ;
24242437}
2438+ else if ( startinfo . CreateNoWindow )
2439+ {
2440+ lpStartupInfo . hStdInput = new SafeFileHandle (
2441+ ProcessNativeMethods . GetStdHandle ( - 10 ) ,
2442+ ownsHandle : false ) ;
2443+ }
24252444
24262445// RedirectionStandardOutput
24272446if ( _redirectstandardoutput != null )
24282447{
2429- hasRedirection = true ;
24302448startinfo . RedirectStandardOutput = true ;
24312449_redirectstandardoutput = ResolveFilePath ( _redirectstandardoutput ) ;
24322450lpStartupInfo . hStdOutput = GetSafeFileHandleForRedirection ( _redirectstandardoutput , FileMode . Create ) ;
24332451}
2452+ else if ( startinfo . CreateNoWindow )
2453+ {
2454+ lpStartupInfo . hStdOutput = new SafeFileHandle (
2455+ ProcessNativeMethods . GetStdHandle ( - 11 ) ,
2456+ ownsHandle : false ) ;
2457+ }
24342458
24352459// RedirectionStandardError
24362460if ( _redirectstandarderror != null )
24372461{
2438- hasRedirection = true ;
24392462startinfo . RedirectStandardError = true ;
24402463_redirectstandarderror = ResolveFilePath ( _redirectstandarderror ) ;
24412464lpStartupInfo . hStdError = GetSafeFileHandleForRedirection ( _redirectstandarderror , FileMode . Create ) ;
24422465}
2466+ else if ( startinfo . CreateNoWindow )
2467+ {
2468+ lpStartupInfo . hStdError = new SafeFileHandle (
2469+ ProcessNativeMethods . GetStdHandle ( - 12 ) ,
2470+ ownsHandle : false ) ;
2471+ }
24432472
24442473if ( hasRedirection )
24452474{
@@ -2881,6 +2910,9 @@ internal struct JOBOBJECT_BASIC_PROCESS_ID_LIST
28812910
28822911internal static class ProcessNativeMethods
28832912{
2913+ [ DllImport ( PinvokeDllNames . GetStdHandleDllName , SetLastError = true ) ]
2914+ public static extern IntPtr GetStdHandle ( int whichHandle ) ;
2915+
28842916[ DllImport ( PinvokeDllNames . CreateProcessWithLogonWDllName , CharSet = CharSet . Unicode , SetLastError = true , ExactSpelling = true ) ]
28852917[ return : MarshalAs ( UnmanagedType . Bool ) ]
28862918internal static extern bool CreateProcessWithLogonW ( string userName ,