{"id":4895,"date":"2012-09-30T00:01:00","date_gmt":"2012-09-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/09\/30\/weekend-scripter-build-your-own-powershell-cmdlet-part-2-of-9\/"},"modified":"2012-09-30T00:01:00","modified_gmt":"2012-09-30T00:01:00","slug":"weekend-scripter-build-your-own-powershell-cmdlet-part-2-of-9","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-build-your-own-powershell-cmdlet-part-2-of-9\/","title":{"rendered":"Weekend Scripter: Build Your Own PowerShell Cmdlet: Part 2 of 9"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Windows PowerShell MVP, Sean Kearney, continues his series of guest blogs that detail how to build your own cmdlet.<\/p>\n<p align=\"left\">Microsoft Scripting Guy, Ed Wilson, is here. We have a special week in store for you. 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><b>Note<\/b> This is Part 2 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>To improve on this script, instead of providing static values, we&rsquo;ll switch them to parameters with defaulting values. In this way, the script will run as we want it to, but it will allow for more flexibility. &nbsp;<\/p>\n<p>This is done by moving the values we want to pass along in a param block. The three values I want to be adjustable are the LogFileName, the Folder, and the Extension. We can assign them default values, however. The beginning of script will now look like this:<\/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<h2>Test the new parameters<\/h2>\n<p>Now we will save this script as newlogfile-param.ps1 in our C:\\PowerShell folder. This new script will behave as before if nothing is passed along to it. But we can now do this to generate the log file with a .txt extension if we like:<\/p>\n<p style=\"padding-left: 30px\">C:\\PowerShell\\newlogfilename-param.ps1 &ndash;Extension .txt<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7002.wes-9-30-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\/7002.wes-9-30-12-1.png\" \/><\/a><\/p>\n<p>Or, now you can create the log file in a totally unique folder and a different preface. Yes. I am having a bit of fun here. We&rsquo;re going to indicate all of our logs that start with &ldquo;BAD&rdquo; and store them in a folder called <b>BadThings<\/b>. Well, where would you put bad things?<\/p>\n<p style=\"padding-left: 30px\">C:\\PowerShell\\newlogfilename-param.ps1 &ndash;Preface Bad &ndash;Folder C:\\BadThings<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1205.2.JPG\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1205.2.JPG\" \/><\/a><\/p>\n<p>We now have a simple and adaptable script that can accept parameters. So how does this become a cmdlet?<\/p>\n<p>~Sean<\/p>\n<p>Join us tomorrow when Sean answers this question and more in Part 3 of Building Your Own PowerShell Cmdlet. Thanks Sean, this is shaping up to be really cool.<\/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 his series of guest blogs that detail how to build your own cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. We have a special week in store for you. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about [&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,61,45],"class_list":["post-4895","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-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, continues his series of guest blogs that detail how to build your own cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. We have a special week in store for you. Guest blogger and Windows PowerShell MVP, Sean Kearney, has written a series about building cmdlets. For more about [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4895","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=4895"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4895\/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=4895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}