This video shows how to make your complex PowerShell scripting tasks easier by importing modules from Windows features, RSAT, and online repositories.
- [Instructor] One of the primary benefits of writing scripts is to make complex tasks easier. What we don't want to do is to make easy tasks more complicated. One way that some people make easy tasks more difficult is by creating their own cmdlets or functions when they already exist. It's worth taking a moment to look for a cmdlet that you think should exist. In my course on learning PowerShell, I have a section specifically about importing and installing modules.
Here I show you how to find and install modules and how to import them so that the cmdlets in them become available. What I want to focus on here is how to find the modules that you might need for your scripts. I've often used Landon Hotel as a sample company in my courses, and Landon Hotel has an in-house restaurant that has been owned and operated by a separate company running under the name of Landon Cafe.
Let's say the two are in the middle of a merger and you as administrator need to move all users from the Landon Cafe active directory to the Landon Hotel directory. Both of these networks use Microsoft's Active Directory as their LDAP. So one of the things that you're going to need to do is create new AD users. There's a command for that. The cmdlet and the module that contains it are not however installed by default on every Windows machine, not even every Windows server.
Here we have a server that is a member of the Landon Cafe domain, but not a domain controller. Active Directory domain services is not installed. If I were to open PowerShell, I could look for cmdlets that are involved in managing users. If I type Get Command, and let's go ahead and put in a wild card. Anything that has the word user at the end of the cmdlet should show up in this list.
And when I run this, I get several local user cmdlets, but nothing about Active Directory. Now it seems unlikely that Microsoft would neglect to create such a cmdlet. In fact, they did create it. They put it in a PowerShell specifically for Windows Server roles, a role that's not installed here. If I was on a Windows Workstation, I could browse out and search for Rsat or remote server administration tools, download and install them.
Or since I'm on a server, I could go back to the server manager, under Manage, select Add Roles and features. And let me go through this process until we get to the point where we're selecting features, not roles but features. Scroll down in this list. Here are remote server administration tools. Role administration tools because Active Directory is a role. And I can go ahead and check the box for Active Directory domain services and the other lightweight directory services tools that may be available.
I'm going to go ahead and finish this installation. It shouldn't take very long as it goes out and retrieves the information and adds these tools to the server. You'll notice that Active Directory module for Windows PowerShell is listed right here as one of the things that we're getting as part of this feature install. Now that that installation is complete, let me go ahead and close out of this. I'm going to close PowerShell and reopen it so that it will see all newly installed and loaded modules.
And here, if I once again type Get Command with the wild card for User cmdlets, we now find Get and New and Set ADUser cmdlets have been added to our list. Well that process is all well and good for finding the cmdlets that Microsoft has written for administering various Windows features. But there are a lot of administrators and developers out there with tasks that may not be all that different from mine. It's reasonable to think that someone somewhere has created functions that could be used in my scripts.
If you want to search for functions and scripts that are already created, a good place to start is PowerShellGallery.com. If I search this site for Active Directory, I'm going to get several hits. As of today, it's returning about 62 items. Some of them are about Azure. Others are about various other features of Active Directory, including pieces on DSC, Desired State Configuration, and other things that you may need to find.
As you start your scripts, look for modules that contain cmdlets that you will need and functions that you may want. These functions created and shared by others can be added in at least a few different ways. And as we move forward, we'll take a look at the most common ways to do this.
- Creating a PowerShell script
- Creating scripts in IDE or Visual Studio Code
- Working with files
- Loading modules
- Using functions
- Managing servers with WMI
- Adding users to a domain