Friday, 18 January 2013

Enabling Exchange Cmdlets in Powershell

In order to run Exchange Server commands within a Powershell script, the relevant cmdlets need to be 'called' within Powershell.

This is accomplished with the following line, which is found at the start of nearly all of my Powershell scripts.

#Enable Exchange cmdlets
add-pssnapin *exchange* -erroraction SilentlyContinue

If you attempt to run Exchange commands in Powershell (eg. Get-Mailbox) without loading the Exchange cmdlets first, you will get an error similar to the following;


The term 'get-mailbox' 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.
At line:1 char:12
+ get-mailbox <<<< 
    + CategoryInfo          : ObjectNotFound: (get-mailbox:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


21 comments:

  1. Thank you! That did the trick.

    ReplyDelete
  2. Of course, you need to have the Exchange management tools installed as well. Otherwise your script silently continues but does nothing.

    ReplyDelete
  3. You are a lifesaver! I had to run this within my Exchange Shell on my Exchange 2013 server in order to get the Exchange commands to work. I have no idea why it doesn't run automatically!

    ReplyDelete
  4. this is the best solution ever.
    Thank you Peter

    ReplyDelete
  5. I have been searching the solution for hours. Thank You!!!

    ReplyDelete
  6. Thanks, was looking for this, can it harm if i set it on top of all my exchange PS1 scripts?

    ReplyDelete
  7. thx, so much.

    ReplyDelete
  8. It worked. Thanks!!

    ReplyDelete
  9. Thanks. this did the trick

    ReplyDelete
  10. You are a fucking legend. Same issue as anonymous, my Exchange shell no longer loads this and i was FREAKING OUT!!!! Thank you SOOOOO much!

    ReplyDelete
  11. Thanks!!!!! so much, was searching for solution like this

    ReplyDelete
  12. Thank you thank you thank you!!!!!! This fixed my problem with remotely administering exchange through powershell!!!

    ReplyDelete
  13. Thank you very much for the tip. It worked well, but after a restart again I have to use this commands to enable cmdlets. Any way to keep it permanent? Thanks again.

    ReplyDelete
    Replies
    1. You would need to add the cmdlet to your powershell profile
      Here's some info in the link below:
      http://www.howtogeek.com/50236/customizing-your-powershell-profile/

      Delete