PowerShell 7 Tutorial 17: Introduction to Creating Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] in this tutorial I will be using the visual studio code ISC if you need help with installing and configuring this integrated scripting environment please check my earlier lessons in this series today I will go over creating functions in Powershell Scripts I'm using Powershell version number seven but however what we are covering today is still applicable for previous versions of Powershell first of all you need to know what are functions in scripts or software code functions allow us to reuse code functions are basically blocks of organized code that can be called in elsewhere in your script in Powershell just like many other scripting and programming languages you can create your own functions in addition to the functions that already comes with the program for example let's look at a simple calculation right so we have 10 plus 5 and I need to add these two together if I run this code it should return 15 and it did what if we have to repeat such calculation over and over in that situation you can create a function and within it you can include this 10 plus 5 calculation in Powershell it is best to use verb noun naming convention when you are creating a function so if I'm going to create a function that will add 10 to 5 I'm going to create a function using the Powershell verb noun naming convention you may recall from my previous tutorials the verb noun naming convention is a convention that we often use in Powershell so that it is consistent across the board so how can I create a function here right so I'm going to clear the screen below the terminal to create make this into a function what I need to do I'm going to press enter here I'm going to include this calculation within my new function so I'm going to start that by typing function so when you type halfway through remember because we are in a script indicator scripting environment it already have that option right here so we can click on that that automatically put this entire thing in here and then you can start adding information within it but before we do that I'm just going to show you a simple way of doing it like if you don't have an integrated environment if I type function and then if I go add Dash numbers why is that Dash numbers because that's what I decided my function name going to be and we're going to do this curly bracket thingy we can press enter and now I can type anything in between this first curly bracket opening and the last curly bracket right here the second query bracket up down here the closing whatever I need to be included in this function so what I'm going to do I'm gonna delete this last line of code I'm going to press tab and plus 10 plus apply so now I have a simple function it doesn't have many information in it it doesn't have that original uh you know it doesn't have the param or anything in here instead I have a simple function so if you are starting out writing functions in Windows Powershell this is how you start out if I run this piece of code would you expect it to return 15 let's see if I run this code what does it return absolutely nothing so few seconds ago a few minutes ago I showed you 10 plus 5 when I run this code it returns 15 but now I have that 10 plus 5 inside a function it return nothing so why is that the reason for that is because when you have a set of code a block of code within a function it does not going to get printed out or it doesn't not get used until you call in that function so I had to go add Dash numbers which is the function that we created here in order for that to call in so now if I run this part now this code now it should return 15. there you go as expected so when you create a function in other words a block of code you can just use that unless you actually call in that function so you need to call in that add numbers function we created up here in most situation we don't just create a function with static hard-coded block of code in this case that's what I did this is a hard coded static numbers right so you don't ever change this every single time you call in add numbers it's just going to keep printing 15. for example if I could add numbers couple of times if I clear the screen and if I run this it's going to repeat 15 15 15 15 because I'm recalling or calling in this block of code in this case is just 10 plus 5 1 2 3 4 times any print one two three four times so in other words this is what we call a static function it's basically have a Statics code that keep getting repeated now what if I want a dynamic one so in most situation like I said we don't use these cyber static uh hard-coded block of code instead of we use what we call a dynamic or one that can do passing of parameters parameters allow us to interact with the code within a function and that allow us to create more customized output out of this function so let's create a dynamic or a function that can take parameters so let's uh start with function again and again this is going to be a simple function but let me uh let me show you uh you know how to properly write a function using this uh you know um option so I just basically call you know within this integrated environment calling this function and we have a function name you can put so I'm going to call this let's go to add values and so just to make this function different from this function instead of add numbers I'm doing at num parallel add values and in order to pass in parameters you start with param and then Open Bracket and close brackets because this is where all your uh you know parameter is going to go in so this is what you typically use that's why in this integrated environment when you try to enter a fun create a function and if you go with this first option it automatically add that param path right part this part even though when you're first starting out you can create a simple function where there are no parameters and with a study block of code so to in order for you to create a function that actually take in parameters you need to have this param within that function code and in here we can give parameters so let's say um I want a parameter of integer I'm just casting into an integer and I'm going to give a parameter number one and I'm gonna get another parameter another integer and I want to have number two I'm very creative with my names right so you know you can you can have anything you like you can you can have creative names and stuff like that um and this is basically a block of code now that has two numbers in it sorry two parameters in here now I need to do something with these parameters so it took the you can take the parameters but what it's going to do is basically nothing with those parameters so what I'm going to do I'm going to press enter after the parameter this part and I'm gonna say I want to add them just like before I'm going to add number one two number two because by doing uh this what we are doing is taking these two numbers and adding them together so now if I call this function in so add values because that's my function name right here what will you expect to happen well you're probably gonna expect to what you're gonna expect to happen is that to ask this function for these two numbers right so let's see what happened so if I run this block of code this part nothing it just basically returns zero so why do you need to ask for these two numbers the reason for that is when you write a function like this in Windows Powershell 7 it automatically set these two variables into zero so right now the variable num1 and variable num2 are set to 0 by automatically by the windows Powershell seven because we didn't specify what these two supposed to be it basically adds 0 to 0 and returns zero so again I'm going to show you again if I run this part of the code this block it's going to return the value of 0. so how can I pass variable values to here to if you just want to hard code this in the code of your power Powershell script you can simply pass it by for example putting 10 space 5 that will give the variable 1 10 and variable 2. 5. unlike in other programming and scripting languages in Powershell you don't need to put a comma you don't put a comma you don't put brackets or anything like that if you have two variables you simply put a space the space basically says the first value goes into the first variable the second value goes into the second variable so that's something to keep in mind now if I clear the screen now if I run this block of code it should return 15. because number one is you know variable one is associated with 10 the variable 2 which is number two is num2 is associated with 5 and then we are telling them to add here and it returns 15. so keep that in mind if you do not give variable values the windows Powershell will automatically set your variable to 0 and it will return 0 as a result so you need to specify your values next thing you may be asking is can I pass these values explicitly in other words this is right now implicit so because 10 is here and the 5 is here 10 automatically gets in here and the file gets in here right that's called implicit but what if I want to do explicit you can do that by calling in the function which is ADD values and then we're gonna go Dash and gonna go numb and we have num1 I mean so for num1 number one I can say 10 and space again a dash like a you know like a line which is on your keyboard right next to your plus equal um key and then you go num two and then you put five so this is what we call explicitly defining variables so if I run this block of code right now issue return 15 still because number one is 10 and number two is five you can um you know switch them around and you can put anything you like for example you don't need to put five you can put you know 20 for example and it will still return um you know the addition because we have an addition right here in this case in uh this situation it will should return 30. uh because reason for that is number one is now set to 10 number num two is set to 20. so this is called explicit uh you know passing of values you can also just uh set the only one value so let's say we want to put um you know number two to just five but not exp Define your number one if I run this piece of code what it will return is just fine because number one remember will be automatically set to 0 equal to equal to zero by Windows Powershell because it it was not defined in here so because this is defined as number two is defined as five we can simply print that out as five so in other words you can pass values into your function either explicitly by using this Dash and uh you know defining explicitly what variable that you want to pass and what value or you can do it implicitly by doing just 10 and 5. for example if I just put 10 here without any um you know explicit definition what that's going to do is just going to pass that 10 to the first variable so that's what that is going to do in here it's just going to pass that value just to into the first variable but if you want to pass it just into a second variable you can do that like I said put a dash in there and put the variable name which is dollar sign sorry not a variable name just don't put a dollar sign just put num in um because the dash knows it is the variable and then you can put 10 in here and that will pass that 10 into the numera the variable in the second place right here num2 so again it's very simple to pass variables you can do it either implicit or explicit if you do a implicit it's going to be in the order in which the variable appear here if it is explicit it will be the explicitly what you are defining will be cast into or pass that value into that variable also take this output from this function in other words a result from this function and save it into a variable for example if I go and create a variable called my results here free salt equal so remember in Windows Powershell uh the dollar sign would create variables just like variables in the function up here you can I'm going to move this a little bit higher up so just like where the variables in inside the function here I just create a variable outside the function but I'm gonna equate that to whatever the values getting out of this so let's go back to our implicit and I will just put 10 space 5 so num 1 is 10 num2 is 5. and if I run this block of code it will return nothing because reason for that is I haven't called in the variable that I have created here even though I did call in the function because functions output is now saved in this variable so if I want to get the output of this what I need to do is to Simply call in this variable so you can store the results of this function into a variable and then call in that variable later in your script so this is a handy little thing that I use sometimes when I write powershells I don't always call in the function and use it right away I store it into a variable and then use it elsewhere in fact you can then take this variable and then can do other things with it for example I have 15 here right but if I want to divide this by something so I can do that like instead of recalling in a result just 15 I can take my result and I can do more things with it like I can do more math with it for example if I if I take the results uh no I just take the results and then my let's say multiply by 5 for example so if I clear this uh let's do multiply by two it's easy so let's um just to be different because it's ten to five that's not going to be confusing so if I run this now this block of code oops clear run twice I'll run one time so if I run that block of code so it gives me the result 10 plus 5 is 15 and if you multiply 15 by 2 30. so by storing your result your output of a function into a variable you can then take that information and do more things with it such as mathematical calculations in this case the result of this function is 15 but then I store it in my result variable and then multiply by that 15 by 2 as a output of that is 30 and that's what we get printed up down here so that's an advantage of storing something in the output of a function into a variable we can also use the result and do other things with it using the same variables contained within this function for example so I have my result and if I go plus equal and call in the function again and this time I'm going to do 10 uh no let's do one and two and now I print my result if I run this part this part of the code what it gonna result in the value of 18 the reason for that is it took the function gave these two variables 10 and 5. they add together in here and it resulted in 15 and then again ran the same function call in this function and gave these two variables 1 and 2 which is then added on top of whatever the results that ran in the line 10 so into 10 and 5 we see is what 15 and then took the 15 and then again add 1 and 2 to this to assign one and two to these two variables and it is addition here and is result in 18 for 15 16 17 18. so 15 is the output here and the output here is 3 15 plus 3 is 18. there you go it's simple math so it should be easy for you to understand so I'm not going to go over anymore but I just want to show you that you can take the same block of code and keep repeating that block of code and keep changing these numer these variables so that you can reuse uh the this block of code for other things so now I'm going to clear the screen next thing I'm going to talk to you about is how we can improve this block of code within this function in other programming languages and scripting languages such as Java we have this key term called return in Windows Powershell return is still a keyword even though it is not necessarily for you to use it for example in here it clearly returned the addition of these two values even without the return keyword but as a good practice because in other programming languages you use that return keyword and it is still a valid keyword in Windows Powershell 7 and previous versions I recommend you that you use the return keyword in your functions so only thing you need to do here is for this calculation I'm going to enter return and as you can see it turns into this the same color as param because it is a valid keyword it didn't change this function it didn't change what this function is doing so if I delete uh this part because I don't need that and if I just simply adding value 10 and 5 and it's exactly doing that it's written 15. because I add value 10 and 5 to the these two variables and I store it in my results um you know variable and I printed any Sprinter 15. so this return keyword didn't do anything to basically improve your functions functionality but rather it make it easier for you to have build that muscle memory because this is a key term that is used in other programming languages and scripting environments so I recommend that you use that create term in here as well now what if I want to print these two numbers instead of actually printing the value can you do that yeah you can so instead of return you can also do fright Dash output what that will do it will not return 15. instead it will return 5 sorry 10 plus 5. so if I run this part of the code it will simply return 10 plus 5. so what right our right output did it just basically take the whatever in here put it into these variables and print it as exactly what you see right here you just took the 10 put it into num1 it took the five put it into num2 and just printed 10 plus 5. it didn't do the calculation so in functions like this you can do a lot of things like this like for example I can you know print it that and also I can return it again you don't need the return keyword pre-term here it'll still do the same calculation but I'm just doing it to just show you if I run this part of the code now it'll not only give you is 10 plus 5 how it arrived at 15 but it also give you the 15 because I'm asking the return right here so I'm just giving you a few more things that you can do with a function like this again I recommend that that you use this key term the return key term whenever you are writing a function uh because it'll improve your muscle memory for not only the windows Powershell but also when you are using other scripting and programming languages again if you want to write this uh values out onto the uh you know terminal or to your code again you can do write out uh right output and then put these two values these two variables and it will simply print like I said it'll simply uh you know print um the the variable values stored in these two uh India right so right output will do that so it needs to come actually at the pop before the return so keep that in mind so those are few options that you have available uh in you know in functions I there are a lot more you can do with function but I'm just giving you some you know key points in this lecture and that's everything for today I just want to keep this thing short and simple as possible for those who are new to Windows Powershell 7. please make sure to thumbs up this video And subscribe to my channel until your next Powershell lesson thank you so much and have a nice day [Music] [Applause] [Music] [Applause]
Info
Channel: NetITGeeks
Views: 298
Rating: undefined out of 5
Keywords: Windows 11, Windows 10, Windows PowerShell, PowerShell 7, PowerShell functions, custom functions, variables, scripting, coding, Computer Science, sysadmin, netadmin, IT Analyst, Microsoft Windows, Windows Server, ISE, scripting environment, PS, VS Code, Visual Studio Code, PS parameters, PowerShell parameters
Id: YvJLpfcLvxw
Channel Id: undefined
Length: 26min 10sec (1570 seconds)
Published: Fri Sep 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.