Networking

How to use PowerShell modules to knock out your admin chores

Expand the functionality of PowerShell by importing and using modules to manage Microsoft services, such as Active Directory and Exchange.

hero
Image: Microsoft
Anyone who has been managing Windows systems for the last decade has no doubt heard of PowerShell (PS) and knows what the modernized shell has to offer. PS is now rounding out its fifth stable version with many more management and scripting capabilities under the hood.

From basic systems management to Active Directory (AD DS), PowerShell does it all. And it achieves this through the use of specialized cmdlets, called modules. They act as self-contained, reusable units of PS code that execute from the PowerShell environment in their own context. This means you could effectively manage Active Directory, for example, by loading the module and making any changes necessary, then simply unloading the module so that one management console session does not affect another inadvertently.

SEE: How to manage Group Policy updates locally and remotely

Where are modules stored?

Modules are primarily stored in a system-wide directory that is accessible for all users:

%WINDIR%\System32\WindowsPowerShell\v1.0\Modules

Additional directories may store modules, such as the %USERPROFILE% and %PROGRAMFILES%, since applications installed, such as MS SQL, will typically include their own set of modules for SQL database management.

Entering the following command will display the location of each directory where modules are stored:

$env.PSModulePath

Figure A

Custom directories may also be used to stored modules by entering the command below. This will enable PS to use that location for loading modules during the session only.

$env.PSModulePath = $env.PSModulePath + ";PATH_TO_DIRECTORY"

Figure B

What modules are loaded in PowerShell?

By default, the modules that are loaded with PS allow for basic management using system-related cmdlets. Specialized cmdlets for managing AD DS require individual modules to be loaded, which I'll address in the next section.

To list what modules are currently loaded in PowerShell, enter this command:

Get-Module -All

Figure C

Note: Replacing the -All switch with -ListAvailable will populate all the modules available to PowerShell, not just the ones currently loaded in the session.

SEE: The 3 reasons why Azure and cloud are the heart of the new Microsoft

How do I import modules?

Importing modules is where the administrator can selectively manage a particular application or service during the session without having it affect PS each time it is launched. To import modules, enter the command:

Import-Module -Name ModuleName

Note: Though optional, adding the -Verbose switch to the end of the cmdlet will display each cmdlet being processed, as shown below.

Figure D

Rerunning the Get-Module -All cmdlet will now show ActiveDirectory listed among the list of available modules currently loaded in the PowerShell session.

Figure E

What cmdlets can I use with a module?

Luckily, PowerShell has a lot of built-in help, examples, and lists available for rookie and veteran admins alike. With the sheer amount of cmdlets present, it's not uncommon to forget a few. To view a list of available cmdlets for a given module, enter this command:

Get-Command -Module ModuleName

Figure F

Drilling down further, if you wish to view the available syntax and options for a specific cmdlet within a module with the New-ADComputer cmdlet, enter the following command:

Get-Help Cmdlet

Figure G

How do I unload an imported module?

If you're done with your session, merely exiting out of PowerShell and closing the window will unload any module(s) recently imported. However, if you will continue to work with PowerShell but have finished work on a particular module, entering the command below will unload only the module specified while keeping the session open:

Remove-Module -Name ModuleName

Figure H

Since there is no confirmation that the module has been unloaded, running Get-Module -All cmdlet will display the list of currently imported modules, confirming the ActiveDirectory module is no longer displayed.

PowerShell is included with every supported version of Windows—client or server—and can always be updated to the latest version by updating the Windows Management Framework to ensure that your shell fully supports all modules and cmdlets.

Also read...

Your take

Do you rely on PowerShell to help you manage Microsoft services? Share your advice and experiences with fellow TechRepublic members.

About Jesus Vigo

Jesus Vigo is a Network Administrator by day and owner of Mac|Jesus, LLC, specializing in Mac and Windows integration and providing solutions to small- and medium-size businesses. He brings 19 years of experience and multiple certifications from seve...

Editor's Picks

Free Newsletters, In your Inbox