title: PowerShell Redirection Does Not Always Work
body: <p>Redirecting the <strong>Warning Stream</strong> for <code>New-PSSession</code> does not work as I believe it should. I am always getting the following (<strong>yellow</strong>) warning message to console regardless of what I do to try suppress / redirect it:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>WARNING: Using New-PSSession with Basic Authentication is going to be deprecated soon, checkout https://aka.ms/exops-docs for using Exchange Online V2 Module which uses Modern Authentication.
  </code></pre>
  
  <p>What am I missing about PowerShell redirection / streams that's allowing that message to permeate through to the console / std_out?</p>
  
  <h1>What doesn't work</h1>
  
  <p>I have tried the following, as per <em>the wisdom of the internet</em>:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>New-PSSession *&gt;$null ...
  </code></pre>
  
  <pre class="lang-bsh prettyprint-override"><code>New-PSSession ... | Out-Null
  </code></pre>
  
  <pre class="lang-bsh prettyprint-override"><code>New-PSSession *&gt;$null ... -OutVariable x -ErrorVariable y -WarningVariable z
  </code></pre>
  
  <pre class="lang-bsh prettyprint-override"><code>New-PSSession *&gt;$null ... -WarningAction SilentlyContinue
  </code></pre>
  
  <pre class="lang-bsh prettyprint-override"><code>$WarningPreference = 'SilentlyContinue'
  New-PSSession ...
  </code></pre>
  
  <p>I've even tried taking temporary control of <code>std_out</code>:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$std_out = [System.Console]::Out
  $out_writer = New-Object IO.StringWriter
  [System.Console]::SetOut($out_writer)
  
  $std_err = [System.Console]::Error
  $err_writer = New-Object IO.StringWriter
  [System.Console]::SetOut($err_writer)
  
  $sess = New-PSSession ...
  
  [System.Console]::SetOut($std_out)
  [System.Console]::SetError($std_err)
  </code></pre>
  
  <p>Along with every combination of above that I can think of, plus several more methods that I'm forgetting.</p>
  
  <h1>What <em>should</em> work</h1>
  
  <p>Testing each of those techniques using <code>Invoke-Command</code> works for other warnings, as expected:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>
  $std_out = [System.Console]::Out
  $out_writer = New-Object IO.StringWriter
  [System.Console]::SetOut($out_writer)
  
  $std_err = [System.Console]::Error
  $err_writer = New-Object IO.StringWriter
  [System.Console]::SetOut($err_writer)
  
  Invoke-Command -ScriptBlock {
  
      Write-Error "My Error"
      Write-Warning "My Warning"
      [console]::WriteLine('Directly to std_out!')
  
  } *&gt;$null -OutVariable x -ErrorVariable y -WarningVariable z
  
  [System.Console]::SetOut($std_out)
  [System.Console]::SetError($std_err)
  
  </code></pre>
  
  <p>But nothing I have tried will suppress or redirect that message from <code>New-PSSession</code>.</p>
  
  <h1>To reproduce</h1>
  
  <pre class="lang-bsh prettyprint-override"><code>param (
      $username,
      $password
  )
  
  $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
  $creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
  $URL = "https://ps.outlook.com/powershell"
  New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $creds -Authentication Basic -AllowRedirection
  </code></pre>
  
  <h1>Environments Tested</h1>
  
  <p>Windows 10:</p>
  
  <pre><code>Name                           Value
  ----                           -----
  PSVersion                      7.1.0-preview.2
  PSEdition                      Core
  GitCommitId                    7.1.0-preview.2
  OS                             Microsoft Windows 10.0.18363
  Platform                       Win32NT
  PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
  PSRemotingProtocolVersion      2.3
  SerializationVersion           1.1.0.1
  WSManStackVersion              3.0
  </code></pre>
  
  <p>Windows 10:</p>
  
  <pre><code>Name                           Value
  ----                           -----
  PSVersion                      5.1.18362.752
  PSEdition                      Desktop
  PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
  BuildVersion                   10.0.18362.752
  CLRVersion                     4.0.30319.42000
  WSManStackVersion              3.0
  PSRemotingProtocolVersion      2.3
  SerializationVersion           1.1.0.1
  </code></pre>
  
  <p>Server 2019:</p>
  
  <pre><code>Name                           Value
  ----                           -----
  PSVersion                      5.1.17763.1007
  PSEdition                      Desktop
  PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
  BuildVersion                   10.0.17763.1007
  CLRVersion                     4.0.30319.42000
  WSManStackVersion              3.0
  PSRemotingProtocolVersion      2.3
  SerializationVersion           1.1.0.1
  </code></pre>
  
  <p>WinRM (Identical for all systems):</p>
  
  <pre><code>Config
      MaxEnvelopeSizekb = 500
      MaxTimeoutms = 60000
      MaxBatchItems = 32000
      MaxProviderRequests = 4294967295
      Client
          NetworkDelayms = 5000
          URLPrefix = wsman
          AllowUnencrypted = false
          Auth
              Basic = true
              Digest = true
              Kerberos = true
              Negotiate = true
              Certificate = true
              CredSSP = false
          DefaultPorts
              HTTP = 5985
              HTTPS = 5986
          TrustedHosts
      Service
          RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
          MaxConcurrentOperations = 4294967295
          MaxConcurrentOperationsPerUser = 1500
          EnumerationTimeoutms = 240000
          MaxConnections = 300
          MaxPacketRetrievalTimeSeconds = 120
          AllowUnencrypted = false
          Auth
              Basic = false
              Kerberos = true
              Negotiate = true
              Certificate = false
              CredSSP = false
              CbtHardeningLevel = Relaxed
          DefaultPorts
              HTTP = 5985
              HTTPS = 5986
          IPv4Filter = *
          IPv6Filter = *
          EnableCompatibilityHttpListener = false
          EnableCompatibilityHttpsListener = false
          CertificateThumbprint
          AllowRemoteAccess = true
      Winrs
          AllowRemoteShellAccess = true
          IdleTimeout = 7200000
          MaxConcurrentUsers = 2147483647
          MaxShellRunTime = 2147483647
          MaxProcessesPerShell = 2147483647
          MaxMemoryPerShellMB = 2147483647
          MaxShellsPerUser = 2147483647
  </code></pre>