PowerShell Basics: (Part 1) Getting Started with PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and welcome to this episode of taste of première guess what season it is hey guys and welcome to the show we've got a pretty cool one today I've got Stephen Matthews with us and Stephen is a DSE in fact he's mydsc he works with me on NASA and FMC and he's he's a friggin PowerShell genius and it's our pleasure to have him Stephen how you doing buddy I'm doing great thanks for that intro yeah pretty high yeah I mean every word of it though I mean I've seen you do some pretty cool stuff with PowerShell and I'm just amazed by it and III think PowerShell is probably one of the coolest most powerful tools that we have in our tools Arsenal and so I'm excited about today's show yes definitely my favorites all yep absolutely okay so talk to us a little bit about what you're gonna show us today all right what I want to show you is just the basics of finding power show where it is and you know going through how to find the commands from the old command prompt where they're at in PowerShell and then exploring commandlets and finding more commands to start using to really just expand your inventory on the commands and how to go about finding more of them and then we'll talk a little bit about difference between PowerShell version two and three and upgrade and the reason upgrading is important awesome all right well let's kick it off let's get started alright so people ask me how I got started with PowerShell and really you start with a conscious decision on my part a few years ago to stop using the GUI and to just start using PowerShell and see how far I could get and it took a lot of time a lot of investment you know it took a long time to learn a lot searching for how to build commands and how to pipe them together and do that but what I found was as I learned more the skills I learned you know four versions ago of PowerShell are still applicable today and so that's how I kind of built upon it and one of the the first way one of the first points when I realized how I got pretty good at it was I was working with a Linux engineer we shared an office in another life and he a user came in to get a account created so I opened up our show and I had the Active Directory module I created him account write PowerShell the user left and my Linux admin he's like hey did you create that account was like you sure did and he goes why he's like how I didn't see you open the GUI and I said I use PowerShell I mean say well I didn't even know windows can do that and so at that point you know I really got excited and felt like you know I can start using this as my as my regular tool and so when I go onto these customer sites what I find is people think that that PowerShell isn't on their systems that it that it's been removed from their corporate build or that you know it just doesn't exist it's not accessible to so I want to tell you that power show started shipping or it was created in 2006 and it shipped with Windows 7 so the odds are that you know it's there and removing power shows bad harness removing the command prompt so we're going to talk about some of the ways to find it Oh usually if I do its to new system I'll open up the search window and I'll type in PowerShell and usually it'll come up here there's ice and some of the other modules I have I have the active module out of the azure module and then you can also do the windows arm for the run box you hit PowerShell there it'll pop up because it's in the system 32 almost over an exit commands in there to get rid of this window and then and then it's also by default it's in your Start menu so if you go to your Start menu and wind programs when there's power show this start menu folder has a space in it I want you to pay attention that here's their different executables you noticed that there's ice and there's pastel and then there's x86 versions as well and then of course the actual pshome this is a variable and this is where it exists in system 32 windows powershell version 1.0 don't worry about that 1.0 it hasn't changed and the five versions i've had it this windows power should go though there is no space in that directory so it can throw people off and again here's your EFC and your ice what I'll do is I'll right-click it and I'll pin it to my taskbar on a new machine so what I have open right now is ice yours may look a little different when you open it it may look something similar to this with the command add-on right and windows flip top and bottom so this is your script pane this is your output pane and then the add-ons will pop up on the right and this is just really you know user preference but what I like to do is I like to set it up with the script pane on the right and get rid of that command window I'll show you how to search for commands without using that yours may look a little different if you have an older version this output window may be split into two where there's a blue window up top which is your output and then like a lighter white window down here which is your prompt and later versions they combined it into this into one and two one version here now the only way you'll know that you're in PowerShell is there's this PS prompt here that prepends where you're at so I want to bring up a command prompt and so this since I'm using ice here I just wanna get I just want to show you some things these are all individual commands on each line and then you'll see me highlight some and I'll be pressing f8 or this little Run button here run selection and that just runs line one at a time and that's how I'm gonna step through this demo so if I bring up command prompt here this is regular command prompt and you'll see that you know I'm in the line I'm in a default directory here and if i type in PowerShell I'm gonna throw a no profile switch on there because my profiles customized it to to clear out anything the only way you're going to know that you're in a PowerShell profile is this PS is lump repented of course if the if you don't see the branding if somebody has a script that starts with command opens PowerShell does a clear screen you won't know that it's running a PowerShell unless you see that PS it's one throw the exit command in there which is the same command that the command prompt uses and so you know X is a nice familiar command from from the command prompt days what that is is that's actually an alias and the aliases for how you're going to how you're going to find out what commandlets to start using to replace your old dos commands so one yeah hey hold on just a second I noticed you're doing something really cool there and that is you're you're clicking on things that are defined in the scripting window and it's actually bringing up items in the command prompt window like you just clicked on get alias and it automatically got all of the aliases and displayed them in the PS window and in the in the you know the command prompt window yeah so these are all in the tool these are all commandlets the blue or commandlets this earthy color here this maroon is strings your red or orange is your environmental variable so that's one of the nice things about ice is that it's color coded like that so you know where you're at and so each one of these lines executes as it as a one line or as they say and I'm crushing it usually I'm pressing the f8 which automatically highlights it throws it over to the output and it runs the commands yeah so if you were to click on get command would we see all the commands yeah yeah you go I'll get there that's awesome yeah all right so now going back to the aliases here this shows you that these are the shortcut the aliases are a shortcut so you don't have to type out the entire commandment so some of the ones should look familiar here dir which is actually get child ID so you're only typing three letters as opposed to that whole command and then it got set which is the old you know how to set variables in the command environment that's actually now the aptly named set variable one I want to point out here in particular is this little one-line script here I found all the aliases that have the that result to remove item and you can see one two three four five six or six aliases that all results this one command so PowerShell has done a lot with their command let's to reduce the number of commands you have to remember so that's how you get that's how I want you to store expanding your command and now if you want to find all the command lists that you have once you get past once you get comfortable with the aliases does that's what this is all about I want you guys as admins to be a comfortable empower show as you are on the command prompt and Dawson bash scripting just this is this is the standard of our command of our command based administration so now get command is going to go through and it's going to go through all the modules that I have on my system and I'll put all the commands so you look here on the right it's kind of cut off these are your source this tells to what directory or what module is loaded from so again it was a sure some DHCP DNS some other some of the other modules that I have loaded on my system and I'm going to go ahead and cut that off because as you can see we're only in the DS it's got a while to go to stop it control see that's your that's your stop or you can press this little red button whilst running the only way and down here I'm left that's where you'll know what the status of your script is and of course if you got the prompt if the prompts not there and it's saying stopping you just gotta wait until it's done so now we want to see you know what modules we have available so now the game module is available it's going to search my system again but it's just looking for the modules and the modules are what holds all the commands say there's a module that you have on another system that you don't have on this system and maybe you're in an environment where you can't download things directly from the internet and you know it's just you have to go through a lot of paperwork to do that you can actually copy modules from one system to another so you go to a month we go to an area a system that has the module you want such as as like this as your module here and let's see here it didn't pop up what I was expecting it to do let me import the module make sure it's loaded so importing a module will tell you how you load all the commandments from a module into your session in later versions of PowerShell it should load those automatically when you run a commands and DeMott it finds the module in there it usually does on the back end but sometimes you have to manually do it so now that I got the module in there now I'm using the up and down arrow that shows that's your history of what you type it's not going to show up anything that you run from the sprint so let's go ahead and run that command again from the script and so what the get module does is it gets all the informations for that module doesn't get the commands but just the properties of that individual module and then I did a select object which would show me all the properties that are available to it and I know I wanted to look for the path so this module path right here is under my windows system32 powershell version 1.0 modules right there so you could copy this the report of that that Asscher and copy that folder to another machine and put it in their modules folder and when you and when you do a git module list available it'll be there and you can import it and that's how easy it is to move modules around machines so do all modules end with PSD one is that the they should and most do however there are some modules that are that are that are just functions that are really advanced functions but they call them modules it could be manifests and and even aliases depending on just how they're built but they're properly built one should in with a PSD one okay so let's just to make things simple a module is essentially a a big box that contains a bunch of PowerShell commands yeah it's centered around a piece of software specific purpose like you have a juror commandlets you have Active Directory command with so all those commandlets for Active Directory they put them up into a they combine them all onto a module and distributed that way think of it as like a zip file flow commands so I install application a and it inst it it automatically puts its PowerShell module in if it has one in the modules directory is that set the way it works it should but some don't some put them and let's see here it's some put them in in the Program Files directory along with the rest of the software it the safest place to put it though is in the powershell 1.0 modules gradually now i'm going to show you here this PS module path directory or i'm sorry can make a variable it's why it's orange this shows you all the default directories where it'll search and this outputs kind of choppy so it looks like it's a bunch of strings put together and i'm going to check to make sure that this is a this object is a string by using a git number so if you run the output of a variable through this get number will tell you how its encoded in this case it is a system string and the system string and has all these methods which are ways you can interact with the string what I'm going to do is only use a split command which uses a particular character as a separate and in this case it's going to be the CEMP semicolon and so these are the default paths where it where the get module is going to look so if a modules anywhere in these directories or nested below it it will find it and you can app that edit this you can add you can add your own path to it as well like this command right here I'm doing a plus equals operator and I'm adding my own directory and I'm throwing the semicolon in there to prepend it to make sure that it formats correctly with the with the rest of it and then I run it again and now there's that c program files and they're just the word of that so cool so now we're going to go on to the I want to show you the commands that are in the module adder so I had an issue when I was trying to convert my trying to upload my PhDs to a sure because you know I was using my iCloud subscription there and I couldn't find the command to convert my PhD X's to VHDs before I uploaded it and I was doing this git command module I sure I couldn't find the convert couldn't find it it's because it's not in this module it's actually in the hyper-v module so and you know then that's some of the things you got a bias but what was in the azure was the ad VHD atom Bhd disk which is the command i needed to actually do the upload not necessarily the divert so i want to show you how to search for commands in a little bit but let's let's talk about the powershell version that we have the easiest way to find out what version of power you have is to run the skin host command and i'll put out the version right there I'm running 55.0 running the the production preview even though the RTM just got released I haven't gotten around to to installing it but so that's just the basic version info there's more version info in a variable conversion table and there's this particular command that command let that will show you what we're going to interact with the variables but I'm not going to tell you what it is let's do a let's do a search and see if we can find it if you do a get help variable usually that second that parameter work in this case variable it will do a search it'll go through all the commandlets and all the modules and pull up all the commands that have that word in there it didn't work in this particular case because variable is actually a is actually a provider so it it or it hasn't so clinician so if get help can't help you you can use the git command now git command here it's going to help pull it I'm not going to run the whole thing because it took a while but I'm going to a measure object and this will tell you how many commandlets that you have to look for to find the one that works with that works with variables this is really cool man yeah so it looks like I don't know how much time I want to give it here but on my particular system because I have more than just the power so I got the azure commandlets I got there our satchels which has Active Directory Commandments and few other things I have about thirty seven hundred Commandments to run to look for to find a commandment that works with variables now I just happen to know that the command I want to use is in the windows powershell utility so if you know what modules in you can actually see here it's stopping and I don't have my prompt that's how you know that you have to wait and I can do a measure object there and cutting it down to that particular module brings it down to about a hundred hundred some Commandments to load before but the best way to search for commandment if you don't have to get help is to use the git command now the git command that's that same command we've been talking about it's got a noun parameter which while at which will search for the second half of the command what name so the command would start with a verb - singular now and that's how little built and there's a list of approved verbs you can one get verb and this will show you the list of the verbs that have been approved by the PowerShell team to use in in the environment that and that is the verbs you'll be using when you start building your own functions and game your own script you want to get into a habit of using those approved verbs and they have different categories of the verbs some are common like Get Set show and then some are more specific like connect which is actually under the Communications Group so if you're trying to join two strings together and you wrote a script that does that you don't want to call it connect because it's got the wrong computation connect would be something where you're testing the connectivity to a remote resource you would want to use the join which is under the common category and so Wyatt why is that why would join be better than connect is it just syntax or it has nothing to do with the syntax it's just it provides a common reference point for all for the entire PowerShell community if I see a command if I see a custom script somebody wrote that starts with connect I can automatically assume that it probably has to do with remote connectivity to another resource as opposed if I see something that says join I know that it's working with probably working with strings to do whatever the commandment does ah makes sense I was talking about the gate command the neck debt with the noun parameter and we're putting the variable word in there so now it's on a search for all the commands with the second half the noun that has the word variable in it and I'm not gonna let that finish but it comes up with five so you know from 3700 to 109 and the module to five if you use this git command with the and it's a get command also has the parameter verbs if you want to find all the git commands or all the set commands you can do it you can use that too all right so if we found the command that we want to do get variable this shows all the variables that are currently defined in this session and these are the names of them now the one we're looking for to find out more information about powershell version is PS version table and you can see that right here PS version two and then you see that it's got this it's got these curly braces curly braces tell you that the information it is a hash thing which means it's got a bunch of it's got a bunch of properties and the properties themselves have values so if we do a if we do a get variable on that name just to pull that one out so get variable name PS version table it pulls out just it returns just that one variable all the variables that are defined but the value is still in this hash table in this hash table format so what we can do is we can look for a parameter inside the get variable to see if there's something that will help with that so if I run a get help you can run I get help and then a get variable or the commandment that you're looking at and it has quite a has a few switches the ones you'll probably use the most is full which will put out all the information description of information all the parameters and all the examples and then you can put a couple of localized parameters such as examples which will just show you the examples of how to use that command line and then the parameter function which is what I put in here and this will show you all the parameters that that this particular command uses so things like exclude include a few other things and then when we go down here we see there's a value only gets only the value of the variable which that sounds exactly so now we've we built this we've built this command laid out we went from get variable to get variable name PS versions table to value only returns animation so that is the proper way to go and find your variables now variables are in power sure to find with the dollar sign and so you can call it directly by just typing dollar sign variable and it's the same information and you can also set your own custom variables using the equals operator right here so I'm gonna set my variable food a bar and then we're gonna get variable on it and there it is foo bar right there and I just defined my own variable now so these are the variables that are in this session now there's environmental variables and the environmental variables are in a different place it's actually in a PS Drive called env so if we do a get PS Drive this will show you all the different PowerShell provider drives that PowerShell knows them and it will and you can use get child item and file system type commandlets to access these systems so there's alias which I showed you the get alias you can do a get child item on the alias instead of just get alias I don't put out the same information in V that will show you all the environmental variables and then functions and look at these hkt see you and hklm and recognize those those are registry entries so you can actually you can actually search the system from powershell command prompt you can search the registry and so let's take a look at the at the environmental variables E&B : and this will put out all the environmental variables of this system and this will put out the same information that's in the set the old set command for boss command or for a command prompt alright so here are all the commands I'm sorry all the environmental variables that are under that e'en VPS drive so these should look very familiar they're all the same ones from the set command from the command prompt you you have your user DNS domain your username your user profile the couple of things home drive home path and just to check let's say we're going to a command /c set so what this will do is if you just type set in powershell it'll automatically search the aliases first and then it's commandlets and then it will finally go back to and look for in executed so if I was just a type set which is the old command prompt for displaying variables it's actually going to default in PowerShell to using this set variable command which is the alias set it's for so we're going to do is we want to throw a command slash C in front of it which tells it to run it as a command from the command prompt and as you can see it puts out the same information well that's really cool hang on just a second so that's kind of neat so I can throw CMD in front of anything and it'll just launch the command prompt command yeah man they slash C and then the command oh that's cool yep and so I'm just going to compare the two real quick this should show me we got 46 in the first one a good shot item a and B and then we have 47 in the command slash C so there's one variable that's different between the two and so you can do that with with all those PS drives so I got a little one-liner here for the get PS Drive what's gonna do is for each one of those PS drive just going to display the name which is gonna be right here and then it's going to get get one item from that and and display so the first one it did the first PS drive here is alias the first alias command is the percent which translates the for each the next the next one did was the file system I believe yep C Drive and the first item in there was my modules directory and then it went in and he went and found a cert and there my assert or my certain Tom's the first environmental variable which is all users profile and HKC you and hklm the first registry entries in there so that's using the get child item four and then you will put in the PS drive colon backslash and your path and you can start getting it you can access the registry commands directly from the command alright so now I want to talk to you now that's awesome yeah I want to talk to you about the diversions between the powershell versions which is so aptly important considering that powershell 5 just wins RTM so I wrote a custom script here which you won't see because it's in my in my own personal directory club compare PS version and I want to compare a version 2 and 3 what it's going through this oh it's going to pop up to Windows run a bunch of commands and then I'm going to point out the information between them all right so on the left here I have a separate power shell window which is running version 2 and on the right here I couldn't get my version 3 to start so this is actually my ice power shell which is version 5 so I would just want to point out the difference here you can tell that this is running version 2 and I specified that with with the start command PowerShell you when you start power show you can throw a couple switches on I showed you the no profile before you can also throw a - version 2 3 4 5 and that will start in that version so this one started with the - version - and you can go in here and you can look at the PS version table and you'll see that your own in the twos the version the stack version and the remoting mine over here is going to have to have have the thighs which I which you saw earlier and then some of the things we're going to look at here I'm not going to run it because the commands don't seem to come back right now but the get module list available all it returns about six hundred and six thousand seven hundred modules and that's not just modules that's like said its modules functions and a few others individual scripts and then the gate command only returns 410 on my five my version three and five it returns 3,700 so that shows you the difference in Pepin some of the back end stuff when you run a gate command powershell version certain with version 3 will go through all those modules pull out all those commands automatically and now I wanted to show you some of the formatting that happens here the difference between two and three so if I want you here and I do a a git command I have my tab completion and I have tab completion for the property - and I know the property I want to use is count but count does not technically so I have to put that in manual now if I now on birth started with version 3 you get the color coded this color coded syntax so if I start typing over here it's like a light blue yeah it's a light blue that's your objects and then select that yeah these are all command with so they're all blue but when I get to the property you'll see that it turns yellow in this particular instance and my tab completes not working right now you can see it running down here it's uh it does a combination of intellisense and tab completions and sometimes it takes a little bit of time for it to figure out exactly what it's trying to do so there's my tab completion and the other thing that's interesting in ice is it also so it also tab completes the actual property names that are available so I don't I can just see tab and it will automatically show me all the available properties but you don't get in PowerShell version 2 and so the other thing here actually kill that I don't want that's a run if you see how in my command here my measure object isn't capitalized if I go back and I hit tab it will automatically why's that but it gets rid of the rest of my command which you know you lose all that writing when you get to powershell version 3 it keeps that information it will make the edits and capitalize measure object in this case but it won't blow it won't it will keep the rest of the line so that's some of the formatting changes that are different so the other things that are different or there's hidden parameters and switches that get added and commandments that don't that you don't see so good child I don't you know if it's a dir for a directory there's a - hidden switch in the newer versions I don't know where it changed and it will only return the hidden files alright so and as you can see with the get child item the - hidden switch was didn't exist on the PowerShell version - whereas here on my on my version my version 5 I do have that switch so they're they're constantly upgrading that they're upgrading the modules and the commandments themselves and you won't find that hidden switch in the help or the online hope either so it's important to keep up to date as much as you can while people are out there building scripts for their modules and stuff to make sure that you're up to date you're not trying to run a script that has an unknown dependency on a variable or switch parameter that came in an updated commandment yeah so that's cool so hey let me ask you a question what version of what version of PowerShell ships with Windows 10 so PowerShell version 2 shipped with Windows 7 and 2008 r2 PowerShell 3 shipped with Windows 8 1 and Server 2012 ok alright but we ran with the Windows as a service with the updates the you should have an up-to-date PowerShell once you go Windows 10 because it should just come as a Windows Update okay and so that'll bring you up to five and could yeah I didn't actually do any updates I started with an 8.1 computer and then when we migrated it to 10 I started getting the updates and I'm up to PowerShell version 5 now okay all right well that's cool all right well so this is really interesting stuff I want to remind everybody that really today all we're doing is just kind of showing you the interface showing you some basic commands showing you kind of what some of the differences are between the different versions of PowerShell and catch everything go back and watch it again but it's not critical we're gonna have Steven back and he's gonna actually walk us through creating some PowerShell scripts and that ought to be really really cool and interesting and I just can't wait for that Steven thank you for doing this for us yeah it's my pleasure and I hope to you know I just want you guys to get your confidence up with PowerShell and to start using it so I can see more PowerShell scripts out in the wild yeah I can't wait I'm really excited you know let me just back up a little bit I was on site with my customer and Steven came on site with my customer and they had you know four or five nagging issues that I think you came in and wiped out with PowerShell scripts in like 20 minutes so so it is a very very powerful tool it's it's amazing the things that you can get done via PowerShell and I'm hoping like Steven that everybody gives it a shot there are a couple of books that I've looked at there's a pretty good learn PowerShell in 24 hours book that I picked up at a at a book store I'm not going to say the name because we can't like you know we can't give props to one over another but really in any bookstore you can find lots of PowerShell documentation lots of books on the internet there's plenty of PowerShell documentation is is there a specific site that you like to send people to Steven the scripting guides and Ed Wilson keeps that site going with multiple posts a day sometimes so there's plenty to read and there's mailbag items too where people ask questions and they give simple simple answers to common questions awesome and we'll throw a page up that has that link but if you know you don't want to wait for that you can certainly just go to Bing and type in the scripting guys in and you'll immediately get a link to that site alright so I can't wait for episode 2 or part 2 of this thanks again I really appreciate you being on and guys that's your taste of premiere
Info
Channel: Taste of Premier
Views: 2,111
Rating: 5 out of 5
Keywords: Windows PowerShell, PowerShell, Microsoft, Windows, Windows Server, Windows 10, Windows Server 2015, Automation, IT, IT Pro, DevOps
Id: DYHSi5QK2bY
Channel Id: undefined
Length: 37min 49sec (2269 seconds)
Published: Wed Dec 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.