Learn about Windows PowerShell
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.”
“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.
“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?”
“I just signed us up for the panel discussion that’s going to be led by Mark Russinovich at Bouchercon in St. Louis.”
“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:
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).
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:
The following figure illustrates her current Windows PowerShell profile.
“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:
“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.
“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:
When Notepad appeared, she typed the following in the profile file:
The profile now appeared as shown in the following figure.
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:
“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:
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.
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 email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
there is nearly nothing to comment here except that functions, the heart of scripting languages, have to be present in profiles imho!
And the other thing I'd like to mention ( as I'm married too ) is that I know the words "You can go now" ( waving her hands, shaking her head, crossing her fingers ... ) very well after I in fact ( as far as I'm concerned ) just started "my lesson" to answer her question!
Her final sentence is usually: "I just wanted to know THAT ... no need to explain any further ... " :-)))
Wouldn't it have been better to just use Edit, since the profile is "data" that's being modified? Edit's not even used in any of the standard cmdlets. Set just makes no sense to me in this context, is all.
'Edit' is not an approved Powershell verb. To see the full list, visit the TechNet Wiki page here: social.technet.microsoft.com/.../4537.aspx
@Klaus cool. Too funny!
@Jon P. Yes, we could have used Edit. It is just that it is listed as a Data verb, and so we decided to go with Set. The idea is the same as set-content that is used to write text to a file ... which is what we were doing. You are correct that Edit is not used by any of the core cmdlets. It is because it is a data verb, and none of the core cmdlets specifically deal with data.
@Rich Edit is in fact an approved verb. It shows up when using the Get-Verb command inside Windows PowerShell. I always use the Get-Verb command to see what verbs are available, and to see how they are used.
So, If I want to CHANGE something in my profile, can I now create an Edit-Profile function?
(as I'm now EDITING the DATA in my profile??)
I have an issue where in my main .ps1 file I have a call to Get-Credential, I need the result of this cmdlet to be stored in a global variable and be available to any sub script (different .ps1 files). Are profiles the answer?
Inside my profile I have this code:
if($global:credentials -eq $null)
# $global.credentials is not set
$global:credentials = Get-Credential
# $global.credentials is set
I call this in main.ps1 but when I run subscript.ps1 after main.ps1 I am then prompted for credentials. What am I doing wrong?