PowerShell 7 Tutorials for Beginners #2 : Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to the second video in our powershell 7.2 for beginners series in the last video we kind of went over some basic fundamentals we found out what a command was we installed visual studio code we installed powershell 7.2.6 so now what we're actually going to be doing is we're actually going to be getting into uh some coding we're not necessarily going to be scripting yet um but we are going to start learning the basics of how to write our scripts uh starting in this video so what we're going to first want to do here is we're going to want to open up visual studio code and then all we want to do is click on file at the top left here and then click open folder and then we're going to go to the folder that you want to store this this file in i'm going to be putting that in c and then i have a folder called scripts and then i have one just for this video uh two variables and then in here i'm just going to hit select folder and then in here everything's going to open up it might take a little while and then here we're going to be creating a new file at the left kind of at the top here top left we're gonna hit new file and here we're just gonna call this um variables.ps1 since we're learning about variables today and now it will take a little moment for visual studio code to actually kick on the actual power shell like engine or core uh you can say so now if we actually do our set dash we're gonna see some command let's get dash we see some commandlets as well so we know visual studio code has loaded in powershell so we are good to go um so the first thing with variables um they work the same way in powershell as pretty much every other uh programming languages the only thing that kind of is different is how we declare them and a little bit how they work so in powershell and any programming language is really variables are pretty much exactly what you remember from high school middle school or post-secondary math class where you had algebra you had to find the value of x or they give you a value of x and a value of y and you have to do some sort of calculation where x or y had a stored value so variables are exactly in that same way in programming and in powershell you can store them to values the neat thing about programming languages or powershell is they don't have to just be equal to numbers uh we're going to see that we could store uh names in there which are actually called strings so strings are series of text or a series of characters that form a word a sentence anything really wrapped in a double quote we're gonna see is called a string and then we could store uh number bit values which these could be integers or they could be doubles we're going to see what the difference is as well and then we also have other variable types like booleans uh date time uh and so on there's a bunch of different variable types we're going to be covering the basic variable types today and in the next couple videos we're actually going to be looking at a little bit more um in-depth uh variables that take a little bit longer to cover just because they aren't necessarily something that you would see out outside of programming so those are arrays hash tables and some custom objects so let's actually go ahead and let's get started and see how we could use variables in powershell so uh first i'm going to explain the notation that i use so i use camel casing so that would be a lowercase for the first character and then any words that come after will actually start with a capital so camel casing will be camel is the first word so that would be lowercase and then casting or casing i should say the casing would start with a capital c another example would be if i had my value the my would be lowercase and then the value would be uppercase for the v so that is camel casing but to declare a variable in powershell you first start off with the dollar sign that signifies that this is a variable and we are going to say we're going to name this my variable and let's first assign it to a name so let's assign that to jacked programmer here and what we can actually say here so we can run this line of code so we can run it like this or we can run it like this as we've seen in the last video both will work and then we can actually display it to the output by just actually putting mod variable with the dollar sign in front of it with it doesn't equal anything we are just putting it here we will see that down here it actually displayed jack programmer and if we actually just run it completely we will see that we actually get it again here so that is perfectly awesome also if you want to clear this console here because it's maybe getting a little cluttered uh you can type clear or you can type cls and that will just clear it and then we could just run it again just so you guys can see it say brush run we get checked programmer now we know or at least i know um and i'm about to tell you guys that this is what we call a string so anything between double quotes is a string uh we can even put the number 12 here this is actually still going to be considered a string because it is in between two double quotes now a string in powershell could be double quotes or it could be uh single quotes as well as long as they both match on either end that will be fine we will see that we get the number 12. now how do i know that this number 12 is actually a string if we do the my variable and then we do a dot notation here we actually see that we get a lot of different things that pop up now these are what's called methods or properties uh depending on what you pick the length is a property and that will actually be notated with a little wrench here i don't know if you can actually see that clearly in the video but the wrench are going to be properties and then the little cubes here are actually methods and methods are what you can run on a specific value so there is actually a method called get type and that will actually automatically put a opening parenthesis we're just going to close that parentheses right away and we're just going to run this dollar sign myvariable.gettype with the open and closing parentheses and when we do that we actually get an output here that it is a base type of system object so it is an object and it is a string so if we actually change this to just 12 here without the double quotes and we actually run these we actually get now that we get a value type and we get into 32 and this means integer and it is a 32-bit integer now if we added a dot 1 here we will actually get a different variable type of once again we still get a value type but it is now a double so basically an integer is a round number uh so one through i believe it is around like 2 billion is the max value for an integer i could be wrong on that as well but it is a whole number so 1 2 3 4 5 6. you can also have negative values as well so negative 1 negative 2 those are all 32-bit integers in powershell a double is when you have a dot or a decimal point in there now you can have as many decimal points as you want in here and this will still be a double now doubles would be more what you would use uh like financial type applications um or anything like that uh if you're dealing with integers uh you might be doing some basic comparisons maybe some little bit of math here and there but the majority of times your num numerical values will probably be doubles um so that is pretty much it for integers and doubles and we know how to find out the variable type as well now very similarly to uh real life we can use these variables in math functions as long as they are stored in integers so we can actually do that here so let's actually just erase what we have here and let's create a couple of variables here so let's do a my value and we're going to do a my value one equals one we're going to do another my value five equals five and then a my value 10 equals 10. perfect so we have three variables here and we have all of them are set to integers currently and what we could do is we could do a my value 1 and we could do a plus dollar sign my value 10 and if we actually run all of this here we actually get the output of 11 because we are adding 1 to 10. so that is exactly what you would expect you would expect to get one uh you would expect to get 11 back so that is awesome but also what you can do is you can actually store that result in a variable and we're going to call that variable my result so if we actually run this here we will see that we actually do not get an output anymore is because instead of being displayed to the screen we stored that output to the my result so if we actually just do a my result here and we just look at what is inside of this value we see that we get 11. so that is perfect now what happens if we try to [Music] get a value that has not been set yet so let's say if we do a my result 2 and we try to get this we actually just get an empty empty value and that is because this is actually equal to null we're actually coming back to that probably a little bit later on in this video but just be aware that if you don't set anything to your value you try to look at it you won't get an error but you won't get anything back so that is just something to keep in mind now the other um the other arithmetic or mathematical operations that you can do of course is the minus so if we do um 1 minus 10 we will see we get minus 9 if we do 1 at times 10 we will see that we get 10 and if we do 1 divided by 10 we will see that we get 0.1 so it actually does convert it automatically to a double here and then there is also one last arithmetic value or mathematical operation that we can do that a lot of people don't necessarily remember or know about but we've already covered the four basic ones the last one is this percent sign and what this is it is called a modulus or modulo or mod operator and what this is is it is the leftover function so back in probably elementary school you were taught how to do long division and always was long division you had a whole value and then you had a remainder this modulo operator will actually give you the remainder so if we do a 1 divided by 10 1 doesn't divide by 10. so it should actually give us the value of one and we do get one so that is perfect now what happens if we actually did uh let's create another variable here let's do my value and we're going to do my value 3 equals 3 and we're going to do my value and modulo my value 3 so we are doing 10 divided by 3 and we're getting the remainder of that so 10 3 goes into 10 three times and we have a remainder of one so let's see what happens here so we do have a remainder of one now what if we did a modulo 2 so i'm actually not going to create a variable we're just going to put 2 directly in the operation here which is completely doable we actually get 0. now this could be something that you use uh to actually detect if something is even the modulo operator is how you would do that you would always do a modulo two if it is equal to zero you know the number is even if it is not equal to zero then you know that the number is not even so that's just something that you could do uh with the operators and you can kind of do that for some conditional statements that we will actually learn later on now what we can also do with the values is we can have boolean data types as i mentioned now the boolean data types are simply true and false so if we actually go and set here my statement is equal to true and let's just go ahead and let's do the my statement dot get type open and close parentheses here we just run these last two we get a value type and we get boolean now as you can also tell not sure how clear it will be in the video but the my statement is a lighter blue than the dollar sign true that's because the dollar sign true is a preset value in powershell if we do a dollar sign false we get the exact same thing and this is because this is already predefined you cannot use the dollar sign true or dollar sign false as variables um if we do a dollar sign true equals one we will see that we actually get a value because the uh we get an error sorry because the variable true cannot be assigned since it is a read-only automatic variable that is built into powershell please use a different name so if you try to assign something that's already a default value in powershell you will get an error message saying that you cannot do that uh and this goes for true false and what we're about to see uh which is actually null so we actually have these boolean values now you can actually get boolean values out of conditional statements or you can also kind of remind yourself of like back in math days where you had operations of greater than or equal then um or lesser than um you would have a boolean value that comes back out of that so if we actually do here parentheses and we do my value is equal to my value 10 and we're going to run this here we actually get false and if we did a get type on this here we will actually get that it is a boolean as well um now you don't have to actually know what the dash eq here means but it does mean just equal we will be learning that a little bit later on uh that's in a different video here that's a little bit exceeding um but in a lot of languages the equal sign uh to check equality would usually be a double equal sign or a triple equal sign that actually does not work in powershell so if we actually run this here uh we will get an error saying that the term uh equal is not recognized that's part of a commandlet so you just have to know um that to check equality it is dash eq but we will have a whole video on um these conditional statements as long as you know that they exist so as we saw a little bit earlier if we have something like my new result and we run this we get the we get nothing back because we haven't assigned this value to anything yet it does not exist now in a lot of programming languages if you try to reference a variable that hasn't been uh declared yet you would get an error now this is not the default functionality in powershell but if you do want that default functionality you can actually still receive that functionality if we do a let me just do it at the top here because this is what you you would typically set it up at the top so if we do a set strict mode dash version latest and we just run this manlet here and we try to do my new result we will actually get an error the variable my new result cannot be retrieved because it has not been set so this actually makes it as a strictly typed programming language or at least making it to where you need to declare your variables and assign your variables before you can reference them it will not let you just reference a variable that has not been declared yet and just automatically assign it to null this will give it a more of a default programming language feel but by all means if you do not want this by default it does not come turn on and if you do turn it on you can always turn it off by set strict mode off and then we can run this line no problem we see that it equals to nothing which actually is a null value now if you need to use nulls for example in your code similar to the true false we can also do a dollar sign null we see that the color changes so we can actually compare the my new result is equal to null here we're just going to wrap this in parenthesis and we are going to run this and we see that it is equal to true imagine because my new result hasn't been set to anything so it is actually equal to null now if you had the strict mode set to on here so if we do a version latest and we try to check for null you will actually get an error once again because this value cannot be referenced because it is null so always be sure if you're trying if you are using strict mode and you're getting a lot of issues um it might be easier to write your code without strict mode on but if you do have the need to make sure that your code the variables are always set nothing is ever null the strict mode could be very very handy for you so do know that it is an option for you it's something that i did not know for the first couple years of me using powershell it is something that i learned in the last couple years so we know what the dollar sign null we know about the strict mode we know about the different operators we know about booleans now as well now the only thing left which i didn't actually cover in the first variables video is we can actually type our variables now what does that actually mean it means that if i give a value so we're going to come back to the top here with our integers if i did a value of 1.2 here for the about my value 1 we just look at the my value 1 here and we set it to 1.2 we get 1.2 back and if we do a my value dot get type we actually get a double which that is perfectly fine because it is a 1.2 it's got a decimal it is a double but now let's see if i only wanted integers to be stored in this variable of my value if i put a pair of square brackets in front of it and put int inside of that and we run this code just to get the output we actually see that we only get one now and the type is now int 32 now if we do the same thing for the my value 3 but this time we're going to set it to a double and it's only two three here just gonna copy these two couple lines here so if we cast it to a double here we actually see that we still get three it doesn't add a point or anything but it is actually stored as a double uh now you might want to be a little careful these doubles do take up a little bit more room than integers in your code uh so if you are casting something to a double make sure it actually needs to be a double or instead you could always just leave it to the defaults and this way you are letting the system kind of figure it out and assign the types according to the variables but you can actually assign a type to the variable itself now one last thing that we're going to be looking at is we've seen commandlets and they output to the console as we've seen so if we do a get date here and we run this we get the date for today and the current time and it's always outputted to the console now with variables we can actually save this data to a variable so if we create a variable here called today and we store that and we make it equal to getdate we don't get the data anymore in the output but if we actually look at today now we actually get today's date so um do know that you can store the output of commandlets into variables and then do some processing on this data all these things we will be doing much later on in the course this course this video is really just to understand how variables work the different aspects of variables the different data types that you can do use in powershell in the next video we're going to be looking at arrays and then we're going to be looking at hash tables and custom objects because those are going to be very very useful uh later on once we get into some more complicated uh scripts and some of the projects that are already on the channel a lot of them use hash tables and custom objects and arrays so you're definitely going to want to know how to use those so that is pretty much it for the variables video if you guys have any questions or comments please let me know down below in the comment section if it's something that i think can benefit a lot of people i will make a video if it's something a little bit more specific i will answer you guys directly always be sure to hit that subscribe button hit that like button as well and be sure to hit that notification bell to be notified when that next video comes out and i will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 19,258
Rating: undefined out of 5
Keywords: powershell 7, windows powershell, configure vs code for powershell, visual studio code powershell, programming, coding, scripting, how to install powershell 7 on windows 11, powershell, powershell for beginners, how to start with powershell, scripts, powershell fundamentals, powershell 7.2, powershell 7 windows 11, windows 11, how to, beginners, variables, powershell variables, powershell basics, powershell tutorial, powershell scripting
Id: I-UIXcXhvcY
Channel Id: undefined
Length: 25min 53sec (1553 seconds)
Published: Mon Sep 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.