The Art of Handling PowerShell Variables: Exploring Strings, Integers, Hashes, and Arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so last time we checked out the introduction to Powershell now we're going to take a look at Powershell variables if you didn't see the last video please go back click on it hashtag like And subscribe you know the routine but today it's variables we're going to take a look at things like strings and integers and hash tables and how Powershell handles variables before we get on to actually writing our scripts so without further Ado on with the demonstration foreign [Music] so we're going to move into the very first thing we need to know about Powershell and that's variables and how they work so variables within Powershell let's come over to our vs code and let's create a new file here variables dot PS1 and within here we're going to create some code now a Powershell variable is very very simple anything that I declare with a dollar in front of it can be a variable so if we take something like dollar message is equal to hello world that is now a variable and I can recall that variable within my code so if we take the example from before like for example write Dash host and we had hello world instead of using hello world like this what I could do is take that away and just do dollar message and this will call that variable for right host if I go to new terminal here and I run this code and I run start debugging you'll notice by output as well it's just hello world here wonderful let's get rid of some of these shells keep everything neat and tidy and that's clear that shell Dam so different types of variables that we've actually got within Powershell so this is just a standard variable example first of all we can do strings now what we have here is something known as a comment notice this has gone green anything that starts with a hash is going to be a comment this is something that the program is not going to process and that is just used for me to remember what it is I'm actually doing so a string type variable is anything that includes numbers and letters and special symbols inside it so if we do dollar string is going to be in fact well not the dollar screen we'll do John dollar name is equal to uh John Doe okay that's a standard string we can also store other types of information as well we could store something like integers or ins so if we use something like dollar age is equal 30 on there now notice something interesting here that I've done this is contained inside some double quotes this is not contained inside anything if I go and run this code now I'm going to start debugging it's going to print out hello world for me but it's also going to load these two variables into memory and I can call these variables inside the shell so if I do dollar name you can see it outputs John Doe if I do dollar age on the other hand it actually outputs 30. now Powershell is smart enough in most cases to figure out the data types some programming languages require us to actually declare or tell the programming language what data type is actually in there with Powershell we can have issues with this later on and we'll cover that when we look at actually declaring our variable types so we can also do other things like for example floats so a float in a simple explanation is something like price here and could be 19.99 so a float itself is anything with a decimal point on there we can also go and include things like arrays if you want to create an array in Powershell an array is a collection of things inside one variable so in this case we might do something like dollar colors and we include equals and we want to do an add symbol and some brackets and we can go red comma green oops green comma blue there is a variable that color is now going to contain red green and blue inside it and we could reference different things inside there so if I go and run this command again let's go and have a look at what dollar price looks like so if I run dollar price that's going to return me my 1999. if I run dollar colors on the other hand that's actually going to return me red green or blue this array has positions that are numbered so red for example is position zero green is position one and blue is position two the we can reference a specific item inside an array very quickly by doing this just pop some square brackets on the end and you can see that if I'm referencing 0 within those square brackets I'm pulling back red if I reference one I'm pulling back green and if I'm going for two I'm pulling back blue I could also reference multiple so I could do 0 comma two pull back position 0 and position two just red and blue I can actually do things in between if you do two dots like this it will pull back everything between zero and between two very nice there is another element in here that's a little more complex there's something that's actually known as a hash table so I'm going to paste one of these inside here now and this is what a hash table actually looks like so a hash table it's a bit similar to an array in the fact that it's got multiple things inside here but notice we've got knock Square not normal brackets or parentheses but we've actually got curly brackets on here and we have inside our hash table what's known as a key value pair so name is equal Jane Doe age is equal 25 and occupation is equal teacher so if I highlight the hash table now right click and click run selection just to run that hash table if I reference that with dollar person and whack enter you'll notice it returns this in a nice little table there's a name and a value we have a key value pair going on here okay so we can also retrieve individual things so we could do person.name and that's just Jane Doe and person dot age it's going to be 25. so hash tables are a little bit like a tiny mini database inside here as well we can also run other interesting things in regards to variables where we can store the results of commands inside variables as well so if we take this other example here and I'm going to paste another example in for us this one directory equals get child item and write out right host is dollar directory dot count now inside here inside Powershell code I'm just going to go see the dot dot here to drop back a directory into my documents folder and inside this document folder if I run dir I can see that I actually have this single folder called Powershell code now you may be used to this command dir in reality in Powershell that doesn't exist in reality that command of dir is actually aliased or linked to this command underneath get Dash child item so if I run get Dash child item that is the actual command to go and list directories out and this produces a number of different results there's a lot more data behind this this is just its default output but what you can do is you can take in this case dollar directory is equal to get Dash child item if I run that selection it's taking the output of that command and inserting it into a variable so if I now call Dollar directory and run it you'll notice that the output of dollar directory is the original output from get Dash child item it doesn't rerun that command again it's just storing that output every time I call the variable what I can also do is I can subselect things so if I do directory Dot and then do count and press enter it's going to count the amount of directories that are there there's one if we go for example directory dot name and run this we can see that the name of the directory is Powershell code we could also run something like a last write time and we can go and run that now if I want to actually find out all the lovely little things I can run against this dollar directory I want a special command I want to do dollar directory that variable and I want to pipe this to something called get member now we will go over the pipe command in a future video in detail but for the moment just remember that if you pipe a variable to a get member like this you'll be able to see the results here of all the lovely little things that we could do to it we have methods which are things that we can actually engage so we've got things like create and delete and get files and get type those are all the things that we can do with that get child item we've also got here what I really want to look at these properties okay so these are the things I could call back so I could call back creation time last access time which we did name parent root Unix file mode that's an interesting one each of these things has documentation behind it and there is a lot of documentation for Powershell but for the moment that completes our very basic look at creating some variables within Powershell and how they actually function I hope you'll join me for the next video as we learn more about Powershell 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: 1,382
Rating: undefined out of 5
Keywords:
Id: BQfs5hHJdxk
Channel Id: undefined
Length: 11min 0sec (660 seconds)
Published: Fri Feb 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.