Expand the functionality of PowerShell by importing and using modules to manage Microsoft services, such as Active Directory and Exchange.
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.
Where are modules stored?
Modules are primarily stored in a system-wide directory that is accessible for all users:
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:
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"
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:
Note: Replacing the -All switch with -ListAvailable will populate all the modules available to PowerShell, not just the ones currently loaded in the session.
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.
Rerunning the Get-Module -All cmdlet will now show ActiveDirectory listed among the list of available modules currently loaded in the PowerShell session.
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
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:
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
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.
- How to automate account pre-staging in WDS with PowerShell
- 10 reasons why you should learn to use PowerShell
- 10 PowerShell commands every Windows admin should know
- 10 cool things you can do with Windows PowerShell
- 10 fundamental concepts for PowerShell scripting
Do you rely on PowerShell to help you manage Microsoft services? Share your advice and experiences with fellow TechRepublic members.