How to setup a PowerShell profile and load the PowerCLI Snapin.

My PowerShell profile has been the most used script that I have wrote, and its not really a script.  The PowerShell profile runs every time that PowerShell ISE is opened. With this profile you can customize it to your needs.  It can load frequently used snap ins, functions or even custom properties.  When I help others with scripts, I forget that not everybody has a PowerShell profile setup and customized like myself.

$exist = Test-path $profile
If ($exist -eq $false) {New-item –type file –force $profile}
Else {Write-Host "PowerShell profile is already created"}

This will generate the PowerShell profile. Once you have the profile, you can customize it to you to amazing things.

notepad $profile

This will open the newly generated PowerShell profile with notepad so you can add your code snippets and functions.

set-location C:\Scripts
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer vCenter_Name -User Domain\NotesofaScripter

This is the start of my profile. This sets the working location to my scripts folder. The next line is the most important line, this one loads the PowerShell snap in for PowerCLI.
Then the last line establishes the connection to vCenter. I don’t like to put passwords in an unencrypted file, so just using the ‘-User’ parameter will force a dialog box to enter the username and password.

Credential Pop-up from PowerShell Profile
Credential Pop-up from PowerShell Profile
Function unlock ($username)
{ 
	unlock-adaccount -identity $username
}

Above is by far my most used function that I have in my profile. With this I just need to type ‘unlock useraccount’ at the prompt in PowerShell ISE and it runs and unlocks the user. I also have built in the script from my last post (link) into the my PowerShell Profile.

 

-Stuart

2 thoughts on “How to setup a PowerShell profile and load the PowerCLI Snapin.”

    1. That is correct Winford. In my profile, I have a lot of frequently used scripts that have been turned into functions.

      – Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *