{"id":12701,"date":"2011-09-14T00:01:00","date_gmt":"2011-09-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/09\/14\/scripting-wife-adds-a-cool-function-to-a-powershell-profile\/"},"modified":"2011-09-14T00:01:00","modified_gmt":"2011-09-14T00:01:00","slug":"scripting-wife-adds-a-cool-function-to-a-powershell-profile","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/scripting-wife-adds-a-cool-function-to-a-powershell-profile\/","title":{"rendered":"Scripting Wife Adds a Cool Function to a PowerShell Profile"},"content":{"rendered":"<p><strong>Summary<\/strong>: The Scripting Wife learns how to add a cool function to her Windows PowerShell profile.<\/p>\n<p>&nbsp;<\/p>\n<p><i>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.<\/i><\/p>\n<p>&nbsp;<\/p>\n<p>The hot weather had finally broken, and fluffy white cotton ball clouds crowded the fall sky like Chicago workers queuing up to catch the &ldquo;L.&rdquo; I was working the day watch out of the Geek Division, when I received the squawk.<\/p>\n<p>&ldquo;One Microsoft one, one Microsoft one.&rdquo;<\/p>\n<p>&ldquo;One Microsoft one, go ahead.&rdquo;<\/p>\n<p>&ldquo;One Microsoft one, see the lady. She is having problems with her Windows PowerShell profile.&rdquo;<\/p>\n<p>&ldquo;Roger.&rdquo;<\/p>\n<p>&ldquo;No, it&rsquo;s not Roger, it&rsquo;s the Scripting Wife.&rdquo;<\/p>\n<p>&ldquo;Roger, Scripting Wife.&rdquo;<\/p>\n<p><i>Dum dum dum&hellip;dum (music plays a la the Dragnet opening theme).<\/i><\/p>\n<p>&ldquo;I&rsquo;m the Scripting Guy, and I carry a wireless keyboard.&rdquo;<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6840.hsg-9-14-11-1.jpg\"><img decoding=\"async\" style=\"border: 0px\" title=\"Photo of Ed &quot;Bouchercon&quot; Wilson\" alt=\"Photo of Ed &quot;Bouchercon&quot; Wilson\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6840.hsg-9-14-11-1.jpg\" width=\"300\" \/><\/a><\/p>\n<p>&ldquo;There you are, my little script monkey,&rdquo; squealed the Scripting Wife. &ldquo;I have been looking for you. I have two things.&rdquo;<\/p>\n<p>&ldquo;And?&rdquo; I coaxed wearily.<\/p>\n<p>&ldquo;First, Happy Birthday! And guess what?&rdquo;<\/p>\n<p>&ldquo;What?&rdquo;<\/p>\n<p>&ldquo;I just signed us up for the panel discussion that&rsquo;s going to be led by Mark Russinovich at <a href=\"http:\/\/www.bouchercon2011.com\/\">Bouchercon<\/a> in St. Louis.&rdquo;<\/p>\n<p>&ldquo;Cool.&rdquo;<\/p>\n<p>&ldquo;Just one thing&mdash;you need to leave that ratty old <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mackintosh\">Mackintosh<\/a> here. The Microsoft Scripting Guy should not be parading around in anything called &ldquo;Mackintosh&rdquo; anyway!&rdquo;<\/p>\n<p>&ldquo;I see what you did there. And what is the second thing?&rdquo;<\/p>\n<p>&ldquo;I am wondering about editing my profile, but I cannot remember how we did it yesterday.&rdquo;<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;This is part three of a four-part series about the Scripting Wife and the profile. On <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/09\/12\/the-scripting-wife-uses-a-profile-to-configure-powershell.aspx\">Monday<\/a>, she created a profile for the Windows PowerShell console. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/09\/13\/the-scripting-wife-creates-a-powershell-ise-profile.aspx\">Yesterday<\/a>, she created a profile for the Windows PowerShell ISE.<\/p>\n<p>&ldquo;Well, sweetheart, the easiest way to do that is to create a function and add it to your profile. So here&rsquo;s looking at you kid,&rdquo; I said in my best <a href=\"http:\/\/en.wikipedia.org\/wiki\/Humphrey_Bogart\">Humphrey Bogart<\/a> voice.<\/p>\n<p>&ldquo;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.&rdquo;<\/p>\n<p>&ldquo;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 <b>notepad $profile<\/b>,&rdquo; I instructed.<\/p>\n<p>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:<\/p>\n<p style=\"padding-left: 30px\">Notepad $profile<\/p>\n<p>Because she used tab expansion, the Scripting Wife did not have to completely type the <b>$profile<\/b> variable name. The keystrokes seen here are exactly what she typed (<b>&lt;space&gt;<\/b> is the Spacebar, <b>&lt;tab&gt;<\/b> is the Tab key, and <b>&lt;enter&gt;<\/b> is the <b>Enter<\/b> key).<\/p>\n<p style=\"padding-left: 30px\">Notepad&lt;space&gt;$pr&lt;tab&gt;&lt;enter&gt;<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<\/p>\n<p>&ldquo;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,&rdquo; I said.<\/p>\n<p>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, &ldquo;Huh,&rdquo; 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 <b>Import-Module pscx<\/b> 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:<\/p>\n<p style=\"padding-left: 30px\"># Variable<\/p>\n<p style=\"padding-left: 30px\"># function<\/p>\n<p style=\"padding-left: 30px\"># psdrive<\/p>\n<p style=\"padding-left: 30px\"># alias<\/p>\n<p style=\"padding-left: 30px\"># command<\/p>\n<p>The following figure illustrates her current Windows PowerShell profile.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0702.hsg-9-14-11-3.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of current profile of Scripting Wife\" alt=\"Image of current profile of Scripting Wife\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0702.hsg-9-14-11-3.png\" \/><\/a><\/p>\n<p>&ldquo;Now, close Notepad, close Windows PowerShell, start Windows PowerShell again, and once again open the Windows PowerShell profile in Notepad,&rdquo; I said.<\/p>\n<p>Here are the steps the Scripting Wife followed:<\/p>\n<ol>\n<li>Saved the Windows PowerShell profile in Notepad by clicking <b>File<\/b> and then <b>Save<\/b> from the Notepad menu.<\/li>\n<li>Closed Notepad.<\/li>\n<li>Closed Windows PowerShell.<\/li>\n<li>Started Windows PowerShell again.<\/li>\n<li>Looked for any errors.<\/li>\n<li>Tested to ensure the PSCX module still loaded by using the <b>Get-Module<\/b> cmdlet.<\/li>\n<\/ol>\n<p>&ldquo;Okay, now what?&rdquo; the Scripting Wife asked.<\/p>\n<p>&ldquo;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,&rdquo; I instructed.<\/p>\n<p>&ldquo;You mean like, CanIt-ScriptMonkey?&rdquo; she asked.<\/p>\n<p>&ldquo;Yes, it is a two-part name, but the problem is that <b>CanIt<\/b> is not an approved verb,&rdquo; I said.<\/p>\n<p>&ldquo;And what are you, the verb police?&rdquo;<\/p>\n<p>&ldquo;No, I am not the verb police. I am the Scripting Guy, and I carry a wireless keyboard,&rdquo; I said in my best <a href=\"http:\/\/en.wikipedia.org\/wiki\/Dragnet_(series)\">Sgt. Joe Friday<\/a> voice. &ldquo;But if your function ever wants to grow up and live in a module, the <b>Import-Module<\/b> cmdlet does have a verb police.&rdquo;<\/p>\n<p>&ldquo;Okay, then why don&rsquo;t I call my new function <b>Edit-Profile<\/b>?&rdquo; she asked.<\/p>\n<p>&ldquo;Well, that would technically work. <b>Edit<\/b> is an approved verb, but it is used as a data verb. You can see this by using the <b>Get-Verb<\/b> cmdlet,&rdquo; I said while pointing to the results of the <b>Get-Verb<\/b> command seen in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4456.hsg-9-14-11-4.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of results of Get-Verb cmdlet\" alt=\"Image of results of Get-Verb cmdlet\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4456.hsg-9-14-11-4.png\" \/><\/a><\/p>\n<p>&ldquo;Why don&rsquo;t you use the verb <b>Set<\/b><i>? <\/i>It is used with <b>Set-Content<\/b>, a standard cmdlet that is used to write data to a file. This is essentially what you are doing: writing data to a file,&rdquo; I suggested.<\/p>\n<p>&ldquo;Okay. So how do I do it?&rdquo;<\/p>\n<p>&ldquo;Go to the <b># function<\/b> portion of your profile and use the <b>function<\/b><i> <\/i>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: <b>Set-Profile<\/b><i>.<\/i> After you have done that, let me know.&rdquo;<\/p>\n<p>The Scripting Wife thought for a few seconds, and then typed <b>Notepad $Profile<\/b> into the open Windows PowerShell console. Next, she located the <b># function<\/b> portion, and then she typed the command, <b>Function Set-Profile<\/b>, in Notepad. The exact commands she typed are shown here:<\/p>\n<p style=\"padding-left: 30px\">Notepad&lt;space&gt;$pr&lt;tab&gt;&lt;enter&gt;<\/p>\n<p>When Notepad appeared, she typed the following in the profile file:<\/p>\n<p style=\"padding-left: 30px\">Function Set-Profile<\/p>\n<p>The profile now appeared as shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6201.hsg-9-14-11-5.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of updated profile\" alt=\"Image of updated profile\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6201.hsg-9-14-11-5.png\" \/><\/a><\/p>\n<p>The Scripting Wife looked up as if to ask, &ldquo;Am I doing it right?&rdquo; But she did not ask. She did not have to seek a cheap reassurance such as that. I continued with my instruction.<\/p>\n<p>&ldquo;Now on the line under your new <b>Function Set-Profile<\/b> 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.&rdquo;<\/p>\n<p>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:<\/p>\n<p style=\"padding-left: 30px\">Function Set-Profile<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>&ldquo;Very good. Now, inside the opening and the closing curly brackets, add the command you used to open your Windows PowerShell profile in Notepad,&rdquo; I said.<\/p>\n<p>A few clickety clickety clacks later, and the Scripting Wife had completed her first function. The code for her new function is shown here:<\/p>\n<p style=\"padding-left: 30px\">Function Set-Profile<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;notepad $profile<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>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, &ldquo;Can it, Script Monkey,&rdquo; for my effort. The Scripting Wife&rsquo;s newly edited Windows PowerShell console profile appears in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2477.hsg-9-14-11-6.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of newly edited Windows PowerShell console profile\" alt=\"Image of newly edited Windows PowerShell console profile\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2477.hsg-9-14-11-6.png\" \/><\/a><\/p>\n<p>The Scripting Wife looked up, waved her hand, and said, &ldquo;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.&rdquo;<\/p>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. You can also follow <a href=\"mailto:the%20Scripting%20Wife%20on%20Twitter\">the Scripting Wife on Twitter<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The Scripting Wife learns how to add a cool function to her Windows PowerShell profile. &nbsp; 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. &nbsp; The hot weather had finally broken, and fluffy [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,144,3,4,74,45],"class_list":["post-12701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-scripting-wife","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: The Scripting Wife learns how to add a cool function to her Windows PowerShell profile. &nbsp; 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. &nbsp; The hot weather had finally broken, and fluffy [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12701"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12701\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}