# Import the ActiveDirectory cmdlets Import-Module ActiveDirectory # List available snapins on your system: Get-PSSnapin # List registered snapins Get-PSSnapin -Registered # Alias: gsnp # Add Snapin: Add-PSSnapin # Examples: Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin # Exchange 2007 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 # Exchange 2010 Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager # WMM (Hyper-V) Add-PSSnapin Quest.Activeroles.ADManagement # Quest commandlets
(You can download the Quest Commandlets from <a href="# Install from http://www.quest.com/powershell/activeroles-server.aspx” title=”# Install from http://www.quest.com/powershell/activeroles-server.aspx“>here.)
You will get an error if you try to add a snapin that is already added. Your script will continue to run but you’ll have a bunch of nasty red letters in your shell. Not too sexy, eh? The way to avoid this is to first check if the snapin is loaded and then only load if it is not.
Do it like this:
# Add Exchange 2007 commandlets (if not added) if(!(Get-PSSnapin | Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.Admin"})) { ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin } # Add Exchange 2010 commandlets (if not added) if(!(Get-PSSnapin | Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) { ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 } # Add Virtual Machine Manager (Hyper-V) commandlets (if not added) if(!(Get-PSSnapin | Where-Object {$_.name -eq "Microsoft.SystemCenter.VirtualMachineManager"})) { ADD-PSSnapin Microsoft.SystemCenter.VirtualMachineManager } # Add Quest commandlets (if not added) if(!(Get-PSSnapin | Where-Object {$_.name -eq "Quest.Activeroles.ADManagement"})) { ADD-PSSnapin Quest.Activeroles.ADManagement }
Head on over to http://blogs.technet.com/b/heyscriptingguy/archive/2010/10/16/learn-how-to-load-and-use-powershell-snap-ins.aspx to learn more about snapins.