title: (Powershell & C#) ExchangeOnline Cmdlets partially working
body: <p>The issue is a little awkward to explain but essentially I have a piece of C# code that connects &amp; logs into ExchangeOnline via powershell (using System.Management.Automation.Powershell) from that I wish to run some more commands specific to the ExchangeOnline instance.</p>
  <p>I've found that certain commands do not work such as &quot;Get-Mailbox&quot; or &quot;Get-DistributionList&quot;. The error stream I have captured simply says the cmdlet does not exist. However if I try these commands in a powershell window it works.</p>
  <p>The apparently problematic code I have running is pasted below (mix of both powershell and C#):</p>
  <pre><code>                string FullTenantScript = @&quot;
  
  [string]$SizeOfAllMailboxes = ((get-exomailbox -ResultSize Unlimited | get-exomailboxstatistics).TotalItemSize.Value.ToMB() | measure-object -sum).sum
  
  
  $TenantProperties = [pscustomobject]@{
      'Primary Domain' = Get-AzureADDomain | Where-Object IsDefault -Match 'True' | Select -ExpandProperty Name
      'Tenant ID' = Get-AzureADTenantDetail | select -ExpandProperty ObjectId
      'Number of users' = @(Get-AzureADUser -all $true).count
      'Number of Mailboxes' = @(get-exomailbox -ResultSize unlimited).Count
      'Number of Devices' = @(Get-AzureADDevice -all $true).Count
      'Number of Subscriptions' = @(Get-AzureADSubscribedSku).Count
      'Number of Distribution Lists' = @(Get-DistributionGroup).Count
      'Number of Dynamic DLs' = @(Get-DynamicDistributionGroup).Count
      'Number of 365 Groups' = @(Get-UnifiedGroup).Count
      'Size of all mailboxes' = $SizeOfAllMailboxes + ' MB'
  }
  
  
  $TenantProperties
  
  &quot;;
  
  
  
              PowerShell getTenantinfo = PowerShell.Create().AddScript(FullTenantScript);
              Collection&lt;PSObject&gt; TenantInfoResult = getTenantinfo.Invoke();
  </code></pre>
  <p>I've read online that some of the older cmlets are being phased out (for example Get-Mailbox), so instead I use (get-exomailbox). I just don't get why I can run them in a powershell window but not in my program (as they should do essentially the same thing).</p>
  <p>Please also note my program connects into Azure also (using Connect-AzureAD).</p>
  <p>Any help or advice as to why this is happening would be greatly appreciated &amp; any further info required I will provide.</p>
  <p>Thanks,
  Mouse</p>