Issues with loading Exchange Commandlets from batch file RRS feed

  • Question

  • Hi,

    I have a powershell script which loads active directory module and exchange commandlets. This works fine if I run file.ps1 within powershell ISE. When I try to run file.ps1 from a batch file I get an error saying

    "The Error generated was: Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.E2010 because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception"

    My batch file looks like this:

    call C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -File C:\file\file.ps1

    Please kindly shed some light on this.

    Thank you.

    Thursday, October 10, 2019 6:13 AM

All replies

  • Hi,

    Thanks for your question.

    Please try to add the following line at the top of your powershell script.

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

    Best regards,


    Just do it.

    Thursday, October 10, 2019 7:45 AM
  • Start by removing "call" from the command.


    Thursday, October 10, 2019 1:34 PM
  • Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 - this didn't help. Script fails if I move this section to the top.

    I've also removed the call command from the batch file and still getting the same error.

    Note that I'm a beginner when it comes to PS and this PS was written by an external consultant. Script starts with Function. The section where import-module and add-pssnapin live is almost at the end of the script.

    Again, if I run Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 outside of the script and comment out this command from the script, everthing works.

    I also forgot to mention that this script works fine on the original server. I'm just moving this script to a new server. Is something missing that needs to be installed on the server? I wouldn't have thought so as it works fine when excluding exchange cmdlets from the PS.


    Friday, October 11, 2019 1:06 AM
  • I recommend that you ask the consultant for help with the script. This forum is not available for free script fixing and we cannot guess at what you consultants script is supposed to do.


    Friday, October 11, 2019 1:19 AM
  • wow.. if you had actually read my posts, the script works. it's the exchange cmdlets that doesn't somehow. anyway thanks for nothing.
    Friday, October 11, 2019 1:56 AM
  • What CmdLet. Nothing you have posted is about any CmdLet.

    You really need to get the author or someone formally trained in this to help you understand and to understand what your issue is.

    The Exchange 2010 Snap-In is not compatible with PowerShell versions beyond PS2 unless they have all of the correct patches installed.  Ultimately there is no way to answer your issue with the information you have provided.  Also we do not support scripts you have found or inherited. If you know enough about PowerShell to come up with a good short example of the failure then we might be able to help.  We cannot guess about anything you are doing other than you are trying to run some unspecified script file called "file.ps1".


    Friday, October 11, 2019 2:03 AM