Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Installing, importing and using any module in powershell

August 26, 2017 12:25PM

With PowerShell you can achieve great way to automation using additional modules. Modules are additional functionalities or extensions to Powershell. You can also treat them as client softwares, with them you can connect to particular softwares, You will see limitation if you want additional commands and modules does not exist. After installing them you can use related .net objects or adapters. Four types are Powershell modules exist Script, Binary, Manifest, Dynamic. For more detailed information read Microsoft official article

For more on using script check Different ways to bypass Powershell execution policy :.ps1 cannot be loaded because running scripts is disabled

To demonstrate I will take example of ActiveDirectory module. To install it use, Search and open Server Manager, in the Manage, click Add Roles and Features. From Select Features menu, expand Remote Server Administration Tools, Under Role Administration Tools, Select all AD DS and AD LDS tools and Install softwares. Same way you can install other modules and tools from list ie DHCP, Hyper-V and etc, these steps are for Windows 2016 server. If you need it on Windows 10, separate RSAT tools need to be downloaded and installed.server manager, remote server administration tools, RSAT, role administration tools, Active directory module for windows Powershell, AD DS tools

Once Active Directory modules for windows PowerShell installed successfully. Same can be searched in find and open it. Module is already imported on the console. To verify the list of loaded modules run command Get-Module, ActiveDirectory module is in the list. To view what commands are associated to AD use command Get-Command -Module ActiveDirectory. Again to verify everything is OK, I am running one of the AD command Get-ADUser vkunal.

Install Active Directory Module for Windows Powershell, Get-Module, Get-ADUser, Install any module

Next step is helpful when you want to run active directory command from normal PowerShell console. By default these steps are not required for newer versions of PowerShell above v4. As whenever you run related commands ie Get-AdUser, module will automatically loaded it exists on machine. But if you want to manually import it run Import-Module ActiveDirectory. It is a best practice while writing script import module first instead of depend on auto loading of module.

If you want to list of all the installed modules you can run command Get-Module -ListAvailable.

Microsoft windows powershell, Get-module -listavailable any module, Import-Module activedirectory

There might be a scenario, your required modules will not exist in RSAT tools and needs to find it online with Find-Module. This requires active internet. It will show all community and official modules in the list from online repositories. To download required module use command Install-Module ModuleName.

Microsoft Windows Powershell, Find-Module azure, Install-Module azure -force, Internet repository psgallary service management

Module files can be directly copied to modulepath, Path can be viewed using inbuilt command $env:PSModulePath -split ';'. There are 3 default paths. 
C:\Users\username\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules

I have copy-pasted files to first location C:\Users\username\Documents\WindowsPowerShell\Modules, If path does not exist create folders. Note my module folder name and psm1 file has same name and it should be that way to Import it. If in-case you have psm1 module in another location simply provide a file path while importing it. For this step administrator rights are not required.

Microsoft Windows Powershell, module $env PSModulePath -split, Import-Module, Get-Module, GroupMembers, Default module paths

One of the other way to extend Powershell is install 3rd party software check VMWARE VSPHERE POWERCLI INSTALLATION AND CONFIGURATION STEP BY STEP.

Useful articles
Active Directory Powershell: Create bulk users from CSV file
POWERSHELL: INSTALLING AND CONFIGURING ACTIVE DIRECTORY 

Go Back

Comment