Book Image

Mastering PowerShell Scripting - Fourth Edition

By : Chris Dent
5 (1)
Book Image

Mastering PowerShell Scripting - Fourth Edition

5 (1)
By: Chris Dent

Overview of this book

PowerShell scripts offer a convenient way to automate various tasks, but working with them can be daunting. Mastering PowerShell Scripting takes away the fear and helps you navigate through PowerShell's capabilities.This extensively revised edition includes new chapters on debugging and troubleshooting and creating GUIs (online chapter). Learn the new features of PowerShell 7.1 by working with parameters, objects, and .NET classes from within PowerShell 7.1. This comprehensive guide starts with the basics before moving on to advanced topics, including asynchronous processing, desired state configuration, using more complex scripts and filters, debugging issues, and error-handling techniques. Explore how to efficiently manage substantial amounts of data and interact with other services using PowerShell 7.1. This book will help you to make the most of PowerShell's automation features, using different methods to parse data, manipulate regular expressions, and work with Windows Management Instrumentation (WMI).
Table of Contents (26 chapters)
24
Other Books You May Enjoy
25
Index

Finding and installing modules

PowerShell includes a module named PowerShellGet, which can be used to register repositories and search for and install modules.

By default, PowerShellGet searches the PowerShell Gallery.

What is the PowerShell Gallery?

The PowerShell Gallery is a Microsoft-run repository and distribution platform for PowerShell scripts and modules written by Microsoft or other users.

The PowerShell Gallery has parallels in other scripting languages, as shown in the following examples:

  • Perl has cpan.org
  • Python has PyPI
  • Ruby has RubyGems

Support for the gallery is included by default in PowerShell 5 and above. For Windows PowerShell 3 and 4, PowerShellGet must be installed as described in Microsoft Docs: https://docs.microsoft.com/powershell/scripting/gallery/installing-psget.

The PowerShell Gallery may be searched using https://www.powershellgallery.com as shown in the following screenshot:

Figure 2.2: Searching the PowerShell Gallery

You can use the Find-Module command to search the PowerShell Gallery, or any registered repository, instead of using the web page.

The Find-Module command

Find-Module is used to search registered PowerShell repositories. Modules are identified by name, as shown in the following example:

Find-Module Carbon 
Find-Module -Name Carbon 
Find-Module -Name Azure* 

You can use the Filter parameter when the name alone is not sufficient to find an appropriate module. Supplying a value for the Filter parameter is equivalent to using the search field in the PowerShell Gallery, it expands the search to include tags:

Find-Module -Filter IIS

The Find-Module command cannot filter based on PowerShell edition, and the result of the search does not state which version the module might work with.

Once found, a module can be installed using the Install-Module command.

The Install-Module command

The Install-Module command installs modules from the PowerShell Gallery or any other configured repository. By default, Install-Module adds modules to the path for AllUsers, at C:\Program Files\PowerShell\Modules on Windows and at /usr/local/share/powershell/Modules on Ubuntu.

Access rights

Installing a module under the AllUsers scope requires administrative access.

For example, the posh-git module may be installed using either of the following two commands:

Find-Module -Name posh-git | Install-Module 
Install-Module posh-git 

Modules may be installed under a user-specific path ($home\Documents\WindowsPowerShell\Modules) using the Scope parameter:

Install-Module carbon -Scope CurrentUser

If the most recent version of a module is already installed, the command ends without providing feedback. If a newer version is available, it will be automatically installed alongside the original.

The Force parameter may be used to reinstall a module:

Install-Module posh-git -Force 

Force may also be used to install a newer version of a module when the existing version was not installed from a PS repository, or when changing the scope a module is installed in.

The Install-Module command does not provide an option to install modules under the $PSHOME directory. The $PSHOME directory is reserved for modules that are shipped with the PowerShell installer, or for Windows PowerShell, those that are shipped with the Windows operating system.

The Update-Module command

You can use the Update-Module command to update any module installed using the Install-Module command.

In both Windows PowerShell and PowerShell 7, Update-Module attempts to update the specified module to the latest or specified version.

The Save-Module command

The Save-Module command downloads the module from the PowerShell Gallery (or any other registered repository) to a path without installing it. This is useful if you save a module to an alternate module path, or if you intend to copy the module onto a system that cannot use Install-Module.

The following example command downloads the Carbon module into a Modules directory in the root of the C: drive:

Save-Module -Name Carbon -Path C:\Modules 

Save-Module will download the module and overwrite any previously saved version in the specified path. The command ignores other downloaded versions of the module.

Each of the preceding commands is part of PowerShellGet 2. PowerShellGet 3 is likely to be released in the coming months and adopts a slightly different approach to the commands.