Summary: Learn how to create a customizable profile for the Windows PowerShell ISE. The Microsoft Scripting Guys walk you through the steps.

 

Microsoft Scripting Guy Ed Wilson here. It is a quiet Sunday morning here in Charlotte, North Carolina. The female humanoid that shares my domicile has departed. She used her last remaining air miles to fly up to Kentucky to visit her family. I still am not flying, and could not have gone with her if I had wanted to—a great excuse, but then not really an excuse if it is true. Before she left, she spent several days telling me both what to do and what not to do while she was gone. She then decided to test my comprehension.

“Now while I am gone, what are you going to do for supper?” she asked with sincerity.

“I have a credit card, and I have the phone number of a good pizza place programmed on speed dial in the phone in my office,” I answered with plausible truthfulness.

“Wrong answer,” she chastised, “I have your meals all laid out with sticky notes on them. The mains are on one shelf in the freezer, and the veggies are on another shelf. Take one of each and pop them in the microwave.”

“Actually, if I order a veggie pizza, that should work,” I said with mock seriousness in my voice.

“No pizza,” she said while raising her voice just a bit. She then added, “Don’t be obtuse.”

I realized she was beginning to stress out about being gone for a while, and I did not want to compound that stress with worries about my health, so I relented.

And now the house is dark, quiet, and empty. My footsteps on the Talavera tile in the kitchen seem to echo endlessly, and even the familiar humming of the four ceiling fans within earshot of my location offer little comfort. I think today will be a good coffee day. I have a fresh bag of Kona coffee I have been anxious to try out anyway. I will take my French press upstairs, and play around with my profile for Windows PowerShell ISE.

There are four profiles that affect the Windows PowerShell ISE, but only two are unique to the ISE itself. Windows PowerShell has a total of six different profiles available to it. The Windows PowerShell profile has been discussed in several previous Hey, Scripting Guy! Blog posts, and you should see those articles for additional information.

The Get-ISEProfiles.ps1 script shown here displays the path to the four Windows PowerShell profiles that affect the Windows PowerShell ISE.

Get-ISEProfiles.ps1

$profile | 
Get-Member -MemberType note* | 
Format-Table -Wrap -AutoSize -Property name, 
 
@{ Label="Path"; Expression={$_.definition -replace "System.String",""} }

When the script runs, the output is displayed that is shown in the following image.

Image of output displayed when script is run

Now, the question is where do I want to apply my modifications? If I create variables, and functions that I might want to use in both the Windows PowerShell ISE and in the Windows PowerShell console, it makes sense to add that item to the current user/all hosts profile. This profile is located in the users documents/WindowsPowerShell folder and is always called profile.ps1. You can use the $profile automatic variable to address this profile, as shown here:

PS C:\Users\ed.NWTRADERS> $profile.CurrentUserAllHosts
C:\Users\ed.NWTRADERS\Documents\WindowsPowerShell\profile.ps1

I will talk about profile coordination in a later Weekend Scripter article. For now, I need to see if I have a profile for my Windows PowerShell ISE. The $profile variable by itself refers to the current user/current host profile. This is shown here:

PS C:\Users\ed.NWTRADERS> $profile
C:\Users\ed.NWTRADERS\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
__________________________________________________________________________
PS C:\Users\ed.NWTRADERS> $profile.CurrentUserCurrentHost
C:\Users\ed.NWTRADERS\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

To see if I have created a profile for the Windows PowerShell ISE, I can use $profile directly with the Test-Path cmdlet as shown here:

PS C:\Users\ed.NWTRADERS> test-path $profile
False

To create an empty Windows PowerShell ISE profile, use the New-Item cmdlet as shown here:

PS C:\Users\ed.NWTRADERS> New-Item -Path $profile -ItemType file -Force

   
Directory: C:\Users\ed.NWTRADERS\Documents\WindowsPowerShell

Mode          LastWriteTime          Length           Name
-------          ------------------          ---------           -------
-a---           9/1/2010 1:29 PM     0                    Microsoft.PowerShellISE_profile.ps1

To delete a profile you no longer want, use the Remove-Item cmdlet. After you have deleted the profile, you may wish to use the Test-Path cmdlet to convince yourself it is really gone. This is because the Remove-Item cmdlet does not return anything when it is used. This is shown here:

PS C:\Users\ed.NWTRADERS> Remove-Item -Path $profile -Force
___________________________________________________
PS C:\Users\ed.NWTRADERS> test-path $profile
False

The creation of a profile can be scripted, as shown here:

Create-Profile.ps1

if( !(Test-Path -Path $profile) )
 
{ New-Item -Path $profile -ItemType file -Force }
else
 
{ "$profile already exists" }

One thing I like to have in a profile is a command to edit the profile. To do this, I created a simple function called Set-Profile and added it to my ISE profile. The function is shown here:

Function Set-Profile

{

 
psedit $profile

} #end function set-profile

After I save the profile and restart the Windows PowerShell ISE, the function is always available. I can type Set-Profile in the command pane of the Windows PowerShell ISE, and the profile will spring into position.

I also decided to add the add-menuitems.ps1 script from yesterday’s Weekend Scripter article to my profile, and to cause it to start automatically. To cause the script to launch automatically, I dot sourced the script as shown here:

. C:\fso\add-MenuItems.ps1

When the ISE starts up, the add-menuitems.ps1 script runs, and the output from the script is displayed in the output pane. If you wish to suppress this behavior, pipe the results of the dot sourced script to the Out-Null cmdlet. This is shown here:

. C:\fso\add-MenuItems.ps1 | out-null
 

Well, I need to get out of here. I am heading to the library.

We would love for you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow, when I will talk about creating a PDF file from a Microsoft Excel worksheet. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys