{"id":4954,"date":"2012-09-19T00:01:00","date_gmt":"2012-09-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/09\/19\/use-powershell-to-create-intelligent-default-values\/"},"modified":"2012-09-19T00:01:00","modified_gmt":"2012-09-19T00:01:00","slug":"use-powershell-to-create-intelligent-default-values","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-intelligent-default-values\/","title":{"rendered":"Use PowerShell to Create Intelligent Default Values"},"content":{"rendered":"<p><b>Summary<\/b>: Windows PowerShell&nbsp;3.0 introduces the PSDefaultParameterValues automatic variable, which permits creating custom default values.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Life is returning to normal&mdash;at least for this week. Next week, the Scripting Wife and I head north where I will be speaking at the <a href=\"http:\/\/powershellgroup.org\/central.oh\" target=\"_blank\">Central Ohio PowerShell User Group<\/a> (COPUG) on October 2, 2012. There are only a few tickets left for this free event, so if you are in the area and you want to catch up with us while we are there, you will need to register soon.<\/p>\n<p>For today, I am sitting at my desk, checking email, and sipping a nice cup of Gunpowder Green Tea with a bit of lemon grass and a cinnamon stick for sweetness. Outside, there has been a cool steady drizzle of rain, and the wind has been whistling through the trees causing the leaves to rustle like crinolines on prom night.<\/p>\n<h2>Finding custom Windows PowerShell default values<\/h2>\n<p>In Windows PowerShell&nbsp;3.0, a new automatic variable exists called <b>PSDefaultParameterValues<\/b>. This variable is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Variable PSDefaultParameterValues<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">PSDefaultParameterValues&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {}<\/p>\n<p>As you can see, the value of the <b>PSDefaultParameterValues <\/b>automatic variable accepts an object. This information also appears in <b>Get-Member<\/b>.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Variable PSDefaultParameterValues | Get-Member &nbsp;-Name value<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: System.Management.Automation.PSVariable<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Value Property&nbsp;&nbsp; System.Object Value {get;set;}<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;Remember that when setting a value or retrieving a value for a variable, the dollar sign ($) identifies the variable. But when using cmdlets such as <b>Get-Variable<\/b>, <b>Set-Variable<\/b>, or <b>New-Variable<\/b>, the dollar sign does not come into play. This is because the dollar sign is not actually part of the variable name.<\/p>\n<h2>Setting custom Windows PowerShell default values<\/h2>\n<p>So what good is the <b>PSDefaultParameterValues<\/b> automatic variable? Suppose I have a cmdlet that I use on a regular basis, and every time I use the cmdlet, I am typing the same parameter values. A useful example of this might be if I use the <b>Send-MailMessage<\/b> cmdlet to send mail notifications from scripts&mdash;I would typically use the same SMTP server for each calling of the cmdlet. In fact, this scenario appears in <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847819\" target=\"_blank\">about_Parameters_Default_Values<\/a> in the Microsoft TechNet Library.<\/p>\n<p>To set a default value for a cmdlet, I essentially use a hash table.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;If you have not noticed, hash tables are really important. To move beyond basic Windows PowerShell techniques, you need to know how to create a hash table. See <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=hash%20table&amp;sections=7618&amp;PageIndex=1\" target=\"_blank\">this collection of Hey, Scripting Guy! Blogs<\/a> for more information about creating and using hash tables.<\/p>\n<p>The syntax to assign a new default value is shown here.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"30\" valign=\"top\">\n<p>&nbsp; @<\/p>\n<\/td>\n<td width=\"25\" valign=\"top\">\n<p>&nbsp; {<\/p>\n<\/td>\n<td width=\"102\" valign=\"top\">\n<p>Cmdlet name<\/p>\n<\/td>\n<td width=\"18\" valign=\"top\">\n<p>&nbsp; :<\/p>\n<\/td>\n<td width=\"224\" valign=\"top\">\n<p>&nbsp; Parameter Name<\/p>\n<\/td>\n<td width=\"23\" valign=\"top\">\n<p>&nbsp; =<\/p>\n<\/td>\n<td width=\"155\" valign=\"top\">\n<p>&nbsp; Value<\/p>\n<\/td>\n<td width=\"62\" valign=\"top\">\n<p>&nbsp; }<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"30\" valign=\"top\">\n<p>&nbsp; @<\/p>\n<\/td>\n<td width=\"25\" valign=\"top\">\n<p>&nbsp; {<\/p>\n<\/td>\n<td width=\"102\" valign=\"top\">\n<p>&ldquo;Get-Process<\/p>\n<\/td>\n<td width=\"18\" valign=\"top\">\n<p>&nbsp; :<\/p>\n<\/td>\n<td width=\"224\" valign=\"top\">\n<p>&nbsp; Name&rdquo;<\/p>\n<\/td>\n<td width=\"23\" valign=\"top\">\n<p>&nbsp; =<\/p>\n<\/td>\n<td width=\"155\" valign=\"top\">\n<p>&nbsp; &ldquo;Explorer&rdquo;<\/p>\n<\/td>\n<td width=\"62\" valign=\"top\">\n<p>&nbsp; }<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the following example, I create a default value for the <b>Name<\/b> parameter of the <b>Get-Process<\/b> cmdlet. This permits me to quickly find information about the <b>Explorer<\/b><i> <\/i>process.<\/p>\n<p style=\"padding-left: 30px\">$PSDefaultParameterValues = @{&#8220;Get-Process:name&#8221;=&#8221;explorer&#8221;}<\/p>\n<p>Now when I run the <b>Get-Process<\/b> cmdlet, it only returns information about the <strong>E<\/strong><b>xplorer<\/b><i> <\/i>process. I can override this by using a wildcard character. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">gps *<\/p>\n<p>The commands and the output associated with the commands are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4331.HSG-9-19-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4331.HSG-9-19-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I open a new Windows PowerShell console, the new defaults do not apply. To change the default behavior of a cmdlet, function, or advanced function that happens every time I open the Windows PowerShell console or Windows PowerShell ISE, I need to add the command to my Windows PowerShell profile.<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Note<\/b> &nbsp;&nbsp;For more information about profiles, see <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=profile&amp;sections=7618\" target=\"_blank\">this collection of Hey, Scripting Guy! Blogs<\/a>.<\/p>\n<p>That is all there is to creating and to using custom default values in Windows PowerShell&nbsp;3.0. Join me tomorrow for more cool Windows PowerShell&nbsp;3.0 stuff.<\/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: Windows PowerShell&nbsp;3.0 introduces the PSDefaultParameterValues automatic variable, which permits creating custom default values. Microsoft Scripting Guy, Ed Wilson, is here. Life is returning to normal&mdash;at least for this week. Next week, the Scripting Wife and I head north where I will be speaking at the Central Ohio PowerShell User Group (COPUG) on October 2, [&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,364,3,4,45],"class_list":["post-4954","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-powershell-3-0","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Windows PowerShell&nbsp;3.0 introduces the PSDefaultParameterValues automatic variable, which permits creating custom default values. Microsoft Scripting Guy, Ed Wilson, is here. Life is returning to normal&mdash;at least for this week. Next week, the Scripting Wife and I head north where I will be speaking at the Central Ohio PowerShell User Group (COPUG) on October 2, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4954","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=4954"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4954\/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=4954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}