# Parameters in Functions

**URL:** <https://forums.powershell.org/t/parameters-in-functions/9274>\
**Category:** PowerShell Help\
**Created:** [August 31, 2017, 7:18am UTC](https://forums.powershell.org/t/parameters-in-functions/9274 "2017-08-31T07:18:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnbull5283](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@turnbull5283](https://forums.powershell.org/u/turnbull5283)\
**Post date:** [August 31, 2017, 7:18am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/1 "2017-08-31T07:18:50Z")

</div>

Hi All

I am relatively new to the powershell seen but i am keep to learn about it so i ave been watching videos and learning.

I currently want to create a script where i can run a certain {script block} depending on the parameter used in my function  
e.g. i want something like

function set-stuff {

param(  
$param1  
$param2  
$param3  
)

if ($param1) {do this}  
if ($param2) {do this}  
if ($param3) {do this}

}

but i don’t want to declare the Variable values until one is chosen,can this be done and can anyone guide me

Thanks all in Advance

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 31, 2017, 7:26am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/2 "2017-08-31T07:26:51Z")

</div>

So … welcome to the brighter world of Powershell scripting! 😉

> but i don't want to declare the Variable values until one is chosen,can this be done and can anyone guide me

I guess you will have to explain that a little more. You cannot change the code of your function after you called it!?

---

<div class="post-metadata">

**Author:** ![turnbull5283](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@turnbull5283](https://forums.powershell.org/u/turnbull5283)\
**Post date:** [August 31, 2017, 7:38am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/3 "2017-08-31T07:38:08Z")

</div>

i wondered if i could call a function with a param that has been chosen from a list, then once the param has been chosen get the user to give it a value so it runs a certain script block?

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 31, 2017, 7:44am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/4 "2017-08-31T07:44:32Z")

</div>

so you want to call a function with an empty variable and then prompt the user for a value from inside the function ?

---

<div class="post-metadata">

**Author:** ![turnbull5283](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@turnbull5283](https://forums.powershell.org/u/turnbull5283)\
**Post date:** [August 31, 2017, 7:47am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/5 "2017-08-31T07:47:04Z")

</div>

yes is possible

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 31, 2017, 7:47am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/6 "2017-08-31T07:47:15Z")

</div>

Like most of the time: It depends. 😉 You have several options to make your function fit your needs. You could make your parameter mandatory - so the user has to provide a value. You could create different parameter sets - so the user has to chose one of it. Or you could … 😉

---

<div class="post-metadata">

**Author:** ![liam-fisher](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/liam-fisher/32/435_2.png) [@liam-fisher](https://forums.powershell.org/u/liam-fisher)\
**Post date:** [August 31, 2017, 7:50am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/7 "2017-08-31T07:50:12Z")

</div>

It would be entirely possible, but perhaps not the best practiced thing to do. What is the use-case for this?

---

<div class="post-metadata">

**Author:** ![liam-fisher](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/liam-fisher/32/435_2.png) [@liam-fisher](https://forums.powershell.org/u/liam-fisher)\
**Post date:** [August 31, 2017, 7:51am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/8 "2017-08-31T07:51:03Z")

</div>

Could you provide a few examples of how you would call and use this function?

---

<div class="post-metadata">

**Author:** ![turnbull5283](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@turnbull5283](https://forums.powershell.org/u/turnbull5283)\
**Post date:** [August 31, 2017, 7:53am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/9 "2017-08-31T07:53:12Z")

</div>

This is purley for learning purposes at the moment to see if i can do it, might be used a couple of times a month

---

<div class="post-metadata">

**Author:** ![turnbull5283](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@turnbull5283](https://forums.powershell.org/u/turnbull5283)\
**Post date:** [August 31, 2017, 7:56am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/10 "2017-08-31T07:56:50Z")

</div>

If mandatory they would have to be declared even if the user doesn’t decide to use that param in the Function, which i do really want to do. ive not worked with parameter sets, can these be used to run differnt script blocks?

---

<div class="post-metadata">

**Author:** ![liam-fisher](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/liam-fisher/32/435_2.png) [@liam-fisher](https://forums.powershell.org/u/liam-fisher)\
**Post date:** [August 31, 2017, 7:58am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/11 "2017-08-31T07:58:52Z")

</div>

I can’t really be sure about requirements as your request is quite vague  
You could create a ValidateSet and switch on that to do different things based on the input of $Do

```
function Do-Thing
{
    param(
        [Parameter(Mandatory = $true)]
        [ValidateSet('Thing1', 'Thing2', 'Thing3')]
        [string]$Do
    )

    switch($Do) {
        'Thing1' {
            Write-Output 'Doing Thing 1'
        }

        'Thing2' {
            Write-Output 'Doing Thing 2'
        }

        'Thing3' {
            Write-Output 'Doing Thing 3'
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![turnbull5283](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@turnbull5283](https://forums.powershell.org/u/turnbull5283)\
**Post date:** [August 31, 2017, 8:02am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/12 "2017-08-31T08:02:51Z")

</div>

ok looks good so i would use  
Do-Thing -Thing1 or -Thing2 or -Thing3?

function Do-Thing  
{  
param(  
[Parameter(Mandatory = $true)]  
[ValidateSet(‘Thing1’, ‘Thing2’, ‘Thing3’)]  
[string]$Do  
)

```
switch($Do) {
    'Thing1' {
        Write-Output 'Doing Thing 1'
    }

    'Thing2' {
        Write-Output 'Doing Thing 2'
    }

    'Thing3' {
        Write-Output 'Doing Thing 3'
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 31, 2017, 8:05am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/13 "2017-08-31T08:05:11Z")

</div>

yes that would work

---

<div class="post-metadata">

**Author:** ![liam-fisher](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/liam-fisher/32/435_2.png) [@liam-fisher](https://forums.powershell.org/u/liam-fisher)\
**Post date:** [August 31, 2017, 8:07am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/14 "2017-08-31T08:07:11Z")

</div>

No, in that case you would use

```
Do-Thing -Do Thing1
```

For the functionality you’re describing you would use switch parameters

```
function Do-Thing
{
    param(
        [switch]$Thing1,

        [switch]$Thing2,

        [switch]$Thing3
    )

    if ($Thing1.IsPresent) {
        Write-Output 'Doing Thing 1'
    }

    if ($Thing2.IsPresent) {
        Write-Output 'Doing Thing 2'
    }

    if ($Thing3.IsPresent) {
        Write-Output 'Doing Thing 3'
    }
}
```

```
Do-Thing -Thing1 -Thing2 -Thing3
```

---

<div class="post-metadata">

**Author:** ![turnbull5283](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@turnbull5283](https://forums.powershell.org/u/turnbull5283)\
**Post date:** [August 31, 2017, 9:29am UTC](https://forums.powershell.org/t/parameters-in-functions/9274/15 "2017-08-31T09:29:55Z")

</div>

I played around a bit and your guidance was appreciated, now i got it to work how i want

Thanks

---

<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:37pm UTC](https://forums.powershell.org/t/parameters-in-functions/9274/16 "2024-05-16T20:37:40Z")

</div>

Closed on May 16, 2024, 8:37pm UTC


