The Scripting Wife Uses a Profile to Configure PowerShell

Summary: The Scripting Wife learns how to use a profile to configure the Windows PowerShell console.

 

Microsoft Scripting Guy Ed Wilson here. One of the coolest events of the year occurs in just a few days. Yes, that’s right, it’s Bouchercon in St. Louis. Bouchercon is a mystery writer’s conference, and the attendee list is a who’s-who among the cloak-and-dagger writer crowd. Conferees to Bouchercon also have the privilege of selecting the Anthony awards for the best mysteries.

It was a dark and stormy night in Charlotte, a town that knows how to keep its secrets, when a sudden disturbance in the other room cut through my concentration on my latest Windows PowerShell script like a dagger slicing through heavy drapery. As I moved stealthily through the foyer, across the living room, and into the kitchen, my eyes rapidly picked up the source of the cacophony of frustration-laden words that tumbled flat on the tile floor like yesterdays’ newspaper. I peeked around the corner.

Photo of Ed "Bouchercon" Wilson peeking around corner

“So, what’s ya beef, sweetheart?”

The Scripting Wife turned her gaze at me from her computer monitor and at that moment, I felt it. I felt it deep in the core of my being like a seven-inch stiletto plunged deep into my inner core, a spark of electricity that said she was the one. I looked down, and she was poking me in my stomach.

“I am not beefing. I have never beefed in my entire life. What is wrong with you anyway? What’s with the trench coat and the fedora?”

“A man on the job has to be prepared for all conditions, all emergencies, all kinds of stormy weather.”

“But we are inside. Besides, your Bogart voice is barely believable.”

“Don’t let the patter fool you, kid. Maybe I am not as tough as I am cracked up to be. Maybe that kind of a reputation is good for business. Inside, I might be a teddy bear, but don’t count on it. Besides, you knew it was Bogey.”

“Only because you always do Bogey. If you can get serious for a minute, I have a problem. Why do I always have to keep loading the PowerShell Community Extension Project every day?”

“Because you use it every day? That’s just a guess, but then my guesses are usually pretty good.”

“Of course, I use it every day, and that is why I need to load it.”

“Whadda I tell ya?”

“No. I mean, you never seem to load it, but I know you use the cmdlets. How is that possible?”

“Oh, I see what you mean. What you need to do is to create a profile. In your profile, you can load modules you find useful, and that you want to have immediately available,” I explained.

“Okay. So how do I get a profile,” she asked.

“Well the first thing to do, is to ensure that you do not already have a profile. Open your Windows PowerShell console, and use the Test-Path cmdlet to see if you have a profile. Use the $profile automatic variable to refer to your profile,” I instructed.

The Scripting Wife thought for a second, and typed the following command into the dark blue Windows PowerShell console:

Test-Path $PROFILE

As the Scripting Wife was typing, she took advantage of the Windows PowerShell tab expansion feature. Therefore, she did not have to type the complete command. What she actually typed was:

Test-p<tab><space>$p<tab><tab><enter>

In this case, <tab> represents the Tab key on the keyboard; <space> represents the spacebar; <enter> represents the Enter key on the keyboard.

On the Scripting Wife’s computer, Test-Path $profile returns False, which means that she does not have a profile. If your command returns True, you can open it in Notepad by using the following command:

Notepad $profile

To back up your profile, you can use this command (replace destination folder with a folder on your local computer):

Copy-item $profile destination folder

“Okay, I am assuming that I do not have a profile, because the Test-Path command came back and said False. So back to my original question: how do I get a profile?” she asked.

“It is simple. You use the New-Item cmdlet to tell it that you want to create an itemtype of file, and give it the $profile variable. Just to be on the safe side, go ahead and use the force switch.”

“You mean you are going to forgo your corny Star Wars ‘Use the force, Luke’ imitation?” she asked with a slightly disappointed voice.

“Yeah, you never seem to laugh at that one.”

“I never seem to laugh at any of them, but it never stopped you before,” she said.

The Scripting Wife said “New-Item $profile dash itemtype file dash force” to herself as she typed the following command:

New-Item $PROFILE -ItemType file –Force

The keys she actually typed are shown here:

New-i<tab><space>$pr<tab> <space>-i<tab><space>file<space>-f<tab><enter>

The reason the Scripting Wife used $pr<tab> was so that she did not have to tab past the $pid variable, which she had to do when she first used the Test-Path cmdlet. The commands and associated output are shown in the following figure.

Image of commands and associated output

“Obviously, it did something, but how is that going to automatically load the PowerShell Community Extensions for me?” she asked.

“Patience, grasshopper,” I said in my best Kung Fu voice.

“I think I prefer your Bogey to your Kung Fu voice. After all, I would rather be a babe than a grasshopper.”

“That reminds me of a Simon and Garfunkel song.”

“Oh, no!” she cried in mock horror. “Just get on with it.”

“Okay, now you have two choices: you can open your profile in Notepad, or you can open it in the Windows PowerShell ISE,” I explained.

“Let’s use Notepad. It is faster,” she said.

“All right. Type Notepad $profile,” I said.

The Scripting Wife did not hesitate as she typed the following command:

Notepad $profile

What she actually typed is shown here:

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

After she pressed Enter, a blank Notepad appeared, as shown in the following figure.

Image of blank Notepad

“Nothing happened,” she complained.

“Are you kidding? You opened Notepad, and not only that, look at the title bar. See how it points to the same location you saw earlier when you used the New-Item cmdlet to create your blank profile?”I asked.

“Okay,” she replied with a somewhat hesitant voice.

“Now, we will add the command to load the PSCX. After the command is added to your profile, it will happen each time you start the Windows PowerShell console. You ready?” I asked.

“You bet. Let’s do it.”

“Okay, in your open Notepad, add the Import-Module pscx command on the first line, and then save and close Notepad.”

She quickly typed the following command:

Import-Module pscx

“I wish that tab expansion worked inside Notepad,” she complained.

“What I do is type the command in the Windows PowerShell console first. Then when I know the command works, I highlight it and copy it, so I can paste it in Notepad. Now, close Windows PowerShell, and open it back up.”

“Ok. So how do I know that it worked?”

“Use the Get-Command cmdlet, and look for commands from the PSCX module,” I said.

The Scripting Wife used the GCM alias instead of typing Get-Command. Here is the command she typed:

gcm -Module pscx

The actual keystrokes she used are shown here:

gcm<space>-m<tab><enter>

The output from that command is shown in the following figure.

Image of output from command

“Wow, that is cool. Thanks,” she said.

“You’re welcome, sweetheart,” I replied in my Bogey voice.

She sprung from her chair and left the keyboard as if it were a thing on fire. Her perfume reminded the now-empty room that a vibrant being had once occupied the premises. The room seemed smaller now that she was gone.

 

The Scripting Wife will return tomorrow when she struggles to configure the Windows PowerShell ISE. 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