Parameters
The following can accept parameters
:
1. Cmdlets
2. functions
3. scripts
These make the scripts
more flexible
Declare and Use a Parameter
To use parameters
we use Param()
keyword.
imagine we have a file named script.ps1
Param(
$Path
)
New-Item $Path
Write-Host "File has been created"
The parameters
been to be comma separated.
Use The Parameter
Then inside the PowerShell
we can use it as follows.
./script.ps1 -Path "./newfile"
Improve Your Parameters
You should keep the following questions in mind:
1. Is this parameter
optional or mandatory?
2. What are the legal and illegal values?
3. Does it accept multiple types of values or single type
4. Can it rely on default values?
5. Can you provide details to the user?
Select an Approach
You can do the following things
If / Else
Param(
$Path
)
If (-Not $Path -eq '') {
New-Item $Path
Write-Host "File created at path $Path"
}
Else {
Write-Error "Path cannot be empty"
}
Making Parameters Mandatory
You can make a parameter
mandatory by using a decorator.
Param(
[Parameter(Mandatory)]
$Path
)
If you run the script
without providing the value of the parameter
, PowerShell
would ask you to enter the value for it.
cmdlet script.ps1 at command pipeline position 1 Supply values for the following parameters:
Path:
You can further improve the decorator by providing a HelpMessage
.
[Parameter(Mandatory, HelpMessage = "Please provide a valid path")]
Then if you type !?
, the HelpMessage
will pop up.
cmdlet script.ps1 at command pipeline position 1 Supply values for the following parameters:
(Type !? for Help.)
Path: !?
Please provide a valid path
Assigning a Type
Param(
[string]$Path
)
Default Values
Param(
[string]$Path = "defaut value"
)