5 Tips to Help You Learn Windows PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay let's hear it for windows powershell yay okay one more time let's try again windows powershot is the best yeah okay all right if you're like me i don't go into windows powershell hardly ever i kind of avoid it there's always other ways to do things than going to powershell but actually what i have discovered is if you pick up a few tips and tricks if you kind of ease yourself into it don't jump in with the 500 character long commands that don't seem to make any sense if you just ease yourself into it actually powershell can actually be quite useful hello there my name is gary sims this is garageband today i want to give you five tips to help you learn windows powershell so if you want to find out more please let me explain [Music] now the problem with windows powershell is because it's kind of tied to the way microsoft think in terms of net and objects it can be pretty uh you know kind of cumbersome you know some of the commands are so long if you type in so much stuff i'm going to die here typing in all this stuff and really you know if you don't understand it you know you kind of it's just like no leave me alone with that and that's where i've been but as i said in the introduction you can actually learn some useful things about powershell so in this video what i'm gonna do is gonna go straight to the powershell we're gonna do an online hands-on tutorial we're going to look at five things to help you learn powershell and i hope you find it useful okay let's get cracking okay so here we are inside of powershell it's a command line so you type something you press enter and then uh something happens that you want to happen now the first thing we're going to do is make sure we're running the latest version of powershell now there's a rather cryptic command and this is as i said part of the problem with uh powershell not everything is kind of as it dollar ps version table will tell us the version as we can see powershell ps i'm using version 5.1 now there is a newer version so we're going to upgrade now the way you do that if you're running windows 10 or above i hope is you type win get search and wingate is a way of windows get installing software on windows from the command line this is also the um preferred way of installing powershell so you do a win get search microsoft.powershell and that will go and search the repositories that it has set up and come back and there we go we can see there are two versions one is the latest stable version and one is a preview version so what we can do now is we can say win get install microsoft dot power shell and that will actually go ahead and install the new version for us [Music] okay so i've restarted powershell and in fact we can see at the top there powershell 7.2.6 let's run our same command again version table by the way if you hit tab you do get completion so i typed in their dollar psv and then tab and it came up and there we go now we're running version 7.2 okay so that's how you get the latest version so let's talk a little bit about powershell now if you are used to talk to the old days of ms-dos and also uh the command prompt used to get in windows windows xp windows 95 98 windows xp and so on you can use all those commands which means for someone like me who kind of came up uh with dos and then the command prompt in windows that's easy to learn so many of these complicated commands so you know you can do things like you know cd make directory so let's for example let's do a make directory temp we can cd into temp change directory cd okay we could for example uh copy a file from somewhere we can delete files so for example i've got a file in the directory above dot dot for directory above uh it's called 10 megabyte file and i can copy it to here and then we can do a dir and there you can see it we could uh rename it with ren ren 10 megabyte file 2 to 10 megabyte file 1 dir it's there we can um delete it with dell 10 megabyte file one so those are the kind of commands you would be used to from with inside of the normal command prompt and really your first introduction to powershell is to use those to use the uh the commands that you're familiar with that have been around in microsoft kind of ecosystem for you know for for decades really but of course that's just simple stuff like changing directory renaming a file copying a file moving a file deleting a file there is of course much much more that can be done now one thing that's good are from the command line to be able to kill a process that's not responding of course you've got the task manager but when i bring up my task manager there's so many processes in there some of them are user processed some are background processes and it's really quite difficult to actually find the one that i want uh to kill so i actually find it often useful to do it from the command line now the way you do that and now we're going to actually use our first time we're going to use a much more kind of power shell kind of thing uh command get minus hyphen process and that will list all the processes that are running on your windows machine and there you go there they are all listed now i've actually got a copy of uh notepad running look here it is here's a copy of notepad and what we're going to do is we'll try and find that copy of notepad so what you can actually do is you can do get process and then notepad and now we can see here that that is the notepad process and it's running how you know it's cpu usage and memory uses all that kind of stuff won't go into that now but if i wanted to kill it i could actually uh get rid of it so let's see whether so it's still here it's still alive i think if i click on the terminal it's going to disappear behind it so i can't show you being killed but what we can do is you can do get process and then we say notepad and it's interesting once this gets this you can then pipe this is different to the pipes that you get in linux or unix this is actually passing an object and the whole thing about powershell is is it reflects different objects that are created this that's for a more advanced tutorial but if we pass it into stop process then it will find this process and then pass that object through to the stop process command and it will kill it so if we now do that and i can tell you that it's gone and in fact if we now do get process again we can see there is no process with the name notepad because it actually got rid of it let me start it up again so here it is actually up and running again now what get hyphen pro says pass it into stock pros this is where i personally struggle with powershell too much too long i'll spend all my life whole life typing in these things luckily there are things called aliases now you can actually there's an alias for get process which is the same as the command you'd have on on linux so if on unix if you just type ps now you can get a list of all the processes so if you kind of know a bit of linux then you can also actually get by a bit on powershell and we can do the same thing we can do ps notepad and that will find the notepad and then we can do the same thing we can do ps notepad and now pipe it into pass it through to kill which is an alias for stop hyphen process and and it's gone again off screen there it went if we do a ps now for notepad it's actually gone now how do we get a list of these aliases well there's actually a command called get get alias and that will list it all out here so if we just scroll up here a little bit there we go ps that we typed it is the same as get process so that's a pretty handy kill here is the same as stop process and actually you'll find that a lot of things man that you find on you know linux is help ls is the same as get children item which actually is how you do a directory listing and so on so there's actually you can study these and you'll actually may find that there's a lot of commands here that you might be used to or familiar with and you can say oh that's good i can just use uh the alias now if you're looking for a particular alias for example we wanted to find out what the aliases were for for stop process what you can do is you can say uh get uh alias and then you can pipe it into and you do a fine straw find string minus i because we don't uh care about the uh the case so case incentive and let's do process and there you can see get processes and stop process we've got several different uh uh aliases and you can use them however you feel uh what you're most comfortable with so they've not only included kind of the linux ones there's also kind of you know short term those gps is get process so they've got their own kind of short term shortcut versions as well which you can use now i've been typing lots and lots of things here now the interestingly there's actually a history where you can see all the different things that we've been typing there you go there's all those commands you've seen me being typing while we're here now you can actually get that with an alias just h and that will list if you wanted to repeat a particular command what you can do is you can invoke history i h wise it's alias i'm not typing all that lot in so if we did uh number 15 for example what's number 50 is this one here look it will do a ps so if we do i hy15 it repeats and does appear so it's really handy if you've been typing in very long commands and you want to uh you know find go back and find them without having to type them in again now another one for example if we wanted to uh redo get alias what we can actually do is an invoke history and you can actually just do get a like that and it will actually show us what in fact he did the last one which would get alias with the fine string for process so it doesn't only you don't only have to provide this number here you can also provide the first few letters of the command and it will find the newest one and execute that so that's what that's what we did to get a there and it found get alias of course be careful with this because obviously you could suddenly invoke a command that you uh that you had like typed in ages ago and it's still in your history and it reformats your discussion i mean i know that's exaggeration but i'm saying be careful when you're calling up history that's equally true on linux very very powerful way of doing a quick shortcut but make sure you don't invoke the wrong command by mistake okay let's talk about how you can put content into files this is very useful if you want to have a quick way of just putting in some fixed content particularly for testing things so we're going to create a new uh file a new item that's what it is in powershell ni and we're going to create a file called test.csv a comma separated file and there we go it's created and it's noticed their length is zero because it's now empty so it's created an empty file for you now we have to go use a long con along command now uh set content so set the content of what of test dot csv well two what well let's put some comma separated values in here let's put in some prime numbers there we go and to make sure there's no carriage return line feed on the end i'm going to put dash no new line at the end so there you go if we now do a dir we can see there is now a fight and then it's actually got something in it 12 characters and we can use cat uh you know like you would on linux it's an alias again for showing us the contents of test dot cs video one three five seven eleven now you can actually uh add to the content of that file there's actually an an add uh content command it's also got an alias ac so let's say ac test dot csv and we're going to put in comma because there wasn't a comma at the end there so we're going to carry on 13 17 19 and 23 and again no [Music] new line and now if we go and do a cat of tess here so there you can see we've now created uh even more content all on that one line now thankfully you don't always have to use uh you know add content setcon that you actually do redirection like you can do on uh like you can do on linux if we do a dir well let's do a dir and pipe that to dir dot text and actually what you'll find is now there is a file called dir dot text and if we have a look at dir dot text it's exactly the output that we had from the dir command now that redirection there's actually several levels of redirection you can redirect everything redirect errors only not include errors we won't go into that now you can look that up if that's the kind of thing you're interesting but just like linux what's interesting though is i can now do dii and if i use double uh chevron's double greater than signs and then again dir dot text it will append to it so now if we look at that that file is even bigger 544 bytes up from 272 bytes and now if we look inside of it we can see that is in fact a copy of two dir instructions so again you don't you you can use these aliasing things really a good way to get into powershell if you're familiar with doing uh other things okay and the fifth and final thing is how we use and this is really powerful secure shell there wasn't always uh ssh secure shell available on windows but it is here so if i wanted to secure a shell into a raspberry pi and let's say i've got one at 192.168.1.44 i actually have to know with four four it's my four gigabyte version of my raspberry pi four so four four easy for me to remember and here is a command line version of secure shell something you're familiar with with linux but it's great having it here on windows i'll just type in the password and that's it i'm now connected to that raspberry pi 4 i can run normal linux commands here h top showing me what's going on on that raspberry pi everything looks great on there now what was interesting of course is that i had to type in my password now there is a command on linux and on units called ssh copy id and this is a way of copying over the the keys onto the other machine so you can type login without needing to specify your password the the keys are checked to make sure everything is okay now there is that this command doesn't exist if i hit that it doesn't exist it's not recognized i don't have that but there is a way of doing it so that you can actually achieve the same thing now it's a bit of a long command but it's worth actually noting it so what do we do type that's the same as cat that means display the contents of dot ssh id rsa pub that's your public key that you want copied over to the remote machine pipe that into secure shell and there's my connection through to my raspberry pi 4 and then once you get there execute this command cat double chevron's means append it to the file authorized key so basically take this public key and add it to the list of authorized keys now the first time you do it of course i need to type in the password again because it's never been done before and that's it it's done it so now if i go back and actually do a secure shell i won't be typing password there we go i'm in straight away you should be doing this with all of your uh raspberry pies and whatever you've got on your your jets and nanos whatever you've got on your local network but it's also great to do it here from with inside the powershell and another thing we can do what have i got here i've got this far right dir dot text is i can do scp that secure shell copy so copy over the network using secure shell and we're gonna well let's use uh dir dot txt and i want to copy that to pi 192.168.1.44 and where do i want to put it well i'm just going to put it straight there in the user directory so dot of course means in that current directory and that will now copy that file all the way over to my raspberry pi so if i now connect into my raspberry pi again no password needed i can now do a cat of dir.txt and there it is that file that we created with the list of directories so now from the powershell command line you can access your linux machines you can access anything that's got secure show you connect over the internet too maybe you've got some web hosting some web servers you've got some servers in the cloud docker whatever it is you're doing you can now connect here from the thing and of course you cut from powershell and you can copy files around using secure copy so that's absolutely brilliant so if you kind of take those five things that's really your favorite dos commands that you're already used to process management killing files using the history being able to add things to files create files uh pipe the content of the output one command into another file and also then using secure shell and secure copy if you can get the master those things i think that you're on a good footstepping to understanding and even growing to like maybe even growing to like windows powershell okay so there it is five ways to help you learn powershell i really hope you enjoyed it if you did please do give it a thumbs up if you like these kind of videos well tell me in the description below what kind of videos like this you'd like to see more of that would be great and also do subscribe to the channel don't forget you can follow me on twitter at gary explains and i also have a monthly newsletter go over to gary explains.com type in your email address no spam but you will get the newsletter okay that's it i'll see in the next one [Music] you
Info
Channel: Gary Explains
Views: 41,144
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Windows, Microsoft, PowerShell, task automation, command-line shell, scripting language, Command-line Shell, command shell, Windows command shell, windows command-line shell, .NET objects, PowerShell 101
Id: KOTH-9uVHTw
Channel Id: undefined
Length: 18min 34sec (1114 seconds)
Published: Tue Sep 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.