PowerShell - Example profile

I use/manage/access Active Directory, Exchange, Operations Manager, Office Communications Server, Configuration Manager(when they make me), VMware, etc. Lots of applications that are accessible via PowerShell. I have been using add-psssnapin or import-module to load what I needed on an individual basis and telling people to use that method. Not a happy answer when setting things up as you always find later you missed something.

When you are new to PowerShell there is mention and piecemeal examples of what should/might go into your PowerShell profile and what is just useful to have starting out. These are generally in articles and how to's and your $profile really is something that is and should be yours to configure and customize. Some of the more fun and interesting looking stuff is way beyond the realm of the beginners to understand so leads to frustration and many just want to know what 'should' be or is 'useful' to have in there.

We are deploying more servers and services that are accessible by PowerShell, in preparation for this I have been evangelizing PowerShell at work for a few years now and have been writing install and configure documents for my team. The question of what should be in my $profile has been coming up more often. Various searches came back with snippets, parts or developer profiles that aren't really all that useful to starting script writers.

Some of this is from those snippets and half remembered readings. It should get anyone started and some of it is written to show slight differences in how things can be done. Add-PSSnapin vs Add-Module for example.

This $Profile.CurrentUserAllHosts will load all registered snap ins and modules on your system. Comments welcome.

## Example PowerShell script profile
##   by: Steven Peck
## date: 03June2010
## This will load all snapins and modules on a given system, setup a PS drive and an alias to Notepad.exe
## Add Snapins
# Get and load all registered Snapins on this system
# To load individual Snapins use Add-PSSnapin
$snapins = Get-PSSnapin -Registered
$snapins | Add-PSSnapin
## Add modules
Get-Module -ListAvailable | Import-Module
# For more control comment out the above line and edit modules as desired in the array.
# $modules = "bsonposh","ActiveDirectory","AppLocker","BitsTransfer","PSDiagnostics","TroubleshootingPack"
# foreach ($module in $modules) {
#  import-module $module
#   }
## List installed Snapins and Modules
Get-PSSnapin | Format-Table -autosize PSVersion, Name
Get-Module | Format-Table -autosize ModuleType, Name
## List command to explore cmdlets
Write-Host "To list all cmdlets from a specific module use " -NoNewLine
Write-Host -foregroundcolor yellow "PS:> Get-Command -Module <Name>"
## Add functions that extend environment cmds
# Some modules add funtions via dot sourced files.  For Example...
# . "C:\support\WindowsPowerShell\OCS-ALL.ps1"
## Create drive called PS: and sets PowerShell starting location
New-PSDrive -Name PS -Root C:\support\ -PSProvider filesystem
Set-Location PS:
# Set editor shortcut
set-alias np 'C:\Windows\system32\notepad.exe'
# set-alias np 'C:\Program Files\Notepad++\Notepad++.exe'

NOTE: To create a profile for just your cmdline PowerShell use the traditional method of;
new-item -path $profile -type file -force.
To create a profile that will apply to all your PowerShell hosts including the ISE, use;
new-item -path $Profile.CurrentUserAllHosts -type file -force

To see all the locations available to $profile use
$profile | fl * -f

Commenting on this Blog entry is closed.




In the comment about loading

In the comment about loading specific modules, you don't need the loop, do you?

The help on the -name parameter for import-module shows that it takes an array, so you should be able to do something like:
import-module -name $modules


Hmm.... I shall test. I may

Hmm.... I shall test. I may just add it in as an update in the comments on two different methods. In part this is also to show different ways to do things and I had forgotten that one so should probably document it.

I could also do Get-PSSnapin  -Registered | Add-PSSnapin but want the two different ways in there as examples and see who asks questions.