title: PowerShell returning a PSSession object
body: <p>I'm new to PowerShell and trying to write some Exchange Online modular scripts for various tasks. The model I'm working on is to have a control script that calls other parameterised scripts to perform reusable functions. </p>
  
  <p>I've written a connection script, but when I return the PSSession object to my command script I can't import the session, it is somehow getting cast to an object.</p>
  
  <p>How should I be sharing this session around my scripts? </p>
  
  <p>Control script:</p>
  
  <pre><code>$session = .\Connection.ps1 -user admin@mydomain.com
  Import-PSSession $session
  </code></pre>
  
  <p>Connection script (summary):</p>
  
  <pre><code>Param (
  [string]$userparam
  )
  $UserCredential = Get-Credential -userName $userparam
  $Exch = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  return $Exch
  </code></pre>