How to use PowerShell's default parameter to change the command behavior

Many people like to use PowerShell because it allows them to do things that can't be done with the Windows GUI. However, it is undeniable that some PowerShell cmdlets can be tedious or complicated. But what if there is a way to edit these cmdlets and get them to 'behave' the way you want? Believe it or not, there is an easy way to do that. You can change the behavior of the cmdlet command by modifying the value of the default PowerShell parameters. This article will show you how to use the default PowerShell parameter to change the cmdlet command behavior.

Warning

Before starting, you need to keep in mind some things. Although the default PowerShell parameter change and cmdelt behavior are very convenient but can lead to problems. If you run a script and the script assumes the modified cmdlet will work in a certain way, you can get unpredictable results. Therefore, you should make sure to change the default PowerShell parameter carefully.

Review fast

There are two PowerShell concepts that you need to be familiar with to work with the default parameters. The first concept is variable. In PowerShell, variable names always begin with a dollar sign and you can write the contents of the variable by entering the variable name.

The second concept you might be familiar with is the hash table. The hash table is basically a list made up of key / value pairs. For example, suppose you want to create a hash table containing the names of American states and their abbreviations. Here is the code:

$ StateList = @ {}
$ StateList.add ('Florida', 'FL')
$ StateList.add ('South Carolina', 'SC')
$ StateList.add ('Georgia', 'GA')
$ StateList

The first line of code creates a blank hash table called $ StateList . The next three lines add items to the table. Each entry is made up of a key (state name) and a value (state abbreviation). The last line lists the contents of the table. You can see the active code in the image below.

How to use PowerShell's default parameter to change the command behavior Picture 1

This is just a very simple example and there are many other ways to use hash tables.

Works with PowerShell default parameters

PowerShell has a built-in variable used to store the default parameters used with its cmdlet. This variable is called $ PSDefaultParameterValues . You may have noticed from the initial introduction and the name of the variable, this is not a common variable but a hash table. However, if you enter the variable name into PowerShell, you will quickly see that this table is empty, as shown below.

How to use PowerShell's default parameter to change the command behavior Picture 2

So what can we do with this $ PSDefaultParameterValues variable. As mentioned above, you can control the behavior of the PowerShell cmdlet command. The only point is that you cannot overcome the inherent capabilities of the cmdlet command. So see an example.

This example, may not be the one you want to use in real life, but it will show you how easy it is to change the behavior of the cmdlet thoroughly. If you enter the Get-TimeZone cmdlet command in PowerShell, you will see the name of the time zone the PC is currently configured to use. You can see this example in the image below.

How to use PowerShell's default parameter to change the command behavior Picture 3

 

Now we change this cmdlet so that it does not display the system time zone that is configured to use but instead the available time zones. To do this, we need the cmdlet syntax using the cmdlet Get-Help followed by the cmdlet Get-TimeZone . You can see the syntax of the cmdlet below.

How to use PowerShell's default parameter to change the command behavior Picture 4

This syntax includes a parameter called ListAvailable . Please enter the command below:

$ PSDefaultParameterValues.Add ('Get-TimeZone: ListAvailable', $ True)

The first part of this command simply tells PowerShell that you want to add a value to the hash table, just like you did in the previous US state abbreviation example. This hash table contains a key / value pair. In this case the key is the name cmdlet (Get-TimeZone) followed by a comma and the name of the parameter you want to set. In this case, the parameter name is ListAvailable. The second part of this cmdlet is the value you want to assign to the parameter. Here is $ True . The ListAvailable parameter usually does not need a value, so specifying the value $ True is the way you require PowerShell to use this parameter without attaching a value to it.

If you look at the image below, you will see what happens when you run the Get-TimeZone cmdlet command.

How to use PowerShell's default parameter to change the command behavior Picture 5

Things to remember

The most important thing is to understand that adding a new value to the default PowerShell parameter is to change the default behavior of the cmdlet command, without deleting the capabilities of that cmdlet. Even when you change the Get-TimeZone cmdlet command to show the available time zones instead of the current time zone. You can still display the current time zone information if you see more.

Another thing you need to know is that you can delete custom default parameters. To delete a single entry, use the $ PSDefaultParameterValues.Remove command, followed by the item name you want to delete. For example:

$ PSDefaultParameterValues.Remove ('Get-TimeZone: ListAvailable')

Instead, you can delete the entire contents of the hash using this command:

$ PSDefaultParameterValues.Clear ();

You can see an example of both techniques below:

How to use PowerShell's default parameter to change the command behavior Picture 6

You can delete the entire hash table or delete a single entry.

Changing the default behavior of the cmdlet command is something you don't normally do but when you need to know how to change it.

I wish you all success!

See more:

  1. How are Command Prompt (cmd) and PowerShell different?
  2. 10 PowerShell commands help you manage the remote computer most effectively
  3. Discover 10 features of PowerShell
5 ★ | 1 Vote

May be interested

  • Fsutil 8dot3name command in WindowsFsutil 8dot3name command in Windows
    fsutil 8dot3name command queries or changes settings for short name behavior (name 8dot3)
  • Del command in WindowsDel command in Windows
    the del command deletes one or more files. this command is the same as the eraser command.
  • Bitsadmin getdisplayname and bitsadmin geterror command in WindowsBitsadmin getdisplayname and bitsadmin geterror command in Windows
    the bitadmin getdisplayname command takes the display name of the specified task. the bitsadmin geterror command retrieves detailed error information for the specified job.
  • Color command in WindowsColor command in Windows
    the color command changes the foreground and background colors in the command prompt window for the current session. if used without parameters, the color command restores the default foreground and background colors of the command prompt window.
  • Thread command in windowsThread command in windows
    the renaming command helps change file names or directories.
  • Command mode in WindowsCommand mode in Windows
    display system status, change system settings or reconfigure port and connected device.
  • Cmd command in WindowsCmd command in Windows
    the cmd command starts a new version of the command interpreter, cmd.exe. if used without parameters, cmd will display copyright information and the version of the operating system.
  • Set command in WindowsSet command in Windows
    the set command helps show, set or remove cmd.exe environment variables.
  • Fc command in WindowsFc command in Windows
    the fc command compares two files or a collection of files and displays the differences between them.
  • Bitsadmin getmodificationtime and bitsadmin getnoprogresstimeout commandBitsadmin getmodificationtime and bitsadmin getnoprogresstimeout command
    the bitadmin getmodificationtime command retrieves the last time the task was modified or the data was successfully transferred. the bitsadmin getnoprogresstimeout command retrieves the duration, in seconds, but the service will try to transfer the file after a temporary error occurs.