Getting Started with PowerShell Parameters
23K views
Apr 24, 2024
Make your scripts and functions resuable with parameters! Learn the basics of parameter data types, mandatory requirements, position, and default values. PowerShell Script Parameters: Getting Started Guide https://jeffbrown.tech/powershell-script-parameters/ Chapters 00:00 Introduction 00:33 Data Types 02:35 Mandatory 04:25 Parameter Position 07:34 Default Values 08:44 Summary
View Video Transcript
0:00
What's up everyone, today I want to dive into PowerShell parameters and how you can use them to improve your scripts and functions that you're writing to make them more reusable and just give them more functionality
0:12
This is a follow up to my last video where I worked on a script that created users out in IntraID
0:19
And one of the things I did in that script was add parameters to input a user data file
0:26
This is also a follow up to a blog post I wrote on getting started with PowerShell parameters
0:31
Let's go ahead and get started. I have a very simple script here called addToNumbers where you can pass in two numbers named firstNumber and secondNumber
0:41
The script is going to display each value and then output the sum of those two numbers
0:48
If I were to run this right now, we'll do firstNumber 10, secondNumber 20, and that outputs just fine
0:55
FirstNumber 10, secondNumber 20, and your sum is 30. The first issue we run into with our parameter definition here on line 6 through 9 is there is no data type
1:08
This can lead to issues where somebody doesn't put in a number but instead types in something like a string here
1:15
If they said 20, that's going to error out because you can't add a number and a string like this
1:23
PowerShell is not a strongly typed language, meaning when you create a variable like firstNumber, you can put any value into it
1:31
You can put a string, an object, an array, whatever it will take unless you specifically put what type of value it's going to be
1:40
In this instance with our parameter, we need to assign a data type just to make sure that people are only allowed to put in numbers when they're using these parameters
1:50
Let's add a data type here. We're going to say this is a double
1:55
These data types are going to be whatever is supported in .NET Framework
1:59
Go ahead and save the script here. Clear out the screen. Let's rerun the incorrect one here again, just where we put in 20
2:09
It still errors out, but it's at least a little bit better. It just says it can't convert 20 to a double because 20 is a string and it's not in the correct format
2:18
However, if we just go back and do 20 as an actual number, it works as expected
2:25
When you're defining your parameters, definitely put in what type of data type you would need to put in there
2:30
Number, a string, maybe it's an object, other things like that. The second thing we want to look at is making sure these parameters are mandatory if our script absolutely needs them to run
2:43
For example, if we were to run this script without any parameters, it's going to do 0, 0, 0
2:49
Really not helpful in what we're trying to do here. We want to be able to add two numbers together
2:55
We want the user running the script to specify numbers even if they happen to be 0
3:00
We can make parameters mandatory by using parameter attributes. Let's start off with our first parameter here
3:08
We will use the parameter keyword here and some open and close parentheses
3:13
This is the parameter attribute section. You can leave it blank. Usually when I'm defining parameters, I will go ahead and put this in here even if I'm not making one mandatory
3:23
Or using things like value from pipeline or parameter sets. Something else we'll probably get into in another video
3:30
In this case, I do want to add an attribute making sure that this parameter is required and mandatory
3:36
I can just add mandatory here. I believe older versions of PowerShell required you to do mandatory equals true
3:44
I think that's a pretty old thing. You can just put mandatory, but just in case you see mandatory equals true, it's the exact same thing
3:52
Let's take this and add it for our second parameter. Let's go ahead and try to rerun our script here without any parameters specified
4:03
You can see it's going to prompt me what you want to put in for the first number and the second number
4:08
I can press enter and it still puts in zero, but at least it's prompting me to put something in
4:14
Same thing if I were to only do one number here, it will prompt for the other number and go through it like that
4:22
If you forget one, it will prompt for the other. Another thing you can look at with your parameters is the idea of position
4:30
You can define what position that they automatically take arguments from and assign them to parameters
4:37
Let's actually jump over to another script here that I have with just a couple of commands in it
4:43
You may already be familiar with position and not even know it
4:47
Let's think about the copy item command. You could run the copy item commandlet and pass it a value here
4:56
You want to copy this item and where do you want to copy it to
5:00
Maybe I want to copy this script and also rename it at the same time
5:05
I can do that exactly. Over here on the left you can see it copied that with its new name
5:12
Notice I did not have to specify parameters with it. It automatically knew that the first one was my original object that I wanted to work with
5:22
and the second one to add three numbers is the destination or the other item that I'm copying to
5:29
You can take a look at parameters and learn if they have positions or not
5:34
To see what this looks like inside of help we can run get help. I'm going to run it on the copy item command
5:40
I'm going to look at the parameters path and destination. Within this you can see it has position attribute
5:47
The path is position zero. Destination is position one. In fact if you look at the help for get help and the name parameter
5:57
you can see the name parameter has a position of zero. Two examples here we just went through that
6:04
Both of these are valid because of parameter position. Going back to our script here PowerShell will automatically assign positions
6:12
to parameters in the order that you specify them. In this case first number is already in position zero
6:19
Second number is already in position one. I could easily just run add two numbers and give it two numbers like this
6:28
This still works fine without defining positions for each parameter. However you might have a script or function with a lot of parameters
6:35
and they may not be in the order that you want them in or you only want to specify specific parameters with positions
6:43
In order to do that you just add the position keyword here
6:49
and what position it is going to be. We'll save our script
6:59
This really doesn't change anything from the default just specifically assigning them to their positions if we wanted to
7:07
Just to demonstrate I am going to switch these around. If we rerun our script here you can see it has actually flipped them
7:18
Since I put second number at position zero ten gets assigned to the second number variable
7:25
and twenty gets assigned to the first number variable. Not really sensical for what we're doing here
7:30
but just wanted to show that that is possible and how it will assign those values
7:35
The last thing we want to take a look at is default values. You can assign a default value to a parameter
7:42
in order to give it a value and the script runner does not have to give it a value inside the parameter
7:49
In this case for second number let's just assign it number ten
7:54
Now let's take a look at some interesting behavior if you were to do this. Let's do add to numbers and we'll say my first number is thirty-three
8:02
It still prompts for second number because it is mandatory but if I were to press enter here
8:08
it's giving it actually a null or zero value probably a zero value
8:12
and it's overriding that default value. If you have a default value assigned to your parameter
8:21
you need to make sure it is probably not mandatory. Let's go ahead and take out the mandatory keyword here
8:29
Clear out the screen and we'll rerun this here and that accurately puts that out
8:36
When you're assigning default values and maybe having them mandatory just test your script and make sure it gives you the behavior that you're looking for
8:44
That does it for this video. This is the very basics of working with parameters
8:49
defining them, looking at mandatory and positions and finding your data types and potentially default values
8:57
In the next video we will dive into a little bit more how you can validate the values that people put into your script parameters
9:05
to make sure you're getting the type of information that you need. Thank you for watching and we'll see you next time
9:19
Microsoft Mechanics www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com www.microsoft.com
#Programming