{"id":570,"date":"2021-12-07T06:36:37","date_gmt":"2021-12-07T14:36:37","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell-community\/?p=570"},"modified":"2021-12-07T06:36:37","modified_gmt":"2021-12-07T14:36:37","slug":"how-to-use-psdefaultparametervalues","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell-community\/how-to-use-psdefaultparametervalues\/","title":{"rendered":"How to Use $PSDefaultParameterValues"},"content":{"rendered":"<p><strong>Q:<\/strong> When I use cmdlets like <code>Receive-Job<\/code> and <code>Format-Table<\/code>, how do I change default values of the <strong>Keep<\/strong> and <strong>Wrap<\/strong> parameters?<\/p>\n<p><strong>A:<\/strong> Use the <code>$PSDefaultValues<\/code> automatic variable.<\/p>\n<p>When I first discovered PowerShell&#8217;s background jobs feature, I would use <code>Receive-Job<\/code> to view job output &#8211; only to discover it&#8217;s no longer there. And almost too often to count, I pipe objects to <code>Format-Table<\/code> cmdlet only to get truncated output because I forgot to use <code>-Wrap<\/code>. I&#8217;m sure you all have parameters whose default value you would gladly change &#8211; at least for your environment.<\/p>\n<p>I&#8217;m sure you have seen this (and know how to use <strong>Wrap<\/strong>), like this:<\/p>\n<pre><code class=\"language-powershell-console\">PS&gt; # Default output in a narrow terminal window.\nPS&gt; Get-Service | Format-Table -Property Name, Status, Description\n\nName              Status Description\n----              ------ -----------\nAarSvc_f88db     Running Runtime for activating conversational \u2026\nAJRouter         Stopped Routes AllJoyn messages for the local \u2026\nALG              Stopped Provides support for 3rd party protoco\u2026\nAppHostSvc       Running Provides administrative services for I\u2026\n...\nPS &gt; # Versus this using -Wrap\nPS &gt; Get-Service | Format-Table -Property Name, Status, Description -Wrap\n\nName             Status Description\n----             ------ -----------\nAarSvc_f88db    Running Runtime for activating conversational agent\n                        applications\nAJRouter        Stopped Routes AllJoyn messages for the local\n                        AllJoyn clients. If this service is stopped\n                        the AllJoyn clients that do not have their\n                        own bundled routers will be unable to run.\nALG             Stopped Provides support for 3rd party protocol\n                        plug-ins for Internet Connection Sharing\nAppHostSvc      Running Provides administrative services for IIS,\n                        for example configuration history and\n                        Application Pool account mapping. If this\n                        service is stopped, configuration history\n                        and locking down files or directories with\n                        Application Pool specific Access Control\n                        Entries will not work.\n<\/code><\/pre>\n<p>So, the question is: how to tell PowerShell to always use <code>-Wrap<\/code> when using <code>Format-Table<\/code> or <code>Format-List<\/code>? It turns out there is a very simple way: use the <code>$PSDefaultParameters<\/code> automatic variable.<\/p>\n<h2>The <code>$PSDefaultParameters<\/code> automatic variable<\/h2>\n<p>When PowerShell (and Windows PowerShell) starts, it creates the <code>$PSDefaultParameters<\/code> automatic variable. The variable has a type: <strong>System.Management.Automation.DefaultParameterDictionary<\/strong>. In other words, the variable is a Powershell hash table. By default, the variable is empty when you start PowerShell.<\/p>\n<p>Each entry in this hash table defines a cmdlet, a parameter and a default value for that parameter. The hash table key is the name of the cmdlet, followed by a colon (<code>:<\/code>), and then the name of the parameter. The hash table value for this key is the new default value for the parameter.<\/p>\n<p>If you wanted, for example, to always use <strong>-Wrap<\/strong> for the <code>Format-*<\/code> cmdlets, you could do this:<\/p>\n<pre><code class=\"language-PowerShell\">$PSDefaultParameterValues.Add('Format-*:Wrap', $True)<\/code><\/pre>\n<h2>Persist the change in your profile<\/h2>\n<p>Any change you make to the <code>$PSDefaultParameterValues<\/code> variable is only applicable for the current session. And the variable is subject to normal scoping rules &#8211; so changing the value in a script does not affect the session as a whole. That means that if you want these changes to occur every time you start a PowerShell console, then you add the appropriate statements in your profile.<\/p>\n<p>On my development box, I use the following snippet inside my <code>$PROFILE<\/code> script:<\/p>\n<pre><code class=\"language-powerShell\">$PSDefaultParameterValues.Add('Format-*:AutoSize', $true)\n$PSDefaultParameterValues.Add('Format-*:Wrap', $true)\n$PSDefaultParameterValues.Add('Receive-Job:Keep', $true)<\/code><\/pre>\n<h2>Summary<\/h2>\n<p>The <code>$PSDefaultParameterValues<\/code> automatic variable is a great tool to help you specify specific values for cmdlet parameters. You can specify one or more cmdlets by using wild cards in the hash table&#8217;s key. Remember that the hash table key is the name of the cmdlet(s), a colon, and then the parameter&#8217;s name. Also, the hash table value is the new &#8220;default&#8221; value for that parameter (and for the specified cmdlet(s)).<\/p>\n<p>You can read more about <code>$PSDefaultParameterValues<\/code>, and other preference variables in <a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/microsoft.powershell.core\/about\/about_preference_variables#psdefaultparametervalues\">about_Preference_Variables<\/a>. And for more details of parameter default values, see the <a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/microsoft.powershell.core\/about\/about_parameters_default_values\">about_Parameters_Default_Values help file<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: When I use cmdlets like Receive-Job and Format-Table, how do I change default values of the Keep and Wrap parameters? A: Use the $PSDefaultValues automatic variable. When I first discovered PowerShell&#8217;s background jobs feature, I would use Receive-Job to view job output &#8211; only to discover it&#8217;s no longer there. And almost too often [&hellip;]<\/p>\n","protected":false},"author":4034,"featured_media":77,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"tags":[59,60],"class_list":["post-570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-default-parameter-values","tag-parameters"],"acf":[],"blog_post_summary":"<p>Q: When I use cmdlets like Receive-Job and Format-Table, how do I change default values of the Keep and Wrap parameters? A: Use the $PSDefaultValues automatic variable. When I first discovered PowerShell&#8217;s background jobs feature, I would use Receive-Job to view job output &#8211; only to discover it&#8217;s no longer there. And almost too often [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/users\/4034"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}