back

PowerShell Basics

for Administrators7 H 50 M

This course is an intro to PowerShell, Microsoft's powerful command line tool. Learn how to take advantage of the many features available in PowerShell.

Episodes
Episodes
  • Introduction
    • Getting Started
    • Basic Syntax and Help
    • Basic Syntax and Help Part 2
    • Command History and Aliases
    • Command History and Aliases Part 2
    • Piping and Formatting
    • Piping and Formatting Part 2
    • Script Execution Policy
    • Script Execution Policy Part 2
    • PowerShell Drives
    • Profiles
    • Profiles Part 2
    • Basic Functions
    • Basic Functions Part 2
    • Module Basics
    • Module Basics Part 2

Getting Started

28 M

itprotv course thumbnailitprotv course thumbnailitprotv course thumbnail
  • Episode Description
  • Transcript

In this episode, Mike and Cherokee introduce PowerShell. They look at the various consoles for working with PowerShell.

Welcome to ITProTV, I'm your host, Don Pezet. [CROSSTALK] [MUSIC] >> You're watching ITProTV. >> Welcome to your PowerShell series. I'm your show host, Cherokee Boose. In this episode, we'll be taking a look at getting started with PowerShell. And of course, we have Mr. Mike Rodrick with us today in studios. How is going today, Mike? >> It's going great, Cherokee, and thanks for having me. And yet you are right, we are gonna be talking about good old PowerShell. Fun stuff, hope everybody is excited. When we get started with PowerShell, I guess the first thing we need to do, like with any technology, is kinda understand where we came from. I think it always helps us to know where we've been, so we kinda see where we're going. And PowerShell is one of those things, it's been around for a long time. And I think over the years more and more people have gotten kind of accustomed to it and started using it. So I'm just gonna take a minute and talk about the history of PowerShell before we dive in and start using it. >> So a history lesson? >> Yeah, exactly. >> Right. >> I gotta do that, right? >> [LAUGH] >> All right, so let's take a look. I've got a little document up here on my screen, cuz a lot of us don't realize PowerShell has been around for a little while. You can see here that PowerShell has introduced back in 2006, so over a decade ago. At the time of this shooting, we've had PowerShell v1.0. And at the time it was an add-on. It wasn't something that came as part of the operating system. It was something we would download. Microsoft's first foray, if you will, into the world of PowerShell. Before that, we had our command-line interpreter, the good old black and white command prompt, that a lot of us were used to using. And PowerShell was new and the idea was this was a new management interface that Microsoft wanted to push out, give us the ability to manage from a command line like interface. And the ability to script things which meant we could start automating things, right? And you might be saying, well, we can do that in the command prompt. Or we could, but the command prompt was a command line interpreter. And it used lots of different commands that were in from different places like netsh had its own set of commands that we could use in that command-line interpreter, firewall settings, or IP address settings. We had different sets of commands that we can use but they were really from all over the place. >> And Mike, it seems like a lot of different administrators maybe coming from maybe like a mixed-base background, they were kind of used to that, having that command-line interface. And with Microsoft, you didn't really get as much a functionality out of that. So really, they were hearing that request, that need, and then kind of conformed and then had the little baby PowerShell. >> [LAUGH] Little baby PowerShell, I like it. >> [LAUGH] >> It was hatched or born or what have you. It came out, and at first, it was kinda had mixed reviews, right? People were a little unsure about it. Those people that were used to their command-line like, why do I need PowerShell? I've already got my command-line interface. So it was something new, something different. But it really did, it began this, I'm trying to think of a good way to say it. Microsoft really started putting a lot of energy into developing PowerShell. And it got to the point where at Microsoft they were developing commands for PowerShell before they were developing commands or that same capability in the interface. If you remember, there were some versions of PowerShell, like managing exchange. There were things I could do in PowerShell that I could not do in the GUI, so we saw this- >> I can see that now. >> Yeah, exactly, we saw that shift in Microsoft where, hey, we are gonna really focus on developing a good scriptable interface that we can use to manage machines. All right, so again, back to my document here, 2006 we had PowerShell v1.0. It was available for download for XP, Windows Vista, and Server 2003. And then in 2009, we got PowerShell v2.0. So big improvement here. It was pre-installed in our operating system. When you loaded up Windows 7 or Server 2008 it was part of the installation. There wasn't anything extra to download. And this really shows you, as Microsoft was shifting gears, and I won't say forcing PowerShell on us but- >> Integrating it into their off-the-shelf solution. >> Yes, and all of a sudden people were thinking wow, this wasn't just a fly-by-night idea or let's see if this works. This is now part of the operating system. It's going to be here for a while, so we can start using it. We can start developing with it and not afraid that it's gonna disappear in the next version of Windows. So again, pre-installed in 2008 Server and Windows 7, you can still download for XP, Vista, 2003. If you wanted to update to PowerShell 2.0, you just had to download it and install it. And the big thing it brought for us was the remoting, the ability to remotely control other machines. And this is a very, very powerful feature. We're gonna look at depth at the PS sessions and how we can manage machines remotely as we go on. Remoting was introduced in PowerShell 2.0, as well as something known as the PowerShell ISE. Before version 2.0, with just PowerShell 1.0, all we had was what looked like a command-line interpreter, the PowerShell >> Window? >> Host or window if you will. >> [LAUGH] >> [COUGH] Yeah, and that's really all we had. With the PowerShell 2.0, they brought us something known as the ISE, the integrated scripting environment. And this was really nice, cuz now all of a sudden I've got a PowerShell interface, but I also have the ability to write scripts right in that same interface. Take advantage of the search capabilities for commands. We'll take a look at the ISE, but it's a great place to get started with Windows PowerShell. I love the ISE. I spent a lot of time there, but if you're going to write scripts, this is where you'll spend some time. So we'll take a look at the ISE as well. Then in 2012, we got PowerShell 3.0. 3.0 came out with 2012 Windows 8. It was pre-installed. And again, we could download that for our older operating system. So if we wanted to get up to PowerShell 3.0 on Windows 7, 2008, we could download those. And you notice I don't have Windows XP, Windows Vista or Server 2003 on there because at this point we didn't have that support anymore. So it was only available for 7 in 2008. But 3.0 brought us online updates for help, and this was big. They were constantly adding new commands, new modules, improving or expanding the capabilities of PowerShell. And trying to get information on those commands, that could be tricky. So having an online help system, or at least the ability to update my help online, meant we could keep that help current, right? It wasn't like I was still using the help file for when my operating system got installed. I was able to go get new help files, and update those help files. So very, very big. And I tell you, Microsoft really took a lesson from some our other operating systems and manual pages and they got help right when it comes to PowerShell, I think. And a lot of great information. We'll take a detailed look at all of the help that we can get within PowerShell because honestly, it's great because it shows you what the command is, what the options are, what the parameters are, examples, detailed help. We'll take a look at that as well. And one of my favorites, IntelliSense for PowerShell ISE, right? Again, ISE was that scripting environment, or the Integrated Scripting Environment and IntelliSense is the ability to start typing a command, and as you type it shows you a list Of possible commands, right? So as I'm typing it out, it's looking at what I'm typing, and it's showing me all the things that start with whatever I've typed so far. And as you type additional characters, the list gets filtered down to fewer and fewer choices. And so if you're not exactly sure what a command is, or exactly how it's spelled, as you type the few letters there you'll get a list of those. So I really like that feature and it's not just the command, it's the parameters and even the values for some of those parameters as well, which, again I'll show you as we go through. So, we got the IntelliSense for ISE in version 3.0. >> Which is great if you can't remember every single cmdlt and parameter there, which I definitely can't, so [LAUGH] >> Yeah, and I don't know too many people that can, and there's thousands and thousands of commands So it's fantastic, especially when you're getting started. Like eventually, you'll have certain commands that you know and use all the time. But when you're looking for a particular command, or you're not sure what parameters are available for that command, the IntelliSense really can help you out. And then in 2013 or 2013 we got PowerShell 4.0. This came pre-installed in Windows 8.1 and Server 2012 R2. We could download it all the way back to Windows 7 2008 which would include Server 2012. And with 3., sorry 4.0 we've got enhanced script security, because now all of a sudden, more and more people are using it, we're using it script, to automate things. And so the ability to control those scripts, make sure they're trusted, was added in 4.0. And enhanced debugging, the ability to, again, more and more people were starting to use this to automate processes, so the ability to debug scripts. Set break points, for example, and work through the code and try to figure out where the problem was, which is a big help as we're writing those more complex scripts. And then in 2016 we got PowerShell 5.0. 5.0 pre-installed on Windows 10 and Server 2016. We could download that for 2012 R2 all the way back to 2008 R2 SP1 and all the way back to back to Windows 7 SP1. That's PowerShell 5.0 and some of the new features that Windows, sorry that PowerShell 5.0 brings us, support for Chocolatey's repository-based package management. >> Sounds delicious. >> It does, doesn't it make you hungry, right? >> [LAUGH] >> [LAUGH] But this is getting, for those coming from the Linux world, you'll understand package-based management, the ability to go out. They can store packages out there in that Chocolatey's repository. Powershell now has the ability to go out, search, find, download and install packages from their repository there. And they've extended support for switch management to layer two network switches. So again always increasing the functionality of PowerShell. So it's a fantastic journey we've been on. All the way back from 2006 till now with version 5. Now, also I forgot to tell you they've got new cmdlts for PowerShell, desired state configuration or DSC. Which is something we'll spend a lot of time on because this is an area that PowerShell really shines at it's something that a lot of administrators are now using. >> All right so you've just showed us over a decade's worth transition there so that gives us a little bit of confidence in hey, I'm not going to waste my time learning a technology or language that may vanish intermittently. But it seems like it's not really going anywhere. >> Well, that's true and now that I'm telling you here is that it's not going away. [LAUGH] No, I did kind of lead us down that road and the funny thing is, is we're on version 5.1 right now. At the time of this shooting, we're on version 5.1. So I'm running Windows 10, Anniversary update, or actually the creator's update, I think. Windows 2016 with the latest build. And when you install, you will see you have version 5.1. And 5.1, again it's kind of a subset of 5.0, right? It brought in a couple of new things. It brought in the fact we have new, now we have additions. So we have PowerShell desktop. We have also have something known as PowerShell Core. And we have, what's the other one, I guess it's just those two. Desktop corner Linux, sorry there's a Linux edition as well which is kind of cool. That means I can actually run PowerShell. There is an open source PowerShell Core for Linux. Now it's a little confusing. As I said go through this, if you go out there at the time of this shooting I'm giving you the information I have, it very well could change tomorrow, or next week. This is all very much influx, you know how Microsoft is. But at the time of this shooting, 5.1 gave us those additions, but they're now coming out with PowerShell Core. And it's PowerShell Core 6.0, but it's not Windows PowerShell 6.0. So we have Windows PowerShell 5.1 and then we have PowerShell Core 6.0, which again is a little bit confusing, but the idea is- >> We are just getting the shell there and then you can- >> It's a limited set of functionality as far as what it's built off of. It's built off instead of the complete .NET, it's built off of .NET Core which does have reduced functionality. I believe it's also because they're trying to make a cross platform, an open source. So probably- >> You may see some changes there. >> Yes. And so this is just me trying to put sense to what you read from Microsoft and their updates and their blogs and things like that. It kinda looks like 5.1 might be towards the end of Windows PowerShell. And it's just gonna be, PowerShell Core 6.0 might very well take over. And that's just me, we could end up with Windows PowerShell 6.0 and PowerShell Core 6.0. I don't know how that's going to play out but we're gonna be focusing in on Windows PowerShell 5.1 because that's what we're gonna be using and administering our Windows systems as of now. Just wanted to make you guys aware and make sure that could keep up to date. You might start seeing PowerShell Core and that's open source and it'll run on multiple platforms. It's supposed to be platform independent, it's the idea. It might not have all the functionality that we get in Windows PowerShell 5.1 but it will most likely be added in. I will tell you I've read a couple blog posts. Sometimes I don't like saying these on our shows because they could change, right? It's a person at Microsoft that's in development, but things could change tomorrow and it completely goes out the window. But, according to the I was reading, they´re slowing down the development of Windows PowerShell 5.1, and focusing all of their energy on PowerShell Core 6.0. So that is probably what we´re gonna see coming out next. >> Okay. >> We'll see. And then again that's just me guessing. >> [LAUGH] >> And you can point your finger into six months from now. Mike, you were completely wrong. >> But I can imagine that a lot of this information that we would be learning in this show can go ahead and be carried into that new. Because the syntax and the fundamentals behind it are gonna be carried through. >> Absolutely, it's still going to play a big part. And as you said, that the syntax is gonna be the same. It might not have all the commands initially but they'll be added and so we won't have any issues there. All right, so I am going to, I shall save that list in case anybody wants it out there. So we took a look at our history. Now let's take a look at PowerShell itself. And so what I want to do is we've just got a little bit of time left here in this episode. I want to talk about the different, or show you the different interfaces that we have for Windows PowerShell. And the first one I'll show you is just good old PowerShell. I've Clicked on my icon down here on my task bar. If that's not, I'd believe I pinned that there myself, so it's probably not there by default. You can always go to your Windows menu and type in PowerShell and you'll see a Windows PowerShell desktop app. And I just choose to pin that to my task bar cuz I use it a lot. All right, and there we have it. This is our PowerShell interface. >> And Mike something I learned from you a long time ago, even if you are working with core and you don't have a PowerShell window open, you can actually launch that directly from commands line, from your regular command prompt as well. >> You can, let's go take a look at that. I happen to have a core install sitting here, which has locked itself, so let me log back in. And I know that font is small, but this is core, so there's not a whole lot that I can do about it. But here as Cherokee was saying, when you log into core, you're sitting at a cmd.exe. So I'm at a normal command line interpreter, CLI window, and all I'm going to do is type in PowerShell and hit Enter and give it just a second, and we'll see it start to fire up. And there we go. Now if you look at my prompt out here, you'll see a little PS out in front of it. That's my indicator that I'm no longer at a command line. I am at a PowerShell prop. So I can start typing in my PowerShell commands, all right? So it's definitely available on core. We could remote into it as well, but we'll take a look at that in a little bit. >> And then we have the regular permissions, and then also the administrative aspect of it just like we see with our command prompt as well. >> Exactly right. Let me flip back over the client. Now when I launched this I just clicked it. If I wanted to give this administrative rights, I would right-click on that icon. And I could do the same thing from the Start menu. We could do PowerShell and I could right-click it here and choose- >> But pinning is really a good idea, especially for this show and with practicing and working with it, you're gonna be in and out all the time. >> Absolutely. >> Smart. >> I'm still. >> [LAUGH] >> I know the new window. All I have to do is hit the Windows key and start typing. I love having my little icons down. >> I'm with you, yeah. >> I don't know why, but that's just me. >> [LAUGH] >> But I can definitely right-click on it. Choose Run as Administrator. Another handy little trick that's not really PowerShell-specific, but I'll show you while we're here, is the whole Ctrl+Shift+Enter. Anything that you have highlighted. So as you start typing, I don't have Word on here, I have WordPad. As you start typing you'll see that it highlights something in your menu. So when I start typing PowerShell, it finds PowerShell. If I do Ctrl+Shift+Enter while that's highlighted it's the same thing as right clicking on it and choosing- >> Fancy. >> Run as administrator. >> [LAUGH] >> So for those of you who like to stay in your keyboard and avoid the mouse, there you go. And so there is, Mike, you can see up at the top, it says administrator, oops. And my path is a little bit different from when I launched normal PowerShell. But we'll take a look at that as we go. So, we've seen the PowerShell interface, we saw it on Core as well. Again, it's by default on 10, 2016 and also we'll take a look at the integrated scripting environment all right. So, let's take at look at PowerShell ICSM. I'm just gonna go to my Startup Menu, type in PowerShell and there I can see PowerShell ISE again, I figured to show you if I right click on it you can also launch ISE from here, or that integrated scripting environment. So I'll launch it up and let it load up. And I haven't adjusted my fonts in here. We'll do that in a little bit, and do that from my options. But this is the integrated scripting environment. And at first glance it doesn't look a lot different from PowerShell. I've got this blue background. I got my little PowerShell prompt sitting here. But it's all the other stuff that we get. First of all I got this commands pane running down the right-hand side. And this gives me access to, you guessed it, all of the commands. And so if I'm trying to find the command, I can simply search through here. I've got a dropdown list where I can go to a particular module and we're gonna talk about modules and what that means in a little bit, but I've got this handy reference to all of my commands. We'll see how I can use that in an upcoming episode. If I don't like that I can always turn that off with this button right up here, allows me to bring that commands window back or hide it. The real power of the ISE, however, is this button right here that says script. Right click on this little down arrow. You see it splits my pane and I now have this area up at the top that's a little different from my PowerShell area down at the bottom. Now I'm going to run a command and I know we haven't really gone over commands and what they are yet so just bear with me, just go, Cool and I'll make sure we go back and explain it. I'm just going to do a get-netipaddress. >> Cool. >> All right. >> [LAUGH] >> And, when I hit Enter, we can see the results show up in my pane right down here. And if I do this from PowerShell, no difference, get-netipaddress and I get this information back again. We'll come back and look at how we manage and specific commands coming up. Right now, just kinda get our feet wet with PowerShell. So I can type the command here, as well as in normal PowerShell and I get the same results. So what is this area up here? Well this is my script pane. When you type in something in PowerShell or the bottom pane of this PowerShell ISE, and you hit enter, that command executes. You saw me type in a command, hit enter, and it returned information. In the script pane up here, I do get-netipaddress and I hit Enter, noting happens. All right? This is where I start writing scripts. When we write scripts, scripts are collections of commands that together are gonna perform some kind of function. And so I can write my script up in the top and not have to worry about the commands executing. And could I do this in Notepad? Absolutely, I could write a script in Notepad if I know my commands. But that's the problem, I don't know the commands, and all the required parameters and optional parameters. By doing it in here, I can take advantage of Intellisense, and I can have it help me type my commands out. And show me the parameters that are possible, and the possible values, without having to execute as soon as I type. So also a great way to store commands. I'll show you guys that later. You'll see me use that technique in my show, because I can have a script that includes commands. And I can execute one line at a time within the PowerShell ISE. For example, let's do a get-netadapter, get-command, no that's not a good one to show you just yet, get-alias, that'll be a good one to show you. So there's three different commands, none of which have executed. Now if I click the play button up here, or F5, all three of those commands are going to execute. You see it going crazy down below? All right. >> But what if I only wanted to see the adapter, is that an option? >> Yeah, exactly. So what I could do, is I could just highlight, I don't even have to highlight, I can just click on that line and hit F8 or run selected and it will do just that one command. Notice down here in my output it didn't scroll for a minute, it just did that one command. So you might have a file, and we'll take about what this file is, the extension and things like that as we go. But I might have a file that has big long complex commands in there, and I can simply open that file up, highlight that command and then hit F8 and run just that one command. So the ISE, the integrated scripting environment, fantastic place to get started because it's gonna help me with what we call IntelliSense. It's gonna help me find my commands and type my commands. It's also where we're going to begin automating things. Where we're gonna start writing scripts, that don't execute. They execute later on and we'll take a look at how we can write those scripts and run those scripts as we progress. Last thing I want to show you. One more place we'll end up seeing PowerShell. I'm actually going to flip on over to my domain controller and open up the Active Directory Administrative Center. This was new back in 2008, I believe. I don't know if you remember. >> Back at the. >> I wanna say the Administrative Center was 2008. But this is when we really started seeing PowerShell take over in the background in a lot of our Windows products. And the administrative center, you know we had a tool. I know. I don't know about you, Cherokee, I know as a system admin, like why do I need this? >> Active directors and users in computers are probably already familiar with it, and you got to learn it and like it. >> I loved it. >> [LAUGH] >> And I still use it to this day. I find myself rarely going to the Administrative Center. But is is the new and improved Active Directory users and computers. And the big difference, everything you did in users and computers, we did in the Administrative Center, you could do it in either one. The difference was that the Administrative Center is actually firing Powershell commands in the background, rather than using old CLI-based commands. For example, if I go into lab here, and I'm going to find my users container. And, I'm just gonna make a new user, so I'll go new, user and let's do, oops. Did I spell that right? >> I don't know I can't see that far. >> Definitely not two Os. >> [LAUGH] >> Okay, that's better. >> I'm just going to make a user real quick. I got his log on name, I'll put in his password here and I think I've got all the requirements, oop I need a SAM account name. Nate and I'm gonna click Okay. And it should create that account. Right? So, nothing new, nothing fancy there. You use the GUI Mike to do that. I thought this was a Power Shell show. But down at the bottom of this active directory administrative center, you see this Windows PowerShell history? The little chevron over here that I can click on. And when I do that, I'm gonna slide this up so we can see it a little better. Here I can see all the commands that got executed. New Dash AD user, and the attributes, and the values that they got set to,and then a separate command, set dash AD account password. Because in the GUI I created the account and set the password and it was enabled automatically, right? >> So, here we can just see how that the commands are broken down, see the syntax, and like at least maybe see that relationship is coming together here between a graphical user interface and those commands there. >> Exactly right. So, if you want to learn how to create a new user in Powershell, go create one in the Administrative Center, and then come in here and look at the command. And I can copy this out, it gives us the ability to copy from here. So I can copy this command, I can take it over to the ISE, I don't have the ISE fired up on this machine. I'll fire it up real quick. I can copy that out. And I could come over here and start pasting that in. Whoops, where'd my script go? There it is. I'm clicking too fast for it. And there's my command, right? All ready to go. So now I can save that. I can start adding parameters. Take out the name Nate, for example and start making that parameter base and start automating the creation of user accounts. So, I just wanted you to see that because I think some people don't realize that's there. We see PowerShell. We see a PowerShell ISE, but don't realize that I can also see PowerShell here in the active directory of Administrative Center. Like I said, it's a great way to kind of get started with administration with PowerShell. >> Well, it sounds like there's a lot that we have to learn here, especially if we haven't worked with PowerShell before, but we really gonna be setting our self up for success by getting familiar with it and working smarter and not harder being a little more efficient by taking advantage this tool. So I'm excited about that's, thank you for joining us today Mike and thank you for joining us as well, but for this show will go ahead and sign out. I've been your host Cherokee Boose. >> And I'm Mike Rodrick. >> See you next time on ITProTV. [MUSIC] >> Thank you for watching ITProTV.

Just you? Training a whole team? There's an ITProTV plan that fits.

With more than 4,000 hours of engaging video training for IT professionals, you'll find the courses you and your team need to stay current and get the latest certifications.