Microsoft Azure AZ-800 — Section 3: Deploy and manage AD DS domain controllers
17. PowerShell Fundamentals for Server
Now, if you’re going to do server administration, one of the things that is definitely going to be necessary is that you understand PowerShell, not only that you take an exam, PowerShell is definitely going to be one of these things that’s going to show up. So, it’s important to have the fundamentals down of PowerShell. Not to mention, there’s going to be PowerShell references and stuff that we’re going to look at throughout the rest of the course, and it’s very important that you understand it.
So, we’re going to start with some fundamental concepts to make sure you understand how the base of PowerShell is going to operate and how you can administer servers with it.
OK, so first things first. PowerShell came out in the year 2006. It is a much more powerful command line environment than what we had prior to that, which was just called command prompt, which was originally Dos and Microsoft. They wanted to add something that could essentially touch every part of the operating system. So, they came out with it in 2006. It was fully implemented with Exchange 2007, and it’s been in every operating system ever since. And it’s again one of these foundational concepts that pretty much we have to understand going forward because you can really speed up the process of server administration, pipeline administration, cloud administration. If you understand it, OK, you can write scripts, you can automate things. And there’s just a there’s a lot of benefits to having a good understanding of PowerShell. All right.
So, to begin with here, how do we get into PowerShell? We’ll probably the easiest way is to right click your start button and go to PowerShell. You’ll notice a couple of versions of PowerShell. You have just regular PowerShell and PowerShell with an admin. The admin is going to obviously give you administrator privileges and then regular PowerShell is just going to treat you like you’re a regular user. So, you can also go down here and do a search for PowerShell, and you’ll see that there’s an x86 version of PowerShell that’s a 32-bit version of PowerShell. So, if you were interacting with something that needed to be 32 bit instead of 64 bit, then you could utilize that version. OK.
But I’m going to right click the Start button, go to Windows PowerShell, and here I am inside PowerShell. The one thing I’m going to know right out of the gates is that if it says peace in front of it, I’m in PowerShell. Another way to get into PowerShell is if you were in command prompt, you can simply just type the word powershell and that’ll bring you into PowerShell. So, it’s another way. Now, I tend to like to right click I like that blue background. It kind of indicates that I’m in PowerShell or not. Command Prompt is just more of a visual cue. You can adjust the settings of PowerShell by right clicking the window bar here, and you can adjust your font and all that if you want. All right.
So, here I am in PowerShell now. The first thing to understand about PowerShell is that it uses a verb noun system. So, that basically means that everything you type is going to start usually with a verb. All right. So, like get is a verb, meaning ‘get me some information’; set is a verb, meaning ‘I want to modify some kind of parameter setting.’ OK. I want to set something. Add means I want to add something. So, I want to add something to something else. Like, for example, if I wanted to add a user to a group, I could do that. The word new means I want to create something new. OK. Move. Well, obviously means I want to move something. Copy. It means I want to copy something. And so those are your main verbs. All right.
So, from there, you’re going to follow up the command, or command it as it’s called, with what’s known as a noun. There’s going to be a -that will separate the verb and the noun. So, for example, if I wanted to see what services are running on this machine right now, I could type get and then -and the word service.
Now, before I hit Enter also want to point out that PowerShell supports what’s called IntelliSense. IntelliSense means that it can try to detect what it is you’re typing, for example, if I just typed get in the letter -s and then I hit Tab, you’re going to notice it’s going to try to detect what I’m trying to type, right? And I can tap through just continuously and hit Tab until I find the command I’m looking for. If I accidentally pass the command I’m looking for, I can hold the shift key down and I can go backwards. OK, so, I can go forwards, I can go backwards.
Another thing you can do is if you type a few more characters, it’ll get a better, it’ll get a more accurate look at what it is I’m trying to find, for example, if I type get -se, it’s going to narrow down the IntelliSense and it’s going to toggle through each one of the nouns that start with se. So, there you go. There’s service and I highly recommend that you utilize Tab. Tab is going to help you not type all stuff. It’s going to help you type faster. So, it’s always like to tell people, ‘Tab is your friend, use Tab.’ All right. So, we’re going to hit Enter and you’re going to be able to see the different services that are running right now on the machine. I can kind of scroll up and down looking at those different services.
OK, now if I want to modify one of those services, I can change the verb, right? So, for example, there is a service right now called the WinRM service, which is a pretty important service for PowerShell because this what is known as Windows Remote Management. This actually what PowerShell uses when it wants to remote into another machine. Actually, that service, it’s more important for that service to be running on the destination machine you’re connecting into than the source machine.
But notice that that service is running right now. So, let’s say I wanted to stop the WinRM from service right now. I could actually type the word stop, right? That’s my verb, and then put a -and then the word service, right? So just tap through that. And there it is.
Now, if I hit Enter, you’re going to notice that it’s going to pop back and say, ‘Hey, you got to specify an input object.’
So, the important thing to understand about PowerShell is the PowerShell needs parameters for certain commands. Parameters are required pieces of information that are going to be needed for PowerShell to do its job. OK. The analogy I like to use is kind of like if you go to a restaurant, let’s say, the restaurant that you go to eat, you know, you’re going to get dinner or something, the restaurant works off PowerShell. So, you go to the restaurant and you type sit -chair and you sit in the chair and you type order -food and a waiter pops out and says, “OK, what can I get you?” And at that point, you’re going to need to specify some parameters, right? You need to specify what kind of food you want. So, for example, if I was going to order steak and I typed order -steak at that point, the problem is that the waiter is going to want to know, “OK, so you want steak? Well, how do you want your steak cooked? What kind of steak do you want?” You have parameters that you have to specify. So, a lot of PowerShell commands are going to work like that. All right.
So, let’s say, I don’t understand how to use the command. I’m going to hit Enter. It’s going to throw an error, OK? One thing you’ll notice about PowerShell is that it gives you pretty detailed errors. OK. But I’m going to use a command called Get Help. So, I’m going to type get -help. So, you guys with Linux experience, you can also type the word man next to that. And that’s kind of how we do it in Linux. But I’m going type get -help and then I’m going to type stop -service. OK, stop -service and I’m going to hit Enter and it’s going to give me some help information that I can look at on how to use the command.
Now, to be honest with you, I’m not a big fan of the help documentation within PowerShell. I prefer to just do a search on the Help. One thing you can do is some commands will let you do a -Online and it’ll pop the website up. But not all commands support that. What I’m going to do is I’m going to go to Google and I’m going to type stop -service from there. There’s like 99 percent of the time there is a manual document that Microsoft has set up on their website. All you got to do is just do a quick Google search and you can find it. There it is right there. Stop -service. So, I’m going to click on that and it’s going to give me information is going to tell me each one of the parameters. It’s also going to give me examples which are really-really like. OK.
Now, I will also tell you that in some cases with PowerShell, you can type -Examples and they’ll have some examples built in. Not all commands have -Examples, though. OK. So, just be aware of that. They do on the help documents that are on Microsoft’s website. So, I’m a huge fan of using their help documentation. Guys, you can always if you don’t understand how to use a command, you can just about always do a search, find the help document, and it’ll give you examples on how to use it.
So, if I want to stop the WinRM service, there’s an example of how I can do that. I’m going to copy that and then I’m going to come into PowerShell and check this out. All I got to do is just right click the window and it’s going to type it in for me. All right. And then I’m just going to change the service name. All right. And just like that, the WinRM Service now should be stopped. You don’t get like a congratulatory message or anything. If no, I should say no news is good news. If you don’t get an error. It worked.
So now we’re going to get that service. And by the way, you can hit the up arrow on your keyboard and cycle back through your previous commands. So, there it is. Get -service. And you’ll notice that WinRM is stopped, which also means that if I wanted to start it back up, it’s probably going to be the same thing, right? Start -service, right then dash. Now, get this. IntelliSense also supports the parameters as well. So, I can toggle through the parameters just like that hitting Tab, which is really great being able to do that. All right.
So, the -Name is the one I want. I’m going to say, Winrm. And then at that point it should be started up. And it is. OK, So, we’re in good shape. All right.
So, there’s lots and lots and lots of commands in PowerShell, like I could type Get -Process, I could see the processes that are running. All right. If I wanted to see what the event logs are doing in windows right now, I could type Get -EventLog, hit Enter. Oh no, there’re some parameters that I need. Well, again, all I got to do is just go to Google, right, type the command in Get -EventLog, hit Enter. There’s the command. All right. Here’s the help document that is pulling up, I can scroll down and I can see examples on how to use the command. All right.
So, in my case, I’m going to do something like this Get -EventLog -LogName System –Newest 5. So, if I do that, let’s just hit, Get -EventLog -LogName System and then show me the newest five results. There we go. There’s the newest five results.
Now, what are we looking at? Well, if we right click Start here, we can open up the Event Viewer in on our server. We can expand the Windows logs and then click System. These are the logs that we’re talking about here. OK. Of course, it’ll take a moment to load these up because there’s a bunch of log entries, but once that loads up, you’ll see those.
Now, another thing you’ll notice is that it kind of minimizes the information. You don’t see it all, expand it out. So, what I can do is I can change the formatting with PowerShell. I can use what’s known as the pipe command, which is that little character, right? There’s the character above the inner key if you hold down shift, right? So, piping is going to take the output from one command and it’s going to attach it to the input of another command. So, there’s actually a nice little command called format -list. So, right now, this is formatted as a table and I can say “Format -list,” and it’s going to format it as a list. I can hit Enter and it’s now going to format it as a list. So, now it’s going to expand it all out. All right now, check this out. I can also hit the up arrow. I’m going to pipe it again and I’m going to say “Out -File c:\”. And we’ll just say, “log.text”, and it’s going to dump it to a text file. So, we’ll hit Enter. We’ll go to our C drive now and pull that up. And there’s the log, and here it is in a text file. All right.
So, very-very simplistic commands there that you can run, you can change the log if you want. Like I could change it to Security if I want, or Application, or whatever and it’s going to going to put it in that file. All right.
So, the other thing about PowerShell, sometimes the trick to PowerShell is just figuring out what command you’re looking for, that you’re trying to perform. So, for example, let’s say I’m trying to perform a command that’s got the word net in it, and I can’t remember the command. So, what I can do is I can type get -command, hit Enter. And then from there you’re going to see every single command that’s in memory right now. It’s going to list them all out on your screen, which of course, is very intimidating. There’s a huge list of commands. So, let me show you how we can narrow down the command.
So, let’s say I’m trying to find a command that has the word net in it somewhere in the command. I can type get -command. Check this out. Hit Space -Noun. OK, and then if I just type the word net, nothing’s going to happen, OK? Because there is no command where the noun is just the word net. OK. So, what I’m going to do is I’m going to use a wildcard. If I put a star symbol in, asterisk symbol, after the word net, then it would show me every command that where the noun starts with the word net. If I put the asterisk before the word net, it’s going to show me every command that ends of the word net. If I put the asterisk at the front and the back of the word net, then it’s going to show me every command that has the word net as the noun. So, if I hit Enter, there’s every command that has the word net somewhere in the nail right there, OK?
All right. So, let’s say that I want to narrow down my search even more that I’m going to add, I’m going to hit the up arrow, hit -Verb. And we’re going to put the word reset in there. So, I’m narrowing it down. So, I know the verb has the word reset in it and the noun has the word net. And look at there, we’ve now narrowed this list. And, of the course, again, let’s say that the command were looking for was, maybe, this command right here, Reset –NetAdapter. So, I’m going to right click that and I’m going to go again to Google, go back over to Google, paste the command in there, hit Enter, and look at there. There’s a help document.
So, it’s very important again, that if you don’t understand how to use a command in PowerShell, you actually can just go in and do a quick Google search. Here are examples on how to use the command. All right. And again, I’m a much bigger fan of doing that. OK. All right.
So, the other thing about working with PowerShell is that PowerShell uses these things called modules, OK? Modules load more commands in memory. You got to understand that there are hundreds of thousands of commands out there. OK. And so, if we type -Module, all right, you can see that these are the current modules that are stored in in memory right now. OK.
So, if I want to run certain commands, I need to have the modules loaded in memory, for example, Active Directory. One of the things you can do once you have Active Directory loaded is you can have the Active Directory commands loaded in the memory so that you can run those. OK. So, you can say Import -Module —Name activedirectory. And it’s now loading all the Active Directory commands into memory, OK.
Keep in mind, you need to have Active Directory and all that going, but you can load those in memory. You can also download commands from the internet as well. So, Microsoft has this thing called the PowerShell gallery. In case I go to powershellgallery.com. You’ll see right here there’s lots and lots and lots of commands that are out there. OK. So, I could look up a command that I want, like I’ll just think of something off the top of my head. How about autopilot? So, there’s a cloud technology called autopilot. I’m not getting into it right now, but if I do a search on autopilot, here is a script for dealing with autopilot. It’s going to grab something called the device information off the machine, the hardware ID of your computer so, it can be used with this thing called autopilot.
So, if I wanted this command from the PowerShell gallery, I can click on it and I can run this command right here. Copy it Install-Script. All right. So, if I do that and just paste that in Install-Script -Name -Get WindowsAutoPilotInfo… Make sure you have an internet connection in order to do this. If you don’t have an internet connection, this is not going to work, but it’s going to tell you. OK, so to download scripts is going to create a path variable. The path variable is going to be the place where scripts are stored. Are you sure you want to do this? Yes. All right. So, it’s going to go ahead and create that that location on your hard drive to store scripts so that it can run scripts. All right. So, from there, it’s going to ask you if you want install new get new get is the software that’s going to let you download scripts from the internet or is going to go ahead and say yes to that. All right. So, it’s going to go ahead and install new. All right. And then the next thing is it’s going to ask me if I’m sure I want to install the script because it’s going to be downloaded from an untrusted source. The PowerShell gallery is an untrusted repository. Oddly enough, Microsoft owns the repository, but it’s an untrusted repository. And the reason it’s an untrusted repository is because it’s a community repository. It’s not just Microsoft people that upload scripts, they’re the community. Other people can upload scripts as well, although they do have good moderators that generally make sure there’s nothing bad that gets uploaded.
So, I’m going to go out and say yes to that and hit Enter. And at that point, that’s it. The script should be available. I should be able to say get -WindowsAutopilotInfo.ps1. And there you go. There’s a script. I’m not going to try to run that script. I’m not really explaining what that script does right now. I just wanted to demonstrate how to download scripts off the internet. OK. The ps1 is a script file, is what that extension is. OK. All right.
OK. So, now hopefully you understand some fundamentals of how to use PowerShell. This is just the basics of PowerShell and there’re other concepts to look into as well, like communicating with the network in the IEC, but these are just the fundamentals of PowerShell.