Load VMware PowerCLI snap-in automatically in PowerShell ISE
The PowerShell Integrated Scripting Environment (ISE) is a very handy application when dealing with the PowerShell. And because of this, the ISE is also a very handy application when dealing with VMware PowerCLI. When I write a script or a one-liner, one of the first things I do is to load the necessary snap-ins. And because I’m lazy, I’m trying to automate everything, what I have to do more than once. So how can I load the necessary snap-ins automatically when starting PowerShell ISE? The Windows PowerShell profile will help you. This is a simple text file, or to be more precise, a PowerShell script. Because of this, you can write everything (cmdlets, scripts, functions etc.) in this script file, and it will be executed when you start the PowerShell or the PowerShell ISE. Please note, that there are two profile files: One for the PowerShell and one for the PowerShell ISE. But where can you find the Windows PowerShell profile files? The path to the PowerShell profile is returned by the built-in variable $profile.
Open a PowerShell windows:
PS C:\Users\patrick> $profile
C:\Users\patrick\Documents\WindowsPowerShell\Microsoft.PowerShell\_profile.ps1
PS C:\Users\patrick>
When you try the same in the ISE, you will get this output:
PS C:\Users\patrick> $profile
C:\Users\patrick\Documents\WindowsPowerShell\Microsoft.PowerShellISE\_profile.ps1
PS C:\Users\patrick>
You see the difference? Depending on your PowerShell environment, the PowerShell reads a different profile file on startup. Usually the files doesn’t exist, except you created them. Check if the files exist. If not, this command will create an empty profile file. Depending on if the command is executed in a PowerShell windows or in the PowerShell ISE, a profile file for PowerShell or PowerShell ISE is created.
PS C:\Users\patrick> Test-Path $profile
False
PS C:\Users\patrick> New-Item -path $profile -type file -force | Out-Null
PS C:\Users\patrick> Test-Path $profile
True
PS C:\Users\patrick>
Close the PowerShell ISE. Now you can open the file with your favorite editor and add the command Add-PsSnapin.
# Load Windows PowerShell cmdlets for managing vSphere
Add-PsSnapin VMware.VimAutomation.Core -ea "SilentlyContinue"
Save the file and open the PowerShell ISE. A Get-PSSnapin should return, that the VMware.VimAutomation.Core module was loaded. You should also notice that you can now use PowerCLI cmdlets. Everytime you open the PowerShell ISE, the VMware.VimAutomation.Core snap-in is automatically loaded.