# Parameters

## Parameters

Jobs support automatically generating forms with parameters based on your script's `param` block. The type of control will change based on the type you define in the block. Parameters that are mandatory will also be required by the UI.

### Basic Parameters

Parameters can be simply defined without any type of parameter attribute and they will show up as text boxes in the UI.

```powershell
param($Test)

$Test
```

### Parameters Types

Universal supports various types of parameters. You can use String, String\[], Int, DateTime, Boolean, Switch and Enum types.

#### String

You can define string parameters by specifying the `[String]` type of by not specifying a type at all. Strings will generate a textbox.

```powershell
param(
    [String]$Textbox,
    $Textbox2
)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-94483f06ebf9fb90f6192786eb744b64b088394d%2Fimage%20\(410\).png?alt=media)

#### String Arrays

You can specify string arrays by using the `[String[]]` type specifier. String arrays will generate a multi-tag select box.

```powershell
param([String[]]$Array)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-0a055faee7f51769f454ccfe49a1bc0ba46bf0ea%2Fimage%20\(20\).png?alt=media)

#### Date and Time

You can use the `[DateTime]` type specifier to create a date and time selector.

```powershell
param([DateTime]$DateTime)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-88595b8e9773304756d1f728ffb1e2e1c6c1ee46%2Fimage%20\(220\).png?alt=media)

#### Boolean

You can use a `[Bool]` type selector to create a switch.

```powershell
param([Bool]$Switch)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-dab6623590c45c409e47e3d99f6c1ab50a360426%2Fimage%20\(8\)%20\(1\).png?alt=media)

#### Integer

You can define a number selector by using the `[Int]` type specifier.

```powershell
param([Int]$Number)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-8449e88f7d6f424da51d78ebe6427f79dbb89fde%2Fimage%20\(216\).png?alt=media)

#### Switch Parameter

You can define a switch parameter using the `[Switch]` type specifier to create a switch.

```powershell
param([Switch]$Switch)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-e2f8818bd9d4f48b7f686c5a688ba9c2589c6f23%2Fimage%20\(95\).png?alt=media)

#### Enumerations

You can use System.Enum values to create select boxes. For example, you could use the `System.DayOrWeek` to create a day of the week selection box.

```powershell
param([System.DayOfWeek]$DayOfWeek)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-00acaf8b920f3429c7e3383f5d0b4532472e45af%2Fimage%20\(566\).png?alt=media)

#### PSCredential

When you specify a `PSCredential` , the user will be presented with a drop down of credentials available as [variables](https://docs.powershelluniversal.com/platform/variables#creating-a-secret-variable).

```powershell
param(
    [PSCredential]$Credential
)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-4b568aa8d83379aa7ed22a089167ce5c5368efe6%2Fimage%20\(69\).png?alt=media)

#### File

You can allow users to upload files by using the `[File]` type.

```powershell
param(
    [File]$File
)
```

Files will be available as a `PSUFile` object in your scripts. This object has a `byte[]` array that you can use to process the file.

For example, you can get the string content for the file by converting it using the Encoding classes.

```powershell
[Text.Encoding]::UTF8.GetString($File.Content)
```

### Display Name

You can use the `DisplayNameAtrribute` to set a display name for the script parameter.

```powershell
param(
    [ComponentModel.DisplayName("My Script")]
    $MyScript
)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-7961670e7b094b53198340d362fe65229d654608%2Fimage%20\(391\).png?alt=media)

### Help Messages

You can define help messages for your parameters by using the `HelpMessage` property of the `Parameter` attribute.

```powershell
param(
    [Parameter(HelpMessage = "Class you want to enroll in")]
    [string]$Class
)
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-d6c5b7af246ed70bf4b5016bee3517fa6a1f66cb%2Fimage%20\(182\).png?alt=media)

### Required Parameters

You can use the Parameter attribute to define required parameters.

```powershell
param(
    [Parameter(Mandatory)]
    $RequiredParameter
)

$RequiredParameter
```

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-6f11ebf6070a52329491e6eb3d2099ad5cc4c58b%2Fimage%20\(547\).png?alt=media)

### Default Value

You can use both static and default values for parameters. The default value is calculated when the job is about to be run.

```powershell
param(
    $Parameter = "Hello, World",
    [DateTime]$ExecutionTime = Get-Date
)

$Parameter
$ExecutionTime
```

## ValidateSet

The `ValidateSet` attribute is used to enforce which values can be passed to a parameter. Learn about [ValidateSet here.](https://duckduckgo.com/?q=validateset\&ia=web) PowerShell Universal will automatically create a drop-down menu with the values provided to the `ValidateSet` attribute for parameters.

For example, a script could define a param block as follows.

```powershell
param(
    [ValidateSet("Steve", "Mary")]
    [string]$Name
)
```

The result is shown below.

<figure><img src="https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-8a8a413a25f1ade962a49e456ce7107391d2225a%2Fimage.png?alt=media" alt=""><figcaption><p>ValidateSet Attribute</p></figcaption></figure>

## Passing Parameters from PowerShell

You can pass parameters from PowerShell using the `Invoke-PSUScript` cmdlet. This cmdlet supports dynamic parameters. If you have a `param` block on your script, these parameters will automatically be added to `Invoke-PSUScript`.

For example, I had a script named Script1.ps1 and the contents were are follows.

```powershell
param($MyParameter)

$MyParameter
```

I could then invoke that script using this syntax.

```powershell
Invoke-PSUScript -Name 'Script.ps1' -MyParameter "Hello"
```

The result would be that Hello was output in the job log and pipeline.

## Parameter Sets

PowerShell Universal supports parameter sets. When a parameter set is defined, a drop down is provided that allows for switching between the sets.

```powershell
param(
    [Parameter(ParameterSetName = 'Set1')]
    $Parameter1,
    [Parameter(ParameterSetName = 'Set2')]
    $Parameter2
)
```

![Parameter Sets](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-f43d04b83d480814f3782620c20ba2f0308ba7b1%2Fimage%20\(408\).png?alt=media)
