{"id":6321,"date":"2015-05-01T00:01:00","date_gmt":"2015-05-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/05\/01\/powershell-tips-and-tricks-using-default-parameter-values\/"},"modified":"2019-02-18T10:29:42","modified_gmt":"2019-02-18T17:29:42","slug":"powershell-tips-and-tricks-using-default-parameter-values","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-tips-and-tricks-using-default-parameter-values\/","title":{"rendered":"PowerShell Tips and Tricks: Using Default Parameter Values"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using default Windows PowerShell parameter values.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The last thing I want to talk about from the tips and tricks session of the Charlotte Windows PowerShell User Group with Lee Holmes is using default Windows PowerShell parameter values.<\/p>\n<p>To effectively use Windows PowerShell default parameter values, I need to first evaluate how I work. For example, if I routinely add <b>&ndash;Wrap<\/b> when I am using the <b>Format-Table<\/b> cmdlet, it might make sense for me to set that as my default.<\/p>\n<p>To do this, I simply need to assign a value for the <b>$PSDefaultParameterValues<\/b> automatic variable. But this is not completely simple, because I need to assign my values as a type of hash table. (It is actually a System.Management.Automation.DefaultParameterDictionary object, but basically it is a hash table).<\/p>\n<p>This means that I will be assigning keys and values to the hash table. The first thing I add is the key, and the second thing I add is my default value.<\/p>\n<p>If I want to always use <b>&ndash;Wrap<\/b> with the <b>Format-Table<\/b> cmdlet, I need to add something like the following:<\/p>\n<p style=\"margin-left:30px\">$PSDefaultParameterValues = @{&quot;Format-Table:Wrap&quot; = $true}<\/p>\n<p>The key name looks a little weird because I specify the cmdlet name and then the parameter name. But if you think about it, maybe it is not too weird because all keys need to be unique, and it is very likely that I might want to specify more than one default parameter for a cmdlet. So this syntax sort of makes sense.<\/p>\n<p>I do not have to add it to my Windows PowerShell profile, but this is a logical place to add it. I could, for example, add it to a script, or simply run it in my Windows PowerShell ISE session. In the following image, I assign the value in my Windows PowerShell ISE session, and then I can take advantage of the default parameter value (if I do not close the Windows PowerShell ISE).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can look at the value of the <b>$PSDefaultParameterValues<\/b> variable and see the hash table I created:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $PSDefaultParameterValues<\/p>\n<p style=\"margin-left:30px\">Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Value&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8212;&#8211;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">Format-Table:Wrap &nbsp; &nbsp; &nbsp; &nbsp;True &nbsp;<\/p>\n<p>If I want to add an additional default parameter value, I simply separate the values with a semicolon. But before I do that, I should stop to see all the places a particular parameter may be used. To do this, I use the <b>Get-Command<\/b> cmdlet and specify the name of the parameter I am interested in using. For example, I often use <b>&ndash;Wrap<\/b> and <b>&ndash;AutoSize<\/b> together, but I want to check to see where <b>&ndash;AutoSize<\/b> is used. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Command -ParameterName autosize<\/p>\n<p style=\"margin-left:30px\">CommandType&nbsp;&nbsp;&nbsp;&nbsp; Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ModuleName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">Cmdlet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Format-Table &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Microsoft.PowerSh&#8230;<\/p>\n<p style=\"margin-left:30px\">Cmdlet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Format-Wide &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Microsoft.PowerSh&#8230;<\/p>\n<p>This makes sense, and if I am using <b>Format-Wide<\/b>, I also add the <b>&ndash;AutoSize<\/b> parameter. With this knowledge, I can use a wildcard formation to add my default parameter value. This is shown here:<\/p>\n<p style=\"margin-left:30px\">$PSDefaultParameterValues = @{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &quot;*:AutoSize&quot;=$true;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &quot;Format-Table:Wrap&quot; = $true;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Now when I run a Windows PowerShell command and pipe it to <b>Format-Table<\/b>, I see that it automatically wraps and sizes. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I want to turn off one of the default parameter values for a single command, I need to remember to overwrite them. In the case of a switched parameter, I add <b>$false<\/b> to the command. This is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Process -FileVersionInfo -ea 0| Format-Table -AutoSize:$false<\/p>\n<p>As shown here, when I run the Windows PowerShell command, I am no longer autosizing my output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can also add a script block that will be evaluated when the cmdlet runs. In the following command, I query Active Directory for all computers. I filter to see if the computer is up and responding to ping requests, and then I create an array of computer names and assign it to the <b>ComputerName<\/b> parameter as a default value.<\/p>\n<p style=\"margin-left:30px\">$PSDefaultParameterValues = @{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &quot;*:ComputerName&quot;= {((Get-ADComputer -filter *).name).Where(<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; {Test-Connection $_ -Quiet})};<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &quot;*:AutoSize&quot;=$true;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &quot;Format-Table:Wrap&quot; = $true;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>This works, as shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-1-15-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The problem with this approach is two-fold. First it is really slow. Every time I want to run a command remotely, I query for a list of computers, and then ping every one in succession to see if they are up. I finally have the list of computers I can use. The second problem is that there are lots of cmdlets that accept a <b>ComputerName<\/b> parameter. Ufortunately, not all of them are firewall friendly, and therefore, not all will work. In addition, some of the commands require special services to be running (such as the remote registry service).<\/p>\n<p>That is all there is to using default Windows PowerShell parameter values. Join me tomorrow when I will have an article from the Scripting Wife about her ideal Ignite 2015 schedule.<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using default Windows PowerShell parameter values. Microsoft Scripting Guy, Ed Wilson, is here. The last thing I want to talk about from the tips and tricks session of the Charlotte Windows PowerShell User Group with Lee Holmes is using default Windows PowerShell parameter values. To effectively use [&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,3,4,45],"class_list":["post-6321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using default Windows PowerShell parameter values. Microsoft Scripting Guy, Ed Wilson, is here. The last thing I want to talk about from the tips and tricks session of the Charlotte Windows PowerShell User Group with Lee Holmes is using default Windows PowerShell parameter values. To effectively use [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6321","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=6321"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6321\/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=6321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}