Share via


add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin

Question

Thursday, March 13, 2014 5:59 PM

I have a script (below) that was designed to be used by people who do not have access to Exchange. The idea is to check if ActiveSync is enabled for the user, it gives the ability to enable or disable ActiveSync, look for connected devices and remove any stale partnerships.

It works great, in ISE, but run it in Powershell and it says:

"add-pssnapin : The Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin' is not installed on this computer."

This is obviously not true as it loads the snap-in and runs the script fine in ISE (both 32 and 64 bit).

Can anyone tell me why it does not work in Powershell but does work in ISE? Every time I've encountered issues like this before it has been the other way around, fails in ISE but works in Powershell.

I am running Windows 8.1, Powershell 4. The domain is Windows server 2008 R2, Exchange is 2007 SP3.

Code:

add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin
load-module activedirectory
$prompt = @"

************************************************************************************************************************
**                                      Please select from the options below                                          **
**                                                                                                                    **
**                                      s = Search ActiveSync Status                                                  **
**                                      e = Enable ActiveSync for User                                                **
**                                      d = Disable ActiveSync for User                                               **
**                                      c = Search For Device Connections                                             **
**                                      r = Remove Device Connection                                                  **
**                                      x = exit                                                                      **
**                                                                                                                    **
************************************************************************************************************************


"@
Clear-host
Do{
    $originalcolor = $host.UI.RawUI.ForegroundColor
    $host.UI.RawUI.ForegroundColor = "Yellow"
    $choice = Read-Host -Prompt $prompt
    $host.UI.RawUI.ForegroundColor = "Yellow"
    Switch($choice){
        s {$user = Read-Host 'Enter the email address to search for ([email protected])' ; Get-CASMailbox $user | Select-Object ActiveSyncEnabled}
        c {$user = Read-Host 'Enter the mailbox to search for (Firstname.Lastname)' ; Get-ActiveSyncDeviceStatistics -Mailbox $user | 
    ft DeviceType, DeviceUserAgent, LastSuccessSync}
        r {$user = Read-Host 'Enter the email address ([email protected]) you wish to remove inactive (no sync in last 7 days) partnership(s) from' ; Get-ActiveSyncDeviceStatistics -Mailbox $user | Where-Object {$_.LastSuccessSync -le (Get-Date).AddDays("-7")} | Remove-ActiveSyncDevice -confirm:$false}
        e {$user = Read-Host 'Enter the email address to enable ActiveSync for ([email protected])' ; get-mailbox $user | set-Casmailbox -ActiveSyncEnabled:$true}
        d {$user = Read-Host 'Enter the email address to disable ActiveSync for ([email protected])' ; get-mailbox "$user" | set-Casmailbox -ActiveSyncEnabled:$false}
        x {break}

        default {write-host "Invalid selection, please try again." -ForegroundColor Red}
    }
}Until($choice -eq "x")

All replies (8)

Sunday, March 16, 2014 8:07 AM ✅Answered

Hi GADavies,

I'm still not sure for why you get that error, it seems everything can works now.

To load the Exchange Management Shell snap-in manually in Windows PowerShell:

For Exchange 2007, run:

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin

For Exchange 2010, run:

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

So it can not work to load with the cmdlet: add-pssnapin Microsoft.Exchange.Management.PowerShell.E2007

You could also enter the following to get a list of Powershell Snap-Ins that are registered on the system and that you could use:

Get-PSSnapIn -Registered

If I have any misunderstanding, please let me know.

If you have any feedback on our support, please click here.

Best Regards,

Anna

TechNet Community Support


Sunday, March 23, 2014 3:07 PM ✅Answered

Hi GADavies,

Please run Get-PSSnapin –Registered and check if the pssnapin“Microsoft.Exchange.Management.PowerShell.Admin”exist.
And check this registry "HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\Microsoft.Exchange.Management.PowerShell.Admin"

If you have any feedback on our support, please click here.

Best Regards,

Anna

TechNet Community Support


Friday, March 14, 2014 6:20 AM

Hi GADavies,

Please try the cmdlet "add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010" and feedback, which can be found in this thread:

How to add exchange 2010 PSSnapin to ISE?

If you have any feedback on our support, please click here.

Best Regards,

Anna

TechNet Community Support


Friday, March 14, 2014 2:17 PM

We run Exchange 2007 at the moment, so I changed the cmdlet to

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2007

the result was

add-pssnapin : The Windows PowerShell snap-in
'Microsoft.Exchange.Management.PowerShell.E2007' is not installed on this
computer.

Just for laughs I left it as E2010, I got

add-pssnapin : The Windows PowerShell snap-in
'Microsoft.Exchange.Management.PowerShell.E2010' is not installed on this
*computer.*Which is what I expected. I had actually tried this previously and got the same result.

The weird thing is, if I try using

add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin

As a one liner in 64 bit PS, it does not throw an error but run it as part of a script it does error out. So, as it is now, I can run the script in either 32 or 64 bit ISE, I can run the cmdlet to add the snap-in in 64 bit Powershell as a one liner but cannot run it as part of a script in any version of Powershell.

This is confusing to say the least!

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2007

errors out in both 32 and 64 bit PS as a one liner, just to add to the confusion

Additional note:

Running the script in PS 64 bit gives the error detailed above, but then it works, I can run the searches, enable/disable and remove stale partnerships so now I'm totally lost!


Thursday, March 20, 2014 2:25 AM

Hi GADavies,

I’m writing to just check in to see if the suggestions were helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up.

If you have any feedback on our support, please click here.

Best Regards,

Anna

TechNet Community Support


Thursday, March 20, 2014 3:28 PM

I'm still trying to figure out why I get the error, but the script now works on my machine, not on any other!


Monday, March 24, 2014 2:12 PM

Interesting results.

Both Microsoft.Exchange.Management.PowerShell.Admin and Microsoft.Exchange.Management.PowerShell.Support are registered, the registry entry is also there but I was still getting the same error.

I eventually gave up and rebuilt my machine, reinstalled everything and it now works fine, no error message, looks like a Microsoft Mystery Theatre production.

Thanks for the help everyone.


Monday, April 21, 2014 1:57 PM

Any chance that you're no longer running it in PS 4.0 or that a newer update was applied?  I've come across similar issues with add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 when used in a script that I have a shortcut to.  Just curious if one of the changes may be similar to solving my issue.