# Check number of parameters in Powershell script

**URL:** <https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928>\
**Category:** PowerShell Help\
**Created:** [October 3, 2013, 4:42am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928 "2013-10-03T04:42:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkat-musunuri](https://avatars.discourse-cdn.com/v4/letter/v/4bbf92/32.png) [@venkat-musunuri](https://forums.powershell.org/u/venkat-musunuri)\
**Post date:** [October 3, 2013, 4:42am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/1 "2013-10-03T04:42:38Z")

</div>

Hi,

I’m trying to write a code to check how many parameters ( with positions ) were entered when executing a powershell script.  
For example

param ( sourceserver,  
sourcedatabase,  
destinationserver,  
destinationdatabase  
)  
when executing a script , if i miss any parameters it should throw an error saying " a parameter is missing " I know we can put a mandatory params but not sure how to achieve what i’m looking for.  
Is it possible to do in powershell ?  
Please let me know  
thanks in advance

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 3, 2013, 4:47am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/2 "2013-10-03T04:47:03Z")

</div>

```
[CmdletBinding()]
param(
  [Parameter(Mandatory=$True)][string]$sourceserver,
  [Parameter(Mandatory=$True)][string]$sourcedatabase
)
```

For example. The shell will prompt for those values rather than throwing an error. If that’s not the behavior you want, you’d have to code it yourself.

```
if (-not $PSBoundParameters.ContainsKey('sourceserver')) { Write-Error "SourceServer parameter missing" }
```

Or something along those lines.

---

<div class="post-metadata">

**Author:** ![venkat-musunuri](https://avatars.discourse-cdn.com/v4/letter/v/4bbf92/32.png) [@venkat-musunuri](https://forums.powershell.org/u/venkat-musunuri)\
**Post date:** [October 3, 2013, 7:08am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/3 "2013-10-03T07:08:41Z")

</div>

Thanks Don…i’m aware of these mandatory parameters. If we schedule the PS script as SQL agent job ( including mandatory parameters as you mentioned above )  
and if we miss a parameters and job runs, what will be its behavior ?

Thanks

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 3, 2013, 7:55am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/4 "2013-10-03T07:55:51Z")

</div>

If it’s run under the standard PowerShell host, it’d probably hang waiting for input.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 3, 2013, 10:50am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/5 "2013-10-03T10:50:21Z")

</div>

If you add the -NonInteractive argument when calling PowerShell.exe to launch your job, you won’t have that problem. Any missing mandatory parameters will cause an exception instead of prompting the user for input:

```
# Test.ps1:
[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    $TestParam
)

$TestParam
```

```
At command prompt:

C:\Users\Dave\SkyDrive\Source\Temp>powershell.exe -NonInteractive -Command "%cd%\test.ps1"
C:\Users\Dave\SkyDrive\Source\Temp\test.ps1 : Cannot process command because of one or more missing mandatory parameters: TestParam.
At line:1 char:1
+ C:\Users\Dave\SkyDrive\Source\Temp\test.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [test.ps1], ParameterBindingException
    + FullyQualifiedErrorId : MissingMandatoryParameter,test.ps1
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 3, 2013, 10:55am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/6 "2013-10-03T10:55:23Z")

</div>

Running the job through Agent wouldn’t give that option, though. You’d run it as a shell task, so you could specify that.

---

<div class="post-metadata">

**Author:** ![venkat-musunuri](https://avatars.discourse-cdn.com/v4/letter/v/4bbf92/32.png) [@venkat-musunuri](https://forums.powershell.org/u/venkat-musunuri)\
**Post date:** [October 4, 2013, 3:59am UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/7 "2013-10-04T03:59:55Z")

</div>

Thank you both for the response…  
Hi David, i have tried your suggestion, -NonInteractive. job is running but no progress…it never ends.  
One thing i observed ( this is new issue ) is when i run the PS script from powershell console it works fine but when i run it as a sql agent job it fails with some error in code which i dont see when i run it from PS console.

Don, what do you mean by " run it as a shell task" ?

Thanks for your valuable responses,time and patience

---

<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:57pm UTC](https://forums.powershell.org/t/check-number-of-parameters-in-powershell-script/1928/8 "2024-05-16T20:57:03Z")

</div>


