September 14th, 2011

Scripting Wife Adds a Cool Function to a PowerShell Profile

Doctor Scripto
Scripter

Summary: The Scripting Wife learns how to add a cool function to her Windows PowerShell profile.

 

The story you are about to read is true. We did not bother to change the names to protect the innocent. However, we did alter some of the facts.

 

The hot weather had finally broken, and fluffy white cotton ball clouds crowded the fall sky like Chicago workers queuing up to catch the “L.” I was working the day watch out of the Geek Division, when I received the squawk.

“One Microsoft one, one Microsoft one.”

“One Microsoft one, go ahead.”

“One Microsoft one, see the lady. She is having problems with her Windows PowerShell profile.”

“Roger.”

“No, it’s not Roger, it’s the Scripting Wife.”

“Roger, Scripting Wife.”

Dum dum dum…dum (music plays a la the Dragnet opening theme).

“I’m the Scripting Guy, and I carry a wireless keyboard.”

As seen in the figure below, I peeked around the corner slowly. One never knows when a user will turn hostile, and it is best not to take chances.

Photo of Ed "Bouchercon" Wilson

“There you are, my little script monkey,” squealed the Scripting Wife. “I have been looking for you. I have two things.”

“And?” I coaxed wearily.

“First, Happy Birthday! And guess what?”

“What?”

“I just signed us up for the panel discussion that’s going to be led by Mark Russinovich at Bouchercon in St. Louis.”

“Cool.”

“Just one thing—you need to leave that ratty old Mackintosh here. The Microsoft Scripting Guy should not be parading around in anything called “Mackintosh” anyway!”

“I see what you did there. And what is the second thing?”

“I am wondering about editing my profile, but I cannot remember how we did it yesterday.”

Note   This is part three of a four-part series about the Scripting Wife and the profile. On Monday, she created a profile for the Windows PowerShell console. Yesterday, she created a profile for the Windows PowerShell ISE.

“Well, sweetheart, the easiest way to do that is to create a function and add it to your profile. So here’s looking at you kid,” I said in my best Humphrey Bogart voice.

“Can it Script Monkey. Now get to work. Cut the comedy, and show me what you are talking about. I have work to do, and you are not helping.”

“Ok, first we will add a function to the profile you created for the Windows PowerShell console. Next, we will add a function to the one you created for the Windows PowerShell ISE. So open the Windows PowerShell console, and type notepad $profile,” I instructed.

The Scripting Wife wasted no time in clicking the Windows PowerShell icon she had created on her Quick Launch toolbar, and as soon as the Windows PowerShell console was running, she typed the following command:

Notepad $profile

Because she used tab expansion, the Scripting Wife did not have to completely type the $profile variable name. The keystrokes seen here are exactly what she typed (<space> is the Spacebar, <tab> is the Tab key, and <enter> is the Enter key).

Notepad<space>$pr<tab><enter>

The command and associated output are shown in the following figure.

“Now, there are usually five things that I put in a Windows PowerShell profile. I like to separate the areas with comments. The five things are variable, function, psdrive, alias, and command. So add a comment on new lines in your profile for each of those five things,” I said.

The Scripting Wife thought for about a minute. She looked up, and began to say something. I thought I saw her beginning to mouth the word, “Huh,” but it might have been my imagination. At last, she turned to the Windows PowerShell profile that was open in Notepad, and went to the first line that contained the Import-Module pscx command. She went to the beginning of the line, and pressed Enter. Then she up-arrowed back to the first line. Now she was ready to add the comments. Here is what she typed:

# Variable

# function

# psdrive

# alias

# command

The following figure illustrates her current Windows PowerShell profile.

Image of current profile of Scripting Wife

“Now, close Notepad, close Windows PowerShell, start Windows PowerShell again, and once again open the Windows PowerShell profile in Notepad,” I said.

Here are the steps the Scripting Wife followed:

  1. Saved the Windows PowerShell profile in Notepad by clicking File and then Save from the Notepad menu.
  2. Closed Notepad.
  3. Closed Windows PowerShell.
  4. Started Windows PowerShell again.
  5. Looked for any errors.
  6. Tested to ensure the PSCX module still loaded by using the Get-Module cmdlet.

“Okay, now what?” the Scripting Wife asked.

“Now that we have verified that we have not messed up the profile with adding those comments, it is time to create a custom function to edit your profile. The first thing to do is to come up with a name. The name should follow the two-part naming convention,” I instructed.

“You mean like, CanIt-ScriptMonkey?” she asked.

“Yes, it is a two-part name, but the problem is that CanIt is not an approved verb,” I said.

“And what are you, the verb police?”

“No, I am not the verb police. I am the Scripting Guy, and I carry a wireless keyboard,” I said in my best Sgt. Joe Friday voice. “But if your function ever wants to grow up and live in a module, the Import-Module cmdlet does have a verb police.”

“Okay, then why don’t I call my new function Edit-Profile?” she asked.

“Well, that would technically work. Edit is an approved verb, but it is used as a data verb. You can see this by using the Get-Verb cmdlet,” I said while pointing to the results of the Get-Verb command seen in the following figure.

Image of results of Get-Verb cmdlet

“Why don’t you use the verb Set? It is used with Set-Content, a standard cmdlet that is used to write data to a file. This is essentially what you are doing: writing data to a file,” I suggested.

“Okay. So how do I do it?”

“Go to the # function portion of your profile and use the function keyword to tell Windows PowerShell you are getting ready to create a new function. Next, provide it with the name you are going to use: Set-Profile. After you have done that, let me know.”

The Scripting Wife thought for a few seconds, and then typed Notepad $Profile into the open Windows PowerShell console. Next, she located the # function portion, and then she typed the command, Function Set-Profile, in Notepad. The exact commands she typed are shown here:

Notepad<space>$pr<tab><enter>

When Notepad appeared, she typed the following in the profile file:

Function Set-Profile

The profile now appeared as shown in the following figure.

Image of updated profile

The Scripting Wife looked up as if to ask, “Am I doing it right?” But she did not ask. She did not have to seek a cheap reassurance such as that. I continued with my instruction.

“Now on the line under your new Function Set-Profile line, I need you to open a curly bracket., Two lines below that, I need you to close the curly brackets. After you have done this, let me know.”

This time, it did not take the Scripting Wife any time at all to make the two keystrokes. When she was finished, her code now appeared as it is shown here:

Function Set-Profile

{

 

}

“Very good. Now, inside the opening and the closing curly brackets, add the command you used to open your Windows PowerShell profile in Notepad,” I said.

A few clickety clickety clacks later, and the Scripting Wife had completed her first function. The code for her new function is shown here:

Function Set-Profile

{

 notepad $profile

}

I told her to add a comment after the closing curly bracket so that she could easily see where the function ended. She did not like it, but she did it anyway, with nothing more than a simple, “Can it, Script Monkey,” for my effort. The Scripting Wife’s newly edited Windows PowerShell console profile appears in the following figure.

Image of newly edited Windows PowerShell console profile

The Scripting Wife looked up, waved her hand, and said, “You can go. I need to figure out what other panel discussions I want to attend at Bouchercon. You would be wise to do the same.”

And with that, I was dismissed. Join me tomorrow when the Scripting Wife returns once again, and she continues to monkey around with her Windows PowerShell profile.

 

I invite you to follow me on Twitter and Facebook. You can also follow the Scripting Wife on Twitter. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.