{"id":4890,"date":"2012-10-01T00:01:00","date_gmt":"2012-10-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/01\/build-your-own-powershell-cmdlet-part-3-of-9\/"},"modified":"2012-10-01T00:01:00","modified_gmt":"2012-10-01T00:01:00","slug":"build-your-own-powershell-cmdlet-part-3-of-9","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-your-own-powershell-cmdlet-part-3-of-9\/","title":{"rendered":"Build Your Own PowerShell Cmdlet: Part 3 of 9"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Windows PowerShell MVP, Sean Kearney, continues a series of guest blogs that detail how to build your own cmdlet.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about Sean, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/sean+kearney\/\" target=\"_blank\">his previous guest blog posts<\/a>.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> This is Part 3 of a nine-part series about building your own Windows PowerShell cmdlet. Read the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/guest+blogger\/sean+kearney\/build+your+own+cmdlet\/\">entire series<\/a> as it unfolds.<\/p>\n<p>Here&rsquo;s Sean&hellip;<\/p>\n<p>We need to do a few minor things. First, we preface the entire script block from beginning to end as a function. But there are rules for becoming a real cmdlet. Just ask that Pinocchio character. The biggest rule is formatting the name. It must follow a Verb-Noun format. The noun should be singular. The third and most important rule that you shalt follow (less the Lords of Monad come crashing through your door), is to use one of the approved verbs in Windows PowerShell.<\/p>\n<h2>Approved verbs?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/h2>\n<p>Yes, approved verbs. Remember one of the biggest strengths in Windows PowerShell is the consistency of how various Windows PowerShell systems work. There is actually an approved list of verbs for the Verb-Noun pairing.<\/p>\n<p>Would it surprise you to find that the list is built-in to Windows PowerShell? Just run the cmdlet <b>Get-Verb<\/b>. This will give you an output similar to the one shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8816.hsg-10-1-12-1.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8816.hsg-10-1-12-1.png\" \/><\/a><\/p>\n<p>How do you choose an appropriate verb? Think of what you&rsquo;re doing and apply a little common sense. Verbs are broken up into individual groups like Common, Data, Lifecycle, Diagnostic, Communications, Security, Other, and SwissCheese.<\/p>\n<p>OK, fair enough. There is no SwissCheese group. I made that one up to see if you were reading. But when you want to write a cmdlet, think about what you are trying to do, and find a verb that seems to make sense.<\/p>\n<p>In our case, we are adding a log file to our file system. Let&rsquo;s see if there is a verb called <b>Add<\/b>.<\/p>\n<p style=\"padding-left: 30px\">Get-Verb Add<\/p>\n<p>Running this cmdlet confirms that <b>Add<\/b> is a valid verb in Windows PowerShell, and it is a member of the Common group. This suggests that it&rsquo;s pretty flexible for usage.<\/p>\n<p>So remember the rule: it has to follow a verb-noun combination for it to be a real cmdlet. Here&rsquo;s the tricky part. &ldquo;Noun&rdquo; is open to interpretation. Although most of the verbs in Windows PowerShell will be found in the standard Webster&rsquo;s Dictionary at your local library, odds are that <b>ADObject<\/b> or <b>QADUser<\/b> won&rsquo;t be.<\/p>\n<p>So think of a Windows PowerShell noun as something more of a thing that you&rsquo;re referencing. The name of the thing may only be unique to your system. In our case, we are adding a log file, so we can use the name pairing of <b>Add-LogFile<\/b>.<\/p>\n<p>So let&rsquo;s convert our present script to an Advanced function called &ldquo;ADD-LOGFILE.&rdquo;<\/p>\n<p style=\"padding-left: 30px\">function global:ADD-LOGFILE{<\/p>\n<p style=\"padding-left: 30px\">PARAM(<\/p>\n<p style=\"padding-left: 30px\">[STRING]$Folder=&#8221;C:\\PowerShell&#8221;,<\/p>\n<p style=\"padding-left: 30px\">[STRING]$Preface=&#8221;Logfile&#8221;,<\/p>\n<p style=\"padding-left: 30px\">[STRING]$Extension=&#8221;.log&#8221;<\/p>\n<p style=\"padding-left: 30px\">)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># GET the Current Date for our Logfile<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Today=GET-DATE<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Extract the Date removing the &ldquo;\/&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Date=$Today.toshortdatestring().Replace(&ldquo;\/&rdquo;,&rdquo;&rdquo;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Extract the Time removing the &ldquo;:&rdquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Time=$Today.tostring(&ldquo;HH:mm:ss&rdquo;).Replace(&ldquo;:&rdquo;,&rdquo;&ldquo;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Build our Filename<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$Logfilename=$Folder+&#8221;\\&#8221;+$Preface+&rdquo;-&ldquo;+$Date+&rdquo;-&ldquo;+$Time+$Extension<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Test and ensure file does not already exist<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">IF (TEST-PATH -path $Logfilename)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">{ WRITE-ERROR &ndash;message &ldquo;Error: $Logfilename exists.&rdquo; &ndash;category &lsquo;WriteError&rsquo;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># If file exists, return a status of Boolean $False for Unsuccessful<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">RETURN $Logfilename,$FALSE }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">ELSE<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Create logfile<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">NEW-ITEM &ndash;Type File -path $Logfilename -Force | OUT-NULL<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># Return the Full path and filename if successful<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">RETURN $Logfilename,$TRUE<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>Let&rsquo;s save this as a new Windows PowerShell script and call it C:\\PowerShell\\addlogfunction.ps1.<\/p>\n<p>When we execute this script, it will appear to do nothing.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6403.hsg-10-1-12-2.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6403.hsg-10-1-12-2.png\" \/><\/a><\/p>\n<p>Before you start to go for that sad trombone link, your script DID work.<\/p>\n<p>What you just did was define a global function named <b>Add-LogFile<\/b>. You then loaded it into the global scope within Windows PowerShell. This means that in your current session, the function has been defined and placed outside of the confines of the script, and it will be accessible by your current Windows PowerShell session after the script terminates.<\/p>\n<p>To test your function, just key in:<\/p>\n<p style=\"padding-left: 30px\">ADD-LOGFILE<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7853.hsg-10-1-12-3.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7853.hsg-10-1-12-3.png\" \/><\/a><\/p>\n<p>You can even run <strong>Get-Help<\/strong> on it&mdash;although the Help will be quite sparse at the moment.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3225.hsg-10-1-12-4.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3225.hsg-10-1-12-4.png\" \/><\/a><\/p>\n<p>~Sean<\/p>\n<p>Thanks Sean! This really good stuff. Keep it coming. &nbsp;It is great to have an honorary scripting guy in the house!<\/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\" target=\"_blank\">Facebook<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, continues a series of guest blogs that detail how to build your own cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about Sean, see his previous guest blog posts. Note This [&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":[372,56,3,154,45],"class_list":["post-4890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-build-your-own-cmdlet","tag-guest-blogger","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, continues a series of guest blogs that detail how to build your own cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about Sean, see his previous guest blog posts. Note This [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4890","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=4890"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4890\/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=4890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}