# Defining Parameters

**URL:** <https://forums.powershell.org/t/defining-parameters/9462>\
**Category:** PowerShell Help\
**Created:** [October 5, 2017, 11:47am UTC](https://forums.powershell.org/t/defining-parameters/9462 "2017-10-05T11:47:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlogan3o13](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@jlogan3o13](https://forums.powershell.org/u/jlogan3o13)\
**Post date:** [October 5, 2017, 11:47am UTC](https://forums.powershell.org/t/defining-parameters/9462/1 "2017-10-05T11:47:17Z")

</div>

I am working on a script for which I have written a GUI. The user fills out the fields and then the GUI passes those by way of parameters to the underlying script. Below is a reproducer of what I am trying:

```
Param (
   [string]$sVM,
   [string]$iCPU,
   [string]$iRAM,
   [string]$sIP,
   [string]$sSubnet,
   [string]$sGateway,
   [string]$sDNS,
   [string]$sDescription,
   [string]$sSite,
   [string]$sHost,
   [string]$sDStore,
   [string]$sTemplate,
   [string]$sSpec,
   [string]$sFolder,
   [string]$sOU
)

Write-Host $args.Count "args"

    ForEach ($sArg in $args) {
        Write-Host $sArg
    }
```

I am relatively new to PowerShell, having come from other languages, but was under the impression the parameters had to be at the top of the script, which is why I formatted this way. When I run the script with something like this (quotes around those fields with spaces):

> Args.ps1 SPERA-SCCM01 4 16 10.10.120.115 255.255.255.0 10.10.115.254 10.10.1.120 "SCCM 2016 Server" ERA ER-vms33.mycompany.com Dev-ERA-App01 "ER 2016 Template 02" ScriptERADomain ScriptDeploy "OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com"

The issue I am having is, if I run the script just as written above, I get the following output which seems to indicate it found none of the parameters:

> ./Args.ps1 SPERA-SCCM01 4 16 10.10.120.115 255.255.255.0 10.10.115.254 10.10.1.120 "SCCM 2016 Server" ERA ER-vms33.mycompany.com Dev-ERA-App01 "ER 2016 Template 02" ScriptERADomain ScriptDeploy "OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com" 0 args

If I add something to the first line, even just a simple Write-Host “Beginning” or a cls, I get an error on Param (which I would expect, it not being the first line), but the output is what I would expect:

> Param : The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At desktop\Args.ps1:3 char:1 + Param ( + ~~~~~ + CategoryInfo : ObjectNotFound: (Param:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
> 
> 15 args  
> SPERA-SCCM01  
> 4  
> 16  
> 10.10.120.115  
> 255.255.255.0  
> 10.10.115.254  
> 10.10.1.120  
> SCCM 2016 Server  
> ERA  
> [ER-vms33.mycompany.com](http://ER-vms33.mycompany.com)  
> Dev-ERA-App01  
> ER 2016 Template 02  
> ScriptERADomain  
> ScriptDeploy  
> OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com

I have to believe whatever I am doing wrong is simple stupid, but if someone could clue me in that would be much appreciated.

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [October 5, 2017, 1:27pm UTC](https://forums.powershell.org/t/defining-parameters/9462/2 "2017-10-05T13:27:47Z")

</div>

When you define parameters the $args automatic variable takes the OVERFLOW

Using this simple script

```
param (
  [string]$param1,
  [string]$param2
)

Write-Host "`$param1 = $param1"
Write-Host "`$param2 = $param2"
Write-Host "`$args = $args"
```

Test it like this

PS\> .\paramtest.ps1 “one”  
$param1 = one  
$param2 =  
$args =  
PS\> .\paramtest.ps1 “one” “two”  
$param1 = one  
$param2 = two  
$args =  
PS\> .\paramtest.ps1 “one” “two” “three”  
$param1 = one  
$param2 = two  
$args = three  
PS\> .\paramtest.ps1 “one” “two” “three” “four”  
$param1 = one  
$param2 = two  
$args = three four

You’ll see that the values are given to the defined parameters first and only after they have been filled does $args get the leftovers.

---

<div class="post-metadata">

**Author:** ![jlogan3o13](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@jlogan3o13](https://forums.powershell.org/u/jlogan3o13)\
**Post date:** [October 5, 2017, 1:35pm UTC](https://forums.powershell.org/t/defining-parameters/9462/3 "2017-10-05T13:35:55Z")

</div>

Lightbulb moment, I was unaware of that. I guess I thought $args took the full list of params you input. Thanks for this.

---

<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/defining-parameters/9462/4 "2024-05-16T20:37:26Z")

</div>


