It is pretty easy to see why PowerShell is so popular, it automates those repetitive tasks that we have to do everyday! But isn’t it silly that this automation adds additional repetitive tasks for us? Take a look at these two scripts to see what I mean:
Both of those scripts make use of the Quest AD cmdlets. If I wanted to run either of these scripts, I would need to run Add-PSSnapinQuest.ActiveRoles.ADManagement to import the Quest AD cmdlets first. When I close and open PowerShell, I have to run the same command again! If I wanted to use any other extension, such as the MDT database tools, I would need to run a similar command.
Lucky for us, we can use the PowerShell Profile to automate this process! Let’s find out how!
What is the PowerShell Profile?
The PowerShell Profile is essentially a first run script that executes every time you start a PowerShell ISE session or a PowerShell Console session. This script allows you to run custom commands, import modules, or automate any other task that you do when you first start PowerShell. Though you can have a profile for each version of PowerShell, these instructions will only walkthrough the ISE profile creation.
Where is the PowerShell Profile?
Launch PowerShell ISE and type $Profile. If you are running PowerShell 3, you should see something like:
The third line in the screenshot above shows us where our PowerShell Profile is located. By default, your PowerShell Profile will exist in the WindowsPowerShell folder within your Documents. Because my documents are redirected, my PowerShell Profile exists on a server. If you just tried browsing to your profile, you probably noticed that it didn’t exist. You can validate this by running test-path $Profile in PowerShell.
Creating the PowerShell Profile
Creating your profile is easy, simply type New-Item -path $profile -type file -force . This will create the Microsoft.PowerShellISE_profile file within the WindowsPowerShell folder.
Take a look in your documents after executing; you should see your new folder and file. Edit Microsoft.PowerShellISE_profile.ps1 by opening it in either NotePad or PowerShell ISE.
Editing the PowerShell Profile
The last step is to customize your PowerShell Profile. While you can get as elaborate as you want, I prefer a simple profile that just extends the functionality of PowerShell. Here is my profile:
Add-PSSnapin Quest.ActiveRoles.ADManagement
Import-Module “C:\Program Files\Microsoft Deployment Toolkit\Bin\MicrosoftDeploymentToolkit.psd1”
Import-Module “\\SERVER\Share\Script Explorer\Modules\MDT\MDTDB.psm1”
Import-Module “\\SERVER\Share\Script Explorer\Modules\DeviceManagement\DeviceManagement.psd1”
write-host “Happy Automation Day!”
Every command (but the last one) imports a PowerShell snap-in or module. This lets me access cmdlets, like the Quest or MDT suites, as soon as PowerShell starts. If you do use my profile to build off of, you’ll need each of those modules and snap-ins. My last command, write-host, is used to visually show me that my profile was loaded correctly (plus, it makes me smile).
Two Last Things
- Redirection your Documents is a huge plus for your PowerShell Profile. Anytime you use PowerShell on any machine, your profile will be automatically loaded.
- If you want to see a more complicated profile, here is an example.
Just a quick one to note, if you are like me and switch between the ISE and the Powershell.exe, they both have separate profiles.
This is useful as you could have more debugging and development tools loaded in the ISE profile and a more generalized set of modules in the .exe profile.
If you wanted them the same you could create a sym link between the two:
cmd /c mklink path\to\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 path\to\source\target\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
Great tip Padd! I love sym links!!
Great blog. Nice and simple explanation to setting up profiles. I like the write-host touch as well. Thank you.
Thanks JT! Little things, like the write-host line, make our jobs a bit more fun. 🙂