Top 10 PowerShell Commands for Beginners | Realistic Examples with Explanations!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody josh here again welcome back to my channel in today's video we're going to be covering my top 10 beginner-friendly powershell commands powershell is essentially a programming scripting language that's originally native to windows but you can install it on mac os and and linux as well pretty much like anything that you can do on the computer anything that you can imagine you can automate it in powershell in one way or another i've used powershell for like every single one of my it jobs and every single one of my cyber security jobs and i even had a job where i like specifically like ninety percent of my time i was developing some big automation project uh using like 100 powershell so it's really useful to know at the very least if you're interested in it or cyber security i think this video will probably be interesting to you at the most it will be like really helpful to you kind of opening your brain up to the powershell world so let's go ahead and get started so feel free to follow along if you want with your own like kind of environment or you can just watch and be entertained as well you'll definitely get something out of it either way so okay so first thing i'll do i'll just open up powershell ice and i'll just kind of open it up on one side of the screen and on the other side of the screen i'll open up a windows explorer and i'll just go to the c drive and then i'm just gonna make a folder here because we're gonna work with files and stuff a little bit so i'll just make a folder called powershell demo and i'll go to it see it's empty and then on the powershell command line down here i'll just change directory to the powershell demo folder like this just so i can kind of have it here okay so for the very first command it's going to be get content get content and basically what this does is it reads all the content from a given text file for example so in our folder over here i'll just go ahead and make like a text file like super quickly i'll just call it hosts and then inside i'll put like google.com instagram.com facebook.com and i'll just save it and then i'll say get content here and then i'll just say host dot text and i will press play and then you can kind of see simply here it just prints these out to the screen there's a lot of different utility for these like if you want to do some action to each one of the items in the list you can you can do that uh you can also this is how you make a variable in powershell you do like a dollar sign and then you you name it something like this like so and then if you say play then the script runs and you notice nothing got printed out but actually the content of hosts got stored into this variable so for example if i go down here and type posts and say enter it will print out the content of this host's file over here so i use this like all the time because there's a lot of instances where you have to like read in some text from a file and kind of do something with it and print it to the screen and then the second command that i use all the time is the for each object command if you're if you've done like other programming before you can kind of think about it as a for each loop because that's essentially exactly what it is but basically the for each command lets you iterate through a collection of objects so what i mean by that is when we do like this get content and bring in like the hosts like bring in these three things and say we want to like store it to a variable we can do we can then do this essentially for each object and then we can do like open close braces to indicate like the block of code where we do stuff in and then when we say like dollar sign underscore this this represents essentially this dollar sign underscore represents each one of these it's kind of confusing at first but maybe try to recognize what happens when we actually run the code so i'll run this and it pretty pretty much looks the same it just prints them out one at a time but the kind of difference with this is it you have the chance to to do something to these items before they actually get printed to the screen so for example we can do something like something like this and then run this and run this and oh you can't really see it very good yeah so background color white foreground color black it lets us manipulate this this these objects in one way or another like each time the loop iterates it presents us with like for example the first time the loop iterates this dollar sign underscore presents us with this the second time this the third time this and then so forth if there's more items in your the content that you got from here if that makes sense so for each object kind of just lets you do something with each object in the list of things so we can do other things too like we can say for example like this you can say hostname we did hostname colon and then the actual thing that's on that line if that makes sense so it's essentially a for each loop i use this all the time and production scripts and stuff i do my personal projects it's just like something you use all the time and by the way like as you're watching this don't look at this and be like oh man i have to memorize all these commands i have to memorize these like you don't have to memorize anything just look at it and kind of acknowledge that it's possible to do and kind of just do your best to understand what's going on and then if you need to remember again in the future you can always just google like oh how to how to do for each powershell like oh how to like read in text file powershell and then it will come back to you at that time so yeah and then the third command that i use like all the time is out file and basically what outfile does is it essentially like takes something that's in powershell and then just outputs it to a text file so for example i'll just actually i'll leave this here for now so you can say something really simple my name is josh and then do a pipe out file and then you can press tab and then you can pick like the file path or i believe you can just if you want it to be in the same directory you can just start typing the name of the file s dot text and then if you say append it will add on to the file if there's something already there so i'll say append and that's that's good enough so i'll just copy this and i'll do one more line this and i'll save it and then you can just highlight these two lines like this and you can click this play button and it will only run these two so i'll display these i'll close this and then we can see that there's a test.txt file in here so if i open this up you can kind of see like what happened here like my name is josh my name my name is timmy i use this all the time it's really good for like writing vlogs if you've ever opened up like a log file there's like a date and like something that happened that it's really good for that kind of thing so we can do something like here we can go back to our our hosts i'll just like close this for now we can go back to our kind of hosts thing that we did up here and instead of write host we i'll just do something like yeah i'll just do this so normally if we run this right it just prints these three to the console but i could do something like this out put hosts text now i'll append it i'll save this and i'll run this and then we have like another text file over here called output hosts and then it kind of matches what we previously had printing printing to the console we just like redirected it to this text file here again i use this all the time for like writing logs and and just if i need to write something to a text file for whatever reason use it all the time so that is uh out file the outfile command and the next command on the list is test net connection and you can think about this as kind of like a ping on steroids essentially so i'll just i'll just type it and i'll show you so test net this is a great command by the way test net connection and then like i don't remember what the parameters are so you can always press tab when you're working in powershell ice or ise and then you can kind of see the parameters so we can say computer name and then we can just enter like you know something like google.com and then i only want to run this line so i'll just click this thing and then you can see it kind of returned uh this kind of basic things like computer name remote address this is ip6 address ethernet ping ping success and then the the latency essentially so there's a lot more inside of this command but it kind of it shows you like a really high level summary of the status of the the net connection that was tested there's a lot of ways that we can see like the full detail of this so we can we can do like i'll just assign this to a variable so i'll say like host status equals and then i'll run this again and basically it's going to assign the status of this command to this variable so i'll run this and i can say i can go down here even i can say host status and then i can do a pipe and then i i'll say like select everything and we'll say enter and it shows like much there's like much much more that you can see a bunch of like dns records and and some other kind of information in here and it's kind of interesting uh going back to this there's kind of more stuff you can do with this you can check if certain ports are open so for example um port right here like i don't know if you've studied like security plus or network plus or anything but port 443 is usually like https maybe they're using a tls or something like this so we can test if por 443 is open for google.com so we'll click here and then we can say tcp tests succeeded true when before it said like a ping ping succeeded so now we're doing we're like pinging but it's over like tcp port 443 for instance so we can kind of take this even like a bit further and kind of integrate some of our other commands that we've used so instead of just doing like the individual like you know testing of google.com we can kind of erase this and then we can get rid of the stuff and then remember like this hosts thing we're reading in like these hosts like google.com instagram.com facebook so we can kind of set this uh for each loop or object to go through and like ping test like each one of those if that makes sense so remember if we just do like oh not hosts if we just do like dollar sign underscore and then run this it just prints out google instagram and facebook so if we want to do like a test net connection for each one of them we can do something like this we can do test net connection computer name and this is our computer name remember the dollar sign underscore represents kind of each one of these lines and then we can say port 443 and we can say 480 and then we can say like something like right host testing host and then dollar sign underscore and then we'll just do like a an empty and empty one afterwards so you can kind of maybe guess what's going to happen with this and then we'll run it and then kind of observe what happens here so that went really fast so basically we have testing host google.com and then it says like we see like testinghost google.com and it kind of shows we tested google over 443 and it was up and over 80 and it was up testing hosts instagram.com over 443 up 80 up and then facebook 443 up and then 80 is is true as well so it's pretty interesting like what you can do you can kind of think about how this might be useful if you have like a bunch of hosts you want to check to see if they're online or something you have a big ass like text file full of host names that's you know something you know that's how you could use these commands in conjunction with each other to kind of do something useful that has a business case business use case and the next command on the list is convert to json so this command it's not that useful unless you know what json is um json's just javascript object notation it's kind of used to represent data in a certain way and i i tend to use it a lot in powershell because a lot of the items and stuff you see in powershell they're actually objects for example for example we'll take the hosts variable that we made up here inside of the host variable if we say enter you can see it's simply just like google.com instagram and facebook.com but there's usually in powershell objects there's usually much more involved in that and if for some reason i want to see like what's in the object because i want to access it or read it for for some reason or another i usually use convert to json to kind of see what the object looks like if that makes sense so i'll just do a demo we'll show you like what it looks like so just normalhost.com has this inside it but it can convert to json you can kind of see it has like much much more properties in it than you than you might have thought so it kind of shows like the value so it's essentially like a list of text it kind of shows like all this like information about it like ps path it's like an array right and it has like value of this ps path is this where like where it lives the folder it lives and is this the child name is this and like all this kind of other stuff so it's possible to to reference these things inside of here like you don't have to don't care about this too much but for example hosts it's it looks like it's actually an array so if i want to do like the zeroth element of hosts i can say enter and it actually like shows google.com and like the first one it's instagram.com it's kind of interesting to like look at what certain objects are and then you can see the structure of them then you can like access data and like manipulate them in in different ways so for example if i do host 0 and then i do convert to json i can see like all this stuff it's it's really crazy this might be overwhelming to look at so don't don't like worry about it too much and the next command that's on the list is get date and that does pretty much what you think it would do so get date you can say enter and it kind of spits out a date for you here get date and then you can you can kind of uh get specific parts of the date by putting it in parentheses then you can say like getdate.date or you can say getdate.day day of week and it says thursday right because today's thursday it's just kind of a useful thing to do so for example one way that this is useful is like if we want to write some kind of log log file or something like this so again going back to our trusty for each loop here we can add like a line in here that says something like testing host well we're going to make a an output to a log file here with the data in it so we'll say something like testing host dollar sign underscore remember that represents each one of these lines dollar sign underscore dollar sign underscore every time this thing iterates we'll do this and then this is going to be like a log file so we're going to put like a date before the actual message that we're outputting to the log so we can do this get date yeah we'll just do this get date and then we'll do like a colon in a space and then we're going to output this to we're going to use out file again we'll say out file uh we'll just name this host test.log something like this and then we'll append it so we'll run this it's going to spit out a bunch of crap on the screen down here and then it's also going to give us a log file over here based on this so i'll just run this whole file let's say run here and the same thing happened out here again and then if we look on our host test.log it pretty much just printed out this thing right here this get date testing host and then the host name so we have the date the time testing the host and then the host and then the host name i should i should probably put like a hole in here or something but you you kind of get the point um get date you can use it to get the date you can get like specific specifically the year the day the month like if you need to like manipulate the date in a certain way i always do that with scripts um because sometimes you need the date in a certain format but yeah good day always use it like all the time and the next command is a really easy one it's just called start sleep all it does is essentially pause the script for a certain amount of time you kind of notice in this last log that we ran here there's like not very much time in between the log entries which is okay but sometimes you want to slow down your script a little bit especially if you're dealing with like apis you're dealing with like a slow system or something you're processing like a large number of records where you can't just like dump all of them for one reason reason or another so you can do start sleep so this is our loop here so if i just do like start sleep i can put some time in between like each time the actions take place in the loop if that makes sense so i can say like start sleep and then you can say like seconds or milliseconds so we'll just do like seconds and we'll say like two seconds for example and i'll i'll just do something interesting here i'll use right hooks again and i'll say sleeping or two seconds background color maybe i'll do black foreground i'll do cyan just so we can do something a little bit more interesting so basically this script is gonna run it's gonna output this pause for two seconds it's gonna ride out this testing host to the command line it's going to log something and then it's going to test 443 and 80 for these three hosts that are in here so i'll get rid of i'll just do 443 so i'll save this and i'll run it we see oh god that's hard to read i don't know i said it's weird so you can see it's sleeping for two seconds runs it sleeping for two seconds runs it's sleeping for two seconds and then kind of runs it again now if i look in our uh host test.log you can kind of see what's been happening a little bit you can see there's like exactly two seconds between these like tests for the most part this is this log format is bad but anyway you could get the idea the idea behind it so yeah that is start sleep and the next command i've been using it like all along this whole time it's just essentially right host right host i use it all the time to like output stuff to the screen it's really useful because you can like make colors and stuff like the background color you can do you know whatever color you think is cool foreground color red my name is josh it's just very useful like this my name is josh of course you can just like forego this whole thing and just you can just do this in powershell and it runs still but if you want to do something like add color to it or do something like this you can you can use right host and you can do you can kind of customize it it's really useful use it all the time of course you saw me like using it already like in the very beginning but right host and the last two commands they're kind of they're kind of boring but they're also like really really really useful and if you remember like if you want to memorize like any commands in those lists just memorize the last two so this command it's just called git command it's it's very very simple so you can say git command and if you don't remember a command you can use git command to kind of find what it is so for example i'll use git command star right star and then it shows all the powershell commands with the word right in it so you can see inside of here like write host like if you couldn't remember the right host command but if you just remembered host or just remembered right you could use like git command to find it you can pretty much find like any any command like this as long as you know like what part of the command is so for example we can do like git command if we want to like figure out how to reference the hard disk or something like that we can do git command star disk star and say enter and then you see like all these different commands that you can do like get disk get disk image whole bunch of disk commands or maybe you're like oh what does git disk do you can say like get disk oh my god let me shrink this and try it again and it looks like it printed out a bunch of information about my my hard disk it seems like we can do git disk select star and then you can really see a bunch of information about my hard drive so i have an nvme drive the size is probably on here somewhere i did see it just like a second ago you're probably looking at it right now oh it's right here this i i think this is like half a terabyte i think this is in bytes but anyway you kind of uh get the idea like git command maybe there's like a command that has to do with the cpu i don't know doesn't look like it how about processor doesn't look like it about network whole bunch of network commands uh by the way you can get stuff about like the cpu and processor and powershell but those just apparently weren't the right commands but anyway git command is really good and that kind of leads us into our last command so if you remember like any command in powershell just remember this one this is the the get help command you can always just type git help and then for example test net connection and you can say like online like this and oh i guess like every command doesn't have an online aspect to it so maybe we can do like instead of test net connection we'll do like get content and then it kind of opens up a web page um straight to the microsoft docs that kind of shows you how to use this command and it has like a bunch of examples in here of how it works if that makes sense so i i use this quite a bit like if i i know that i want to do something i know it's possible but i don't really know how i'll use like get help dash on whatever the command is like dash online and it will i'll like look at examples and kind of learn how to do it yeah if you found that interesting i have like this other video here that kind of goes through like setting up active directory and like setting up the infrastructure and like kind of toward the end i go through this like long script where we like import and create a thousand different user accounts using powershell and it's relatively real world for the most part go ahead and check that out if you want to yeah that's my top 10 powershell commands i hope that it was useful if you thought it was useful please feel free to like and subscribe i'm trying to get to 10k subs so if you could sub i would really appreciate a lot definitely i respond to everybody's comments all the time so if you want to leave comments 100 i'm going to read it and respond to it i also have patreon if you feel like supporting me and i also do mentoring as well you can check both of those things out in the description but otherwise yeah thank you so much for watching this far and we will see you next time bye you
Info
Channel: Josh Madakor
Views: 43,526
Rating: undefined out of 5
Keywords: josh, josh madakor, powershell commands, powershell, powershell commands for beginners, microsoft powershell, 15 powershell commands, 10 powershell commands, 5 powershell commands, learn windows powershell, powershell for beginners, powershell pause for seconds, powershell copy-item, powershell tutorial, windows powershell, Windows PowerShell, powershell tutorial for beginners, PowerShell examples, Powershell, Top 10 PowerShell Commands, PowerShell for Beginners
Id: MP_UR5iWfZQ
Channel Id: undefined
Length: 19min 55sec (1195 seconds)
Published: Fri Oct 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.