Ezine 156 - PowerShell's Profile.ps1 and Add-PSSnapinEzine 156 - PowerShell's Profile.ps1 and Add-PSSnapinIf 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
This Week's SecretOnce 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. This Week's MissionMy 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. ˆ Example 1 - Pure Profile.ps1Let 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. 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). Example 2 - Add-PSSnapinPre-requisites 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: 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. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever Example 2a - Check your snap-insIf 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 Expected result: 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-PSSnapinIf 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. See more about PowerShell cmdlets *
|
|||||