PowerShell Functions begin with the basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to check out Powershell functions Powershell functions are the Lego bricks of our Powershell code they allow us to build repeatable actions that can be reused many many many times functions are not just a basis of Powershell but they are at the basis of most object-orientated programming languages out there and they are essential for us to kind of get our heads around and actually learn before we start to write proper large scripts within Powershell so let's get straight on with the demo for Powershell functions foreign [Music] so we're going to look now at functions within Powershell functions allow us to pre-package things inside our code into kind of like little boxes that can be called upon later on within the code so instead of having to repeat specific actions we could create an action inside a function and continue to call that function throughout the code functions are in most programming languages we find out there and we can see them now in Powershell so to create a function in Powershell all you have to do is use the word function very easy we could name the function something like this and get greeting we could name it anything we wanted to it doesn't have to be in this format of noun ver or verb noun format we could actually name this apple we can name this Bananarama Apple it doesn't particularly matter but after we've named our function our function code is stored between these two cardi brackets I have currently got a greeting variable declared inside here called hello world and I have a write host for dollar greeting this variable is actually scoped inside this function so I can't call this variable or access it outside of the context of this this box this this functional box if I run this now within Powershell you'll notice nothing actually happens nothing happens until I call the function for use the function was called get Dash greeting so if I do get Dash greeting here in the terminal it will run that function and returns hello world I could also put that directly at the end of the code here if I wanted to get Dash greeting and I could go and run that same code and if you'd see the output there as hello world we can expand on this concept greatly as well we can also add into our functions things like parameters because functions are a little bit like commands so if I take this param block here and add a couple of brackets to it I can now start declaring some things inside here so if I go for example string oops string and we'll name this string dollar name if we want to or even better idea we'll name this string dollar greeting okay great so here what we can do is we can also change this right host around so here we can also see we're getting this greeting this greeting is currently setting itself to hello world we can remove that and now we can actually pass this as a parameter so if for example I remove this code here I now re-run this can now run get Dash greeting I put a parameter on the end with a dash and that parameter is going to be called greeting so if I go and add in hello that code is now going to run and I'm importing information inside a parameter so let's take that a little bit more in depth let's create another function down here and we're going to paste in an existing function that I have and it's called get square this is to go and get some squared numbers so here we have a parameter it's a different parameter it's an integer not a string and the integer is going to be number the result is going to be number multiplied by number to square a number so let's clear this up a little bit and let's go and rerun this code now if I run this code start debugging what's actually going to happen is it's going to load both of these functions into memory for me to play with so I could run get Dash greeting with my greeting variable on there whoops or I could run get Dash Square and give this a number say for example number three and the square of 3 is 9. what happens if I don't give that a variable though if I delete this and press enter I get zero because there's a lot of things you can do with parameters and we'll take a look at those later on like for example making sure that parameters are declared as Def and you are pre-populating those parameters with information or you can make parameters required or optional so you can make a commandlet or make a function in this sense with a variable so with a parameter you could make a function in this sense with a parameter that you have to fill in or the function is not actually going to work so this is a very basic example of Powershell functions and we will reuse this concept A Lot in the coming series and you know the routine hashtag like And subscribe and I hope you enjoyed this video and will join me next time goodbye
Info
Channel: Mike in the Cloud
Views: 8,716
Rating: undefined out of 5
Keywords:
Id: z-6O-qsu2ho
Channel Id: undefined
Length: 6min 4sec (364 seconds)
Published: Mon Mar 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.