Weekend Scripter: Clean Up Your PowerShell ISE Profile by Using a Module

ScriptingGuy1

 

Summary: Microsoft Scripting Guy Ed Wilson shows how to clean up your Windows PowerShell ISE profile by creating a module.

 

Microsoft Scripting Guy Ed Wilson here, it is getting cold here in the Deep South. It is nearly 50 degrees Fahrenheit (10 degrees Celsius). The smell of hot chocolate permeates the early morning atmosphere in our kitchen-definite signs of the Scripting Wife’s presence. I glance over at the kitchen computer, and see she is hanging out on Twitter with some of her new friends from SQL Saturday #49 in Orlando, Florida. Unbeknownst to me, she signed us up to go to SQL Saturday #62 in Tampa, Florida on January 15, 2011.

SQL Saturday #49 was awesome. I finally got to meet many of my tweeps, including Microsoft MVP Maximo Trinidad, seen in the following image.

 

One of the things that was especially great about SQL Saturday #49 was that they had an entire track of Windows PowerShell sessions – that’s right, you could attend the daylong event, spend it in a single room, and listen to most excellent presentations by Microsoft MVP’s, MVP wannabes, and yours truly “The Microsoft Scripting Guy.” The Windows PowerShell track presenters are seen in the next image, and are from left to right: Ron Dameron, Aaron Nelson, Maximo Trinidad, Chris Miller and Ed Wilson (that’s me).

 

As seen in the following image, it was a beautiful day outside and yet the event was completely sold out; some Windows PowerShell sessions were standing room only!

 

Anyway, we are back home, unpacked, and it is time to get back into a routine. One thing that I have been meaning to do is to change the way my Windows PowerShell ISE profile is stored. As seen in the following image it is very cluttered-and some files are stored in my c:\fso folder which means that if they are ever moved, the profile will not work correctly. This is the same profile that was developed in my series of posts about how to customize the Windows PowerShell ISE.

 

Before I mess around too much with my Windows PowerShell ISE profile, I want to make sure that I have a backup copy of it. Luckily, I have a function called backup-Profile that is stored in the Windows PowerShell ISE profile. Therefore, all I have to type is backup-profile into the Windows PowerShell ISE Command Prompt window and press Enter. After you do this, a backup of the profile is created and stored in the backup location. This step is really not necessary because my Windows PowerShell ISE profile causes a backup of the profile to be created every time that the Windows PowerShell ISE is launched – which in my case is several dozen times a day, but still, one never knows. I am just paranoid I guess, because I lost my Windows PowerShell profile one time, a long time ago, when I upgraded the hard disk on my laptop. Ever since, I have included a backup-profile function in my Windows PowerShell profile that runs every time Windows PowerShell starts.  Here is the function, just in case you do not have a copy of it.

Function backUp-Profile()

{

 $dte = get-date

 $buName = $dte.tostring() -replace “[\s:{/}]”,”_”

 $buName = $buName + “_Profile”

 copy-item -path $profile -destination “C:\trans\backup\ISE$buName.ps1”

} #end backup-Profile

 

After the profile is backed up, I open a new script pane in the Windows PowerShell ISE and copy everything from my profile into the module and then I save it as a .psm1 file. To install the module, it has to go into the appropriate modules folder in a folder that is the same as the name of the module itself. For me that location is seen here:

C:\Users\ed.NWTRADERS\Documents\WindowsPowerShell\Modules\ISEProfileModule

This process is discussed in the first of my series of Weekend Scripter posts on modules. Working with module paths can be a bit annoying, and I wrote a Copy-Modules.ps1 script to help me in installing modules. The article where I talk about this was “Hey, Scripting Guy! How do I Work with Windows PowerShell module paths?”

After I had my ISEProfileModule folder in my modules directory, I decided to move the Add-MenuItems.ps1 script to that location also. In my ISEProfileModule I created a variable called $moduleHome that refers to my module location. I now use that variable in my profile. The Add-MenuItems script was discussed in my series of posts about how to change the Windows PowerShell ISE.

Now my Windows PowerShell ISE profile is very short and clean. It is seen here.

# *** Entry Point to Profile ***

Import-Module ISEProfileModule

backUp-Profile

. $moduleHome\iseProfileModule\add-MenuItems.ps1 | out-null

 

The Windows PowerShell ISE module is uploaded to the Scripting Guys Script Repository.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow when I will talk about adding a couple of pretty cool functions to my Windows PowerShell ISE. I will store the scripts in the ISEProfileModule directory, and dot source them into my Windows PowerShell ISE profile. It will be cool. See you tomorrow.

 

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon