Only import specific cmdlets from Exchange?

I have a script that starts a PSSession to exchange server

$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$exchangeserver/PowerShell/
Import-PSSession $exchangesession

Open in new window

The Import-PSSession will work just fine, however, it takes a while. I think it's because it's loading everything Exchange.
Since I'm only running a couple of cmdlets within powershell after the PSSession import, is there a way to only import specific cmdlets?

Such as Get-Mailbox, Get-User, etc.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


This is very interesting. My understanding of PowerShell is that you cannot do this. Each cmdlet must be part of a module, modules are imported in turn importing each and every cmdlet.

I'm doing a lot of work with DSC Custom Resources at the moment and the same theory applies.

Kindest regards

Subash SundharanIT Infrastructure Architect Commented:
Import-pssession has some parameter which will allow you to import specific commands.. I didn't get a chance to test it but you can try and see if it works for you.

import-pssession $exchangesession -type cmdlet -name Get-Mailbox,Get-User -FormatTypeName *

Open in new window

import-pssession $exchangesession -commandname Get-Mailbox -formattypename *

Open in new window

Ref :

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garryshapeAuthor Commented:
Amazing. I appear to have overlooked that part in the MS examples. This link is much more helpful.
Thank you
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

@Subsun AWESOME, I will take note :)
garryshapeAuthor Commented:
Well, interestingly, my theory was wrong:
Importing the specific commands does not make the script load any faster.
It actually takes about 10 seconds longer.
I timed it because I have a Windows Form script driven by powershell and I time based on how soon the form opens up.
Consistently, with the import-pssession importing the entire module, is 10 seconds faster.

I do wonder importing specific commands helps lessen the strain on the server, if the script is being re-run multiple times, however.
Subash SundharanIT Infrastructure Architect Commented:
Are you sure Importing the session takes the major % of the script starting time? in my experience loading the Exchange Add-in locally on exchange server itself may take 6- 10 seconds depends on the capacity of server. If you think the load time for your script is abnormal then you may post another question including the script to check if anything wrong about the script or to check if anything can be done to make it better.
garryshapeAuthor Commented:
No the majority of the load time is due to the script in its entirety.
I'm just finding that for some reason, it takes about 10 seconds longer when I change the single line of code that imports the pssession to only import cmdlets (Get-Mailbox, Add-MailboxPermissions, Add-ADPermission)
Subash SundharanIT Infrastructure Architect Commented:
I will test this the pssession import in my environment and let you know the result..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.