Set new defaults for PowerShell commands

Viewed 49

Is it possible to set default values for existing commands in PowerShell? What I specifically want to do is to tell the Get-ChildItem command to show both normal and hidden files (Get-ChildItem -Force).

I know I can write a function where I can add this option and use whatever else is specified on the command line. But then I lose the auto-completion functionality for all parameters and options.

1 Answers

You should be able to do so using $PSDefaultParameterValues. Has worked from 3.0+.

Microsoft's documentation:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters_default_values?view=powershell-7.1

Here is a set of examples directly from that documentation:

$PSDefaultParameterValues=@{"CmdletName:ParameterName"="DefaultValue"}

$PSDefaultParameterValues=@{ "CmdletName:ParameterName"={{ScriptBlock}} }

$PSDefaultParameterValues["Disabled"]=$True | $False
Related