kiko - Fotolia


PowerShell repository supplies a quick route to scripts

Don't have time to sift through thousands of PowerShell scripts to find one for a specific task? The PowerShell Gallery is one-stop shopping for your administrative needs.

Administrators in search of a PowerShell script to perform a specific task once had a frustrating web browser-based...

slog ahead of them. Now, they can find and pipe scripts through a dedicated repository.

Microsoft's TechNet Script Center Repository holds more than 13,000 scripts -- many of them outdated -- across several languages, such as Python and JavaScript. The effort to find a particular script could take some time. Microsoft addressed the need for a more direct way to find PowerShell scripts and modules with its PowerShell Gallery, which it released to the public in February 2016.

This PowerShell repository improves on the Script Center in several ways. For example, the PowerShellGet module connects the gallery to the user's PowerShell command-line interface; this eliminates the extra step of opening a web browser to find and download scripts.

Microsoft's engineers check all user-submitted material for malware and run each script through the PSScriptAnalyzer, a rules-based defect-checking and analysis tool, to ensure the code meets best practices.

Microsoft also provided a set of cmdlets for administrators to host scripts on its PowerShell repository. To publish scripts, users sign up for a PowerShell Gallery account to obtain an API key from Microsoft.

Get inside the PowerShell script repository

Access to the PowerShell Gallery requires the PowerShellGet module, which provides the commands to find, install and publish PowerShell scripts, modules, role capabilities and Desired State Configuration (DSC) resources.

Systems on Windows Management Framework (WMF) v5 or later should have the PowerShellGet module installed by default. Since WMF v5 is free and supported on all Windows platforms after Windows 7/Windows Server 2008 R2, administrators should upgrade to get the PowerShell improvements over earlier WMF versions.

To confirm your machine has the required cmdlets to use the PowerShell Gallery, run the Get-Command cmdlet. If cmdlets such as Find-Script or Install-Module do not appear on the list, upgrade the machine to WMF v5, or download the PowerShellGet module with this command:

Get-Command -Module PowerShellGet

Find and fetch scripts from the PowerShell repository

Once you install the necessary cmdlets, see what's available from the PowerShell repository.

The PowerShell Gallery consists of two item types -- scripts and modules -- and each has its own cmdlet verbs, such as Find, Install and Save.

Windows Server 2016 release brings opportunity, challenges in a cloud world

In this podcast, Wes Miller, research analyst at Directions on Microsoft, says PowerShell skills are "fundamental" for systems administrators who manage Windows Server.  "The longer people wait to learn it, the longer they're going to be holding themselves back from learning the latest technologies and taking advantage of the stuff Microsoft is building."

The Find-Script cmdlet retrieves a list of scripts in the repository. Without any parameters, the cmdlet shows all the available scripts in no particular order. This truncated sample lists some of them:

PS> Find-Script

Version    Name                                Repository           Description
-------    ----                                ----------           -----------
1.0.1      PSAutoMute                          PSGallery            Powershell script to Auto Mute you sound devices like  headphones, if they are accidently unplugged.
1.0.0      Invoke-BlogArchive                  PSGallery            Script to web data scrape blog articles and tweet them on twitter with Topic, a short URL and appropriate hashtags in a set interval.
1.0.0      Get-UsersOnlineOnReddit             PSGallery            Script to web data scrape reddit user trend and pump all the data points script captured from Reddit's Powershell Community page to AWS Cloud Watch (AWS monitoring sol...
1.0.0      Get-Quotation                       PSGallery            Get-Quote cmdlet data harvests a/multiple quote(s) from  Web outputs into your powershell console
1.0.1      Install-DockerOnWS2016UsingDSC      PSGallery            Installs Docker on a Windows Server 2016 server using DSC....
1.7.9      Connect-O365                        PSGallery            Connect to Office 365 and most related services and get ready to administer all services....
2.4        Enable-AzureRMDiagnostics           PSGallery            This script takes a SubscriptionID, ResourceType, ResourceGroup and a workspace ID as parameters, analyzes the subscription or...

Use the -Name parameter to narrow the results. For example, this command finds scripts related to the Docker container platform:

PS> Find-Script -Name *Docker*

Version    Name                                Repository           Description
-------    ----                                ----------           -----------
1.0.1      Install-DockerOnWS2016UsingDSC      PSGallery            Installs Docker on a Windows Server 2016 server using DSC....
0.0.3      ConvertFrom-Docker                  PSGallery            Script to parse docker output into PowerShell objects

To use the ConvertFrom-Docker script, pipe it to the Install-Script cmdlet, which then prompts the user for a download location.

PS> Find-Script -Name ConvertFrom-Docker | Install-Script -Scope CurrentUser

PATH Environment Variable Change
Your system has not been configured with a default script installation path yet, which means you can only run a script
by specifying the full path to the script file. This action places the script into the folder
'\\Mac\Home\Documents\WindowsPowerShell\Scripts', and adds that folder to your PATH environment variable. Do you want
to add the script installation path '\\Mac\Home\Documents\WindowsPowerShell\Scripts' to the PATH environment variable?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

After the download, find the script with the Get-Command cmdlet:

PS> Get-Command ConvertFrom-Docker

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
ExternalScript  ConvertFrom-Docker.ps1                                        \\Mac\Home\Documents\WindowsPowerShell...

The retrieval process is similar for modules. For example, an administrator who wants to configure and manage a domain name system (DNS) server can find and install a module that contains DSC resources for the task via the Find-Module and Install-Module commands:

Find-Module -Name xDnsServer | Install-Module

It is not required to use the Find cmdlets to download the module or script. If you know its name, skip the search portion, and run the Install cmdlet to save the right result to your machine.

Next Steps

How PowerShell automates common Active Directory tasks

Be prudent with automated PowerShell authentication

PowerShell scripts alleviate Office 365 and SharePoint administrative work

Dig Deeper on Windows administration tools