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


32 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
  14. BEST man, THE BEST, thanks very much.

    ReplyDelete
  15. Searched far and wide for this! thanks

    ReplyDelete
  16. i love you dude, thanks so much!

    ReplyDelete
  17. i love you dude, thanks so much!

    ReplyDelete
  18. Thank you from the Netherlands! ;)

    ReplyDelete
  19. Thank you it worked for me on 2010 Exchange

    ReplyDelete
  20. THIS WORKED

    On-prem exhange 2008 server - get-mailbox cmdlet not recognized
    -Opened elevated powershell then ran add-pssnapin *exchange* -erroraction SilentlyContinue
    Get-mailbox worked!

    thank you!!

    ReplyDelete
  21. god knows what causes this, but thank you!

    ReplyDelete
  22. Wow! Thank you so much. So many complicated options out there but this was very simple and precise.

    ReplyDelete
  23. Same here. This solution from Peter WORKED for me on Exchange 2013. Thanks Peter --Ike E.

    ReplyDelete