Run powershell with specific powershell modules loaded

Posted on 2013-02-06
Last Modified: 2013-03-06

I'd like to run powershell-commands to vmware, AD etc.
Now when I run a powershell ps1 (right mouse "run with powershell" it hasn't the modules loaded.

I guess that's something to do with profiles? How does it work + how can I do this automatically by system startup (load + connect to vcenter) so preventing slow down startup of a script (if it needs to load the modules each time I run a script it takes seconds before it runs).

Thanks for your advice.
Question by:janhoedt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +3
LVL 121
ID: 38858833
I would recommend using the following, also VMware recommends the use of PowerCLI, which is PowerShell interface with vSphere-specific additions.

One of the simplest PowerCLI examples, and something which is actually extremely useful on its own, is the Get-VM cmdlet. Which lists VMs.

PowerCLI can be a bit overwhelming to use,  But it can be enhanced with a nice toolset and a library of preconfigured scripts to jump-start your mass changes, which is possible with PowerGUI


VMware Community Pack
LVL 40

Expert Comment

ID: 38858841
You need to import the respective modules while running the script..

You can do this by adding command Import-Module <Module Name> @ the beginning of the script..

For example...

    Import-Module -Name Activedirectory

    Import-Module -Name c:\snapins\Scripting\Scripting.dll


Or use Add-pssnapin
Ref :
LVL 10

Expert Comment

by:Prashant Girennavar
ID: 38859255

 If you want to use PowerShell for Actrive Directory (On you client system) , You need install RSAT First to get the AD Snap in to your client system.

RSAT Can be downloded from here..

once done run import-module activeDirectory

If you are runing powershell on DC , then no need of installtion RSAT , SUBSUN suggestion will work .


Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

LVL 10

Expert Comment

by:Pramod Ubhe
ID: 38859503
You need to add all the import commands in profile.ps1 and keep it in the folder where powershell.exe is. It will load all the modules mentioned in profile whenever powershell starts.
This is not much secure method. Also try profiles.ps1, don't remember if S is needed in that file name.

Author Comment

ID: 38869550
Please advise more on the profile and how it works/to configure it.
LVL 121
ID: 38869563
Much easier to use PowerGUI as a beginner.

Author Comment

ID: 38876853
Ok, but I want to run a powershell command with right-click button. Then I 'd need profiles, right? Please advise on that (had already powergui installed).
LVL 10

Expert Comment

by:Pramod Ubhe
ID: 38876898
What is the OS and powershell version?
I will give you a detailed write up tomorrow.

Author Comment

ID: 38877158
Windows 2008 r2 64 bit and windows 8 32 bit mainly. No changes to default powershell but I can update them. How does this matter?
LVL 10

Expert Comment

by:Pramod Ubhe
ID: 38877180
Not sure about windows 8 but I can definitely tell you how to do it on 2008 tomorrow as i am away from my computer right now. You will have to do some research on windows 8.
LVL 40

Expert Comment

ID: 38877247
Did you try adding modules inside script as I suggested in my previous comment?
LVL 10

Expert Comment

by:Pramod Ubhe
ID: 38879218
For windows 2008 R2 follow these steps -

1. Open powershell and run this command -  Get-ExecutionPolicy

2. Execution policy should be at least "RemoteSigned" or change it by command Set-ExecutionPolicy
*** This is a security risk and you need to check your org. policies before changing the execution type.

3. Download attached file and modify it as per your needs, I hope it is self explanatory.

4. Rename the file from Profile.txt to Profile.PS1 and put it in below location -

5. Open powershell and see if the commands in profile.ps1 are loaded/executed.

Above steps are much simpler but you can also refer below links if you need more info -

Author Comment

ID: 38892687
Thanks pramod_ubhe!

Now, 3 more questions:

1. I would like to impport SCCM 2012 modules on the sccm-server.
To load them, I need to execute this command:
Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
How come in your example the path is not needed + can I add them with the path in the profile?

2.howto execute these commands remotely? Do I need these modules loaded remotely or can I exectue them from another pc/workstation?

3.What about Windows Powershell ISE? It's included in Powershell 3.0 and completes/suggests completions. Is it as good as Powergui?
LVL 10

Accepted Solution

Pramod Ubhe earned 500 total points
ID: 38921430
Sorry for the delay in response.

1. You can directly put the mentioned command in profile file and it will be executed whenever you open powershell. You can add path in profile.
2. To execute these commands remotely, you need to include them in your PS script.
3. ISE is a very good tool to write ps script. PowerGUI Has more functionalities but I like ISE, it's your choice both are good tools.

I may have not understood your questions but I would recommend that you do some testing on a test machine so that you will get better understanding of profile.ps1 Basically it executes everything written in it whenever you open powershell.
LVL 25

Expert Comment

ID: 38921666
A few additional comments..

If you have Powershell v3.0, it will autoload the necessary modules when called, as long as they are loaded on the system.  I.e. If you have the AD tools installed on the system, and you call get-aduser, it will dynamically load the ActiveDirectory module.   Your Windows 8 systems will have PS3 on them already, and you can load it yourself on Win2k8R2.

The MS ISE in v3.0 is a substantial upgrade from v2.0, but it is not as good as PowerGUI.  

If you want to execute the commands remotely with v3.0, you will want to enable remoting on all your target devices, and spend time with the invoke-command cmdlet. It will allow you to easily run your commands on the remote machines.    Check the remoting help topic in powershell.  I believe it is about_remoting.  


Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Teach the user how to use vSphere Update Manager to update the VMware Tools and virtual machine hardware version Open vSphere Client: Review manual processes for updating VMware Tools and virtual hardware versions: Create a new baseline group in vSp…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question