- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to pass the parameters in the PowerShell function?
You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don’t need to pass the argument because the variable is itself a Public and can be accessible inside the function. But in some cases we need to pass the parameters to the function and below is the example explains how you can write the code for it.
The single parameter passed in function,
function writeName($str){ Write-Output "Hi! there .. $str" } $name = Read-Host "Enter Name" writeName($name)
Here, we are passing the $name in the WriteName function and $str variable in the function catches the parameter so, inside the function, you can use $str to retrieve the value.
Output
Enter Name: PowerShell Hi! there .. PowerShell
For passing multiple values to the function,You can’t use the other programming language method to pass the multiple values to the parameter. Below example is the wrong way,
writeName($name1,$name2)
Instead, in PowerShell, you can use the method mention below to pass the multiple values.
Example
writeName $name1 $name2 function writeName($str1,$str2){ Write-Output "Hi! there .. $str1 $str2" } $name = Read-Host "Enter Name" $surname = Read-Host "Enter Name" writeName $name $surname
Output
Enter Name: Harry Enter Name: Potter Hi! there .. Harry Potter
- Related Articles
- How to pass optional parameters to a function in Python?
- How to pass keyword parameters to a function in Python?
- How to pass the value 'undefined' to a function with multiple parameters in JavaScript?
- How to pass reference parameters PHP?
- How to get supported parameters of the cmdlet in PowerShell?
- How to pass parameters to a method in C#?
- How can I pass optional or keyword parameters from one function to another in Python?
- How to pass pointers as parameters to methods in C#?
- How to pass parameters using param array in a C# method?
- How to pass arguments in Invoke-Command in PowerShell?
- How to add help in the PowerShell function?
- How to get services based on multiple conditional parameters in PowerShell?
- How to use the ValidateRange attribute in PowerShell function?
- How to use the ValidateSet Attribute in PowerShell function?
- How to use the ValidateScript attribute in PowerShell function?
