Saturday, April 11, 2020

PowerShell: Install-Module and Uninstall-Module

In order to automate Chrome-based DevOps tasks, Selenium and PowerShell will be used. The site, PowerShellGallery, contains a variety of PowerShell modules that can be installed including Selenium for PowerShell, https://www.powershellgallery.com/packages/Selenium. The cmdlets used to install and uninstall PowerShell modules are Install-Module and Uninstall-Module respectively.

Clicking on the abo e link displays the following (note; the version number will change depending on the current version number where at the time of this blog entry being written is 3.0.0):


Install-Module: User Scope

The installation instructions shown on PowerShellGallary.com's Selenium page make use of Install-Module cmdlet which can be run from PowerShell:

Install-Module -Name Selenium

Another way to run the Install-Module cmdlet is from a Visual Studio Code Terminal window set to PowerShell versus BASH:


Running Install-Module defaults to user scope meaning the Selenium module for PowerShell (Selenium.psm1) will be installed at the following location:

C:\Users\<user name>\Documents\PowerShell\Modules\Selenium\3.0.0

To import this module (Selenium.psm1) into a PowerShell script, the following code could be used:

[string] $UserModules = 
             "$HOME\Documents\WindowsPowerShell\Modules"
Import-Module "$UserModules\Selenium\3.0.0\Selenium.psm1"


In the previous code, $HOME is an automatic variable in PowerShell defined on How-to: Automatic Variables as containing:


The Import-Module cmdlet imports the Selenium.psm1 module into the current session so that functions and classes exposed by the module can be used.

Install-Module: All Users

The environment where I develop automation scripts for uses multiple "Bot" accounts each corresponding to a different user on Windows. Installing a module with per-User scope (the default scope) would be problematic. To install PowerShell module (such as Selenium) for use by the all user scope, use the -Scope AllUsers parameter as follows:

Install-Module -Name Selenium -Scope AllUsers

An example of Visual Studio Code's Terminal window during the install of Selenium for all users is a follows:


When the Selenium or any module is installed at all users scope, there is no need to use the Import-Module cmdlet in order to access the installed module.

An example of using the Selenium.psm1 module is as follows and notice that the Import-Module cmdlet is not required due to Selenium being installed at under the all user scope:

$Driver = Start-SeChrome
Enter-SeUrl https://www.google.com -Driver $Driver


The previous PowerShell code launches an instance of Chrome and navigates the Chrome browser to the URL: https://www.google.com.

Uninstall-Module

Uninstalling a module is performed using the Uninstall-Module cmdlet. An example of the  Uninstall-Module cmdlet being used to uninstall the Selenium PowerShell module is follows:

Uninstall-Module -Name Selenium




No comments :

Post a Comment