Ezine 156 – PowerShell’s Profile.ps1 and Add-PSSnapin
If you wish to control the settings when PowerShell’s launches, then add instructions to a special file called profile.ps1. One key command to include in this text file is add-PSSnapin. What this does is load extra cmdlets into the PowerShell environment.
PowerShell Topics for Profile.ps1 and Add-PSSnapin
Once you achieve mastery in a particular area, any difficulties that you endured during the learning process seem to fade into the background. So it is with understanding Profile.ps1. Configuring this file is ridiculously easy once you have mastered it, but impenetrably difficult when you try and figure it out for the first time.
The secret of setting-up profile.ps1 is attention to spelling. In particular, the filename ‘profile’ must be singular, and the directory name must be precisely: ‘WindowsPowerShell’.
As we will see, the killer reason for mastering profile.ps1 is so that you can load PSSnapins. As a result of this procedure PowerShell will load extra cmdlets effortlessly every time it starts.
My mission this week is to introduce you to the file profile.ps1 and the command add-PSSnapin .
Profile.ps1 is rather like the old autoexec.bat file in that it runs a series of commands at start time. A more modern analogy would be the way Windows loads the contents of the Startup folder. Actually, there is connection in that each User’s profile has its own Startup folder.
PSSnapin translates to PowerShell Snap-in. The benefit of this command is that you can add extra third-party cmdlets. For example, active directory commands supplied by QAD (Quest Active Directory).
At this point, perhaps you are reading my mind, and thinking why don’t we incorporate the add-PSSnapin command into the profile.ps1 file? Perfect, that will be this week’s mission.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Example 1 – Pure Profile.ps1
Let me give you a few pointers with the text file profile.ps1. The file must be called precisely profile.ps1. The name profileS.psL would be doubly wrong because of the plural filename and ‘L’ instead of a 1 in the extension.
You must save profile.ps1 into a specific directory. Begin your drill-down by navigating to the C:\Documents and Settings folder. Next you need to know the Windows profile name for the user who is logged on, in my case \guyt. The rest of the path is My Documents\WindowsPowerShell. Pay close attention to the folder name: WindowsPowerShell. Here is my full path.
C:\Documents and Settings\guyt\My Documents\WindowsPowerShell
It is in the above WindowsPowerShell folder that I save profile.ps1.
There is a whole micro-industry in creating clever instructions for profile.ps1, (just as there was tweaking the old DOS autoexec.bat). For our mission we will focus on just two instructions, setting the HOME directory and loading snap-ins.
# Simple instruction for Profile.ps1 (Feel free to amend c:\)
I always like to build on success, thus I include the simple command, ‘set-Location c:\’ merely to show that PowerShell is in fact reading profile.ps1. If this works and PowerShell opens at the c:\ prompt, then you could try a more realistic ‘home’ directory for your PowerShell, for example, set-Location c:\PowerShell (Providing you have a folder called precisely, PowerShell).
The only slight problem with this challenge is that we need two pre-requisites over-and-above installing PowerShell. Firstly, download the third party snap-in from the Quest site. Without a snap-in the command, add-PSSnapin makes no sense. Your plan B would be to substitute one of the growing number of third-party snap-ins.
Secondly, to get any action from this active directory snap-in you need to logon as administrator of a domain. I downloaded and installed the snap-in, then I ran the following command on a Domain Controller. I say again, if you don’t have a domain, research the internet for alternative snap-ins.
# Instruction for Profile.ps1 to add a Snap-in
Note 1: There are two ways to test that you have really added this QAD snap-in to the PowerShell interface type:
get-PSSnapin |ft name
Note 2: If these commands don’t reveal the quest snap-ins, you probably missed one of my pre-requisites. As I mentioned earlier, you must download and install the quest.activeroles.admanagement snap-in from Quest’s site, and you need to have an active directory domain.
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
Example 2a – Check your snap-ins
If you substitute the verb ‘get’ for ‘set’, then PSSnapin will display all snap-ins including the Microsoft default sets.
# Optional instruction to display all Snap-ins
get-PSSnapin |ft name
Note 1: Did you get just the 5 Microsoft snap-ins? Or did you get a 6th snap-in from quest?
Summary of PowerShell’s Profile.ps1 and add-PSSnapin
If you need to employ PowerShell to manipulate active directory, then seek additional cmdlets. One of the best sources is Quest (or PowerGUI). Additionally, when you need to control PowerShell’s start-up environment, then take the time to create and configure profile.ps1.
Once you have installed the snap-ins, you need a command to load them each time PowerShell launches, that command is : add-PSSnapin. Why not get the best of both worlds? Master profile.ps1 and make sure that the quest.activeroles.admanagement snap-in is always available.
If you like this page then please share it with your friends
See more Microsoft PowerShell tutorials
If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links.