title: Remote Powershell: The term 'Where-Object' is not recognized in "Invoke-Command"
body: <p>When iam running the following command locally on my Exchange Server,</p>
  
  <pre><code>$username = 'username'; $password = ConvertTo-SecureString 'password' -asplaintext -force; $UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,$password; $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'http://exchange.myfirm.local/PowerShell' -Authentication Kerberos -Credential $UserCredential; Invoke-Command -Session $Session {Get-Mailbox -RecipientTypeDetails 'SharedMailbox' | Get-MailboxPermission | Where-Object User -like 'anotherusername' | fl identity}
  </code></pre>
  
  <p>i get this error message:</p>
  
  <pre><code>The term 'Where-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
      + CategoryInfo          : ObjectNotFound: (Where-Object:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
      + PSComputerName        : exchange.myfirm.local
  </code></pre>
  
  <p>How could i add this cmdlet to this "session" or get arround this problem?</p>
  
  <p>Background:
  Iam trying to automate some stuff remotely with a ruby script using winrm and havent found any other alternative to connect to the Exchange Powershell "Layer". </p>
  
  <p>On the "real" Exchange Management Shell, this command works fine (so i dont think the whole powershell installation is broken (Module Microsoft.PowerShell.Utility)). I think its more the way i connect to this shell over the "New-PSSession" and "Invoke-Command" thing.</p>
  
  <p>If it helps, here is the ruby code (but i think this has nothing to do with the way i connect to the powershell remotely - but if there is a much better way, let me know, please):</p>
  
  <pre class="lang-rb prettyprint-override"><code>require 'winrm'
  
  $exch_user = 'username'
  $exch_password = 'password'
  
  endpoint = 'http://ipaddress:5985/wsman' # this is the connection URI to the Exchange 
  connectionuri = 'http://exchange.myfirm.local/PowerShell' # keep in mind that the PowerShell URI MAY have to be a FQDN
  winrm = WinRM::WinRMWebService.new(endpoint, :plaintext, :user =&gt; $exch_user, :pass =&gt; $exch_password, :basic_auth_only =&gt; true)
  executor = winrm.create_executor
  
  exch_cmd = "Get-Mailbox anotherusername | Select-Object -expand EMailAddresses alias"
  
  command = "powershell -NonInteractive -WindowStyle Hidden -command \" $username = '#{$exch_user}'; $password = ConvertTo-SecureString '#{$exch_password}' -asplaintext -force; $UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,$password; $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri '#{connectionuri}' -Authentication Kerberos -Credential $UserCredential; Invoke-Command -Session $Session {#{exch_cmd}}\""
  
  #$i = 0
  $a ||= Array.new
  executor.run_cmd(command) do |stdout, stderr|
      stdout.each_line do |l|
          $a &lt;&lt; l[21..-1] if l.match(/^AddressString/)
      end
    #STDOUT.print "#{stdout}"
    #STDERR.print stderr
    #STDOUT.print stdout.class #string
    #STDOUT.print stdout.scan 'AddressString'
    #$i += 1
  end
  
  puts $a.sort
  </code></pre>