Home > Mobile >  Set new defaults for PowerShell commands
Set new defaults for PowerShell commands

Time:09-17

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.

CodePudding user response:

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