# Using argument or default values in PowerShell Script

**URL:** <https://forums.powershell.org/t/using-argument-or-default-values-in-powershell-script/14944>\
**Category:** PowerShell Help\
**Created:** [September 14, 2020, 3:52am UTC](https://forums.powershell.org/t/using-argument-or-default-values-in-powershell-script/14944 "2020-09-14T03:52:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![powerslearner135](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@powerslearner135](https://forums.powershell.org/u/powerslearner135)\
**Post date:** [September 14, 2020, 3:52am UTC](https://forums.powershell.org/t/using-argument-or-default-values-in-powershell-script/14944/1 "2020-09-14T03:52:20Z")

</div>

Hi,

I want my PowerShell script to use five variable values that the user will enter but if there is no value provided by the user, than it should take the default values that I would provide for those variables at the beginning.  
What is the best approach to do this?

Thank you

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [September 14, 2020, 3:57am UTC](https://forums.powershell.org/t/using-argument-or-default-values-in-powershell-script/14944/2 "2020-09-14T03:57:22Z")

</div>

Should be able to add something like below at the top of your script.

```
    Param
(
	[string]$param1 = "some default value",
	[string]$param2 = "some default value",
	[string]$param3 = "some default value",
	[string]$param4 = "some default value",
	[string]$param4 = "some default value"
)

```

---

<div class="post-metadata">

**Author:** ![powerslearner135](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@powerslearner135](https://forums.powershell.org/u/powerslearner135)\
**Post date:** [September 14, 2020, 4:01am UTC](https://forums.powershell.org/t/using-argument-or-default-values-in-powershell-script/14944/3 "2020-09-14T04:01:58Z")

</div>

Yes, this is for setting up the default values. But firstly I need to check if the user enters values for these variables.  
If it does, than we should use the values that the user has entered. If there are no values provided, than the default ones.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 14, 2020, 4:17am UTC](https://forums.powershell.org/t/using-argument-or-default-values-in-powershell-script/14944/4 "2020-09-14T04:17:28Z")

</div>

That’s exactly how the parameter binder works. David’s example is exactly what you described. If the user provides any of those individually, those values will be used… otherwise the default values will be used. You would be duplicating work already handled by powershell. Maybe this example will help illustrate.

```
Function Test-DefaultValue {
    Param (
        $a = 'a',
        $b = 'b',
        $c = 'c'
    )

    'a','b','c' | foreach {
        write-host The value of parameter $_ is (get-variable $_).value
    } 
}
Test-DefaultValue
```

When called without providing values as shown, the output is

```
The value of parameter a is a
The value of parameter b is b
The value of parameter c is c
```

If you provide 1 or all with values, they will be used instead

```
Test-DefaultValue -a 'powershell' -c 'org'

The value of parameter a is powershell
The value of parameter b is b
The value of parameter c is org
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/using-argument-or-default-values-in-powershell-script/14944/5 "2024-05-16T20:28:55Z")

</div>


