EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Powershell Parameter
Secondary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Powershell Parameter

Powershell Parameter

Introduction to Powershell Parameter

A parameter is nothing but an input provided to a function or any cmdlet. Every parameter will have a name and a data type associated with it. It is always not necessary that parameters are mandatory. Some parameters may also have default values, and these values are used when a value for the parameter is not explicitly mentioned. For a function, the parameters are defined with the Param block. Mandatory parameters are defined using the [Parameter (Mandatory)] attribute. It is also possible to validate the value passed to each parameter using the ValidateSet property. Parameter names are always preceded by a hyphen (-), which denotes the PowerShell that the word after (-) is a parameter. This article will explain in detail about the parameters and their types in PowerShell, the various types of parameters, how to pass parameters to a function, etc., in detail.

Syntax of Powershell Parameter

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The following example shows how to pass parameters to a cmdlet

Get-ChildItem -Path “C:\vignesh\test”

In the above example, the path is a parameter for the cmdlet, and the corresponding value of the parameter is enclosed within “”.

To define parameters for a function, the below format is used

param(
[Parameter()] [string]$Testparameter1
[Parameter()] [string]$Testparameter2
[Parameter()] [int]$Testparameter3
)

Identifying the various parameters associated with a cmdlet:

To identify the various parameters that are available for a cmdlet, the below cmdlet can be used.

Get-Help CmdletName -Parameter *

Example:

Get-Help out-file -Parameter *

Output:

powershell parameter output 1

The above shows the various parameters that are associated with the Out-File cmdlet. It also shows whether a parameter is a mandatory one, its position, aliases.

Attributes of a parameter

Here are the attributes of a parameter mentioned below

-Required

This denotes whether the parameter is a must for running this cmdlet. If this value is true for a parameter, then it means that this is a mandatory one. An error will be thrown if the appropriate value for that parameter is not passed.

-Position

Positional parameters are parameters that have its position set to a positive integer. When using this type of parameter, the parameter name is not required, but the parameter value must be mentioned in the appropriate position. If the position value is 0, then the parameter name is not required, but its value should be the first to appear after the cmdlets name. If the position setting is excluded, it can be defined anywhere in the cmdlet.

-Type

It denotes the type of the parameter like string, int, switches, etc.

-Default Value

This denotes the default value of the parameter when another value is not specified. For a required parameter, there is never a default value and is always supplied by the user. For many optional parameters, there are no default values as optional parameters don’t have much significance.

-Accepts Multiple Values

This denotes whether a parameter can accept multiple values. In case if multiple values are allowed, they are typed in a comma-separated and passed, or the values can be saved in a comma-separated way in a variable, and that variable can be passed as a value to the parameter.

-Accepts Pipeline Input

This denotes whether the pipeline can be passed as input to the parameter. If its value is false, it denotes the parameter doesn’t accept the pipeline for an input.

-Accepts Wildcard Characters

This denotes whether the parameter can use the wildcard to match characters.

Validation of Parameters

Below are some ways of validating the value that is passed to a parameter.

1. Making a parameter Mandatory and allowing Null Value

The mandatory parameter is used to denote whether a parameter compulsorily requires a value or not. AllowNull attribute is used to allow null values as a value.

Example:

Param(
[Parameter(Mandatory=$true)] [AllowNull()] [String] $UserName
)

In the above, UserName is a mandatory parameter, and it accepts null for a value.

2. AllowEmptyString validation attribute

This attribute is used to allow empty string as a value to the mandatory parameter. The Allow Empty collection attribute is used to allow empty collection as a value to a mandatory string parameter.

Example:

Param(
[Parameter(Mandatory=$true)] [AllowNull()] [AllowEmptyCollection()] [String] $UserName
)

ValidateCount attribute is used to validate the number of values that can be passed to a parameter.

ValidateLenght attribute is used to specify the minimum and maximum length of the value that is passed to a parameter.

Validatepattern is used to match a regular expression with the value that is passed to the parameter.

ValdiateRange specifies a range in which the value of the variable must be.

ValidateSet denotes a set of values from which one of the values must be passed for the parameter. Value outside this set can’t be set to the parameter.

ValidateDrive is used to validate the value of a path parameter to a certain drive

Example:

Param(
[Parameter(Mandatory=$true)] [AllowNull()] [AllowEmptyCollection()] [ValidateCount(5,50)] [ValidateLength(10,20)] [ValidatePattern("[1-9][0-4][4-9][1-4]")] [ValidateDrive("C", "Function", "Drive")] [String] $UserName
)
Example:
Input:
Write-Host "Welcome to parameter example"
function test1()
{
Write-Host "Demo of function without any parameter"
Write-Host "Function without parameters is called"
}
#calling test1 function
test1
function test2($username)
{
Write-Host "Demo of Function with a single parameter"
Write-Host "the value of username supplied is" $username
}
#calling test2 function with parameter
test2 -username "Viki"
function test2
{
Param(
[parameter(Mandatory=$true)] [ValidateLength(1,30)] [String] $Name,
[parameter(Mandatory=$true)] [Int] $age,
[parameter(Mandatory=$true)] [ValidateSet("Chennai", "Mumbai", "Delhi")] [String] $City
)
Write-Host "Demo of fucntion with multiple parameters"
Write-Host "The name supplied is" $Name
Write-Host "the age supplied is "$age
Write-Host "The city passed is " $City
}
#function calling
test2 -Name "Vignesh" -age 28 -City Chennai
function test3
{
Param(
[parameter(position=1)] [ValidateLength(1,30)] [String] $Name,
[parameter(position=2)] [Int] $age,
[parameter(position=3)] [ValidateSet("Chennai", "Mumbai", "Delhi")] [String] $City
)
Write-Host "Demo of fucntion with Positional parameters"
Write-Host "The name supplied is" $Name
Write-Host "the age supplied is "$age
Write-Host "The city passed is " $City
}
#function calling
test3 "viki" 35 "Chennai"

Output:

powershell parameter output 2

Conclusion – Powershell Parameter

Thus, the article covered in detail about parameters in PowerShell. It covered various types of parameters, various attributes that are associated with a parameter, different parameter types, mandatory and non-mandatory parameters, validation of parameters, etc. To learn more in detail about the parameters, it is advisable to write sample scripts and execute them.

Recommended Articles

This is a guide to Powershell Parameter. Here we discuss the attributes of a parameter and some ways of validating the value that is passed to a parameter. You may also have a look at the following articles to learn more –

  1. PowerShell Concatenate String
  2. PowerShell Get-Item
  3. PowerShell Split String
  4. PowerShell Scheduled Task
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more