-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
By :
The PowerShellGet module enables you to work with repositories, sites which contain scripts and modules to download and use. If you have a Linux background, you are familiar with repositories and tools like apt-get (On Ubuntu Linux) and RPM (on Red Hat Linux). PowerShellGet delivers similar functionality within PowerShell.
Ensure you're running with administrator privileges so you can update PowerShellGet to the latest version.
PowerShellGet module: Get-Command -Module PowerShellGetNuGet to get the PackageManagement module current, then update the PowerShellGet module per the GitHub instructions at https://github.com/powershell/powershellget.PowerShellGet has a dependency on PackageManagement, which in turn relies on NuGet. PowerShellGet and PackageMangagement both come within Windows 10 and Server 2016, but Windows updates are less frequent than releases at the PowerShell gallery. Updating ensures you have the latest versions of all the dependencies. To update NuGet: Install-PackageProvider -Name NuGet -Force -VerboseExit and open a new PowerShell session.NuGetPackageProvider:Get-PackageProvider -Name NuGet | Select-Object Version
PowerShellGet: Install-Module -Name PowerShellGet -ForceExit and reopen it again.PowerShellGet:Get-Module -Name PowerShellGet | Select-Object -ExpandProperty Version
PSGallery repository for PowerShellGet: Get-PSRepositoryFind-PackageProvider | Select-Object -Property Name, Source, Summary | Format-Table -Wrap -AutoSize
PSGallery:Find-PackageProvider -Source PSGallery | Select-Object -Property Name, Summary | Format-Table -Wrap -AutoSize
Get-Command cmdlet to find cmdlets in PowerShellGet: Get-Command -Module PowerShellGet -Verb FindPowerShellGet module, store them in a variable, and store the count as well:$CommandCount = Find-Command | Tee-Object -Variable 'Commands' | Measure-Object "{0} commands available in PowerShellGet" ` -f $CommandCount.Count
Out-GridView and note the module names: $Commands | Out-GridViewPowerShellGet modules, store them in a variable and store the count as well:$ModuleCount = Find-Module | Tee-Object -Variable 'Modules' | Measure-Object "{0} Modules available in PowerShellGet" -f $ModuleCount.Count
Out-GridView: $Modules | Out-GridViewOut-GridView:$DSCResourceCount = Find-DSCResource | Tee-Object -Variable 'DSCResources' | Measure-Object "{0} DSCResources available in PowerShellGet" -f ` $DSCResourceCount.Count $DSCResources | Out-GridView
Out-GridView:$ScriptCount = Find-Script | Tee-Object -Variable 'Scripts' | Measure-Object "{0} Scripts available in PowerShellGet" -f $ScriptCount.Count $Scripts | Out-GridView
Scripts, DSCResources, and so on: Get-Command -Module PowerShellGet -Verb InstallTreeSize module, as an example, or choose your own. As this is a public repository, Windows does not trust it by default, so you must approve the installation: Install-Module -Name TreeSize -VerboseInstallationPolicy to Trusted, and you'll no longer need to confirm each installation: Use at your own risk, you are responsible for all software you install on servers you manage: Set-PSRepository -Name PSGallery -InstallationPolicy TrustedGet-Command -Module TreeSize Get-Help Get-TreeSize -Examples Get-TreeSize -Path $env:TEMP -Depth 1
Uninstall-Module -Name TreeSize -VerboseNew-Item -ItemType Directory ` -Path $env:HOMEDRIVE\downloadedModules Save-Module -Name TreeSize ` -Path $env:HOMEDRIVE\downloadedModules” + "$env:windirexplorer.exe" $env:HOMEDRIVE\downloadedModules
$ModuleFolder = "$env:HOMEDRIVE\downloadedModules\TreeSize" Get-ChildItem -Path $ModuleFolder -Filter *.psm1 -Recurse | Select-Object -ExpandProperty FullName -First 1 | Import-Module -Verbose
Remove-Module -Name TreeSize $ModuleFolder | Remove-Item -Recurse -Force
In step 1, you start by reviewing the cmdlets in the PowerShellGet module:

In steps 2-7, you ensure PowerShellGet and its dependency PackageManagement are up to date by updating the NuGet provider, verifying the version, then restarting your PowerShell session and updating PowerShellGet and verifying its version.

The -Verbose flag gives you more details on the installation, but it is not required. Note that you must Exit your session after running this command and reopen to continue with the latest version.
Check our NuGet provider version after reopening our PowerShell session:

In step 6-7, you update the PowerShellGetmodule:

Note that you must exit your session after running this command and reopen to continue with the latest version.
In step 8, check your PowerShellGet version after reopening your PowerShell session:

In step 9, you use Get-PSRepository. PowerShellGet starts with a single repository PSGallery installed by default:

In step 10, review the package providers available:

Note the source column; the first three providers listed correspond to NuGet, OneGet, and Chocolatey providers. NuGet is a repository devoted to developer libraries. OneGet was the name of this module (and repository) but has been deprecated and replaced by PackageManagement. You explore Chocolatey in a later recipe. The remaining rows are the available providers in the PSGallery repository.
In step 11, you limit your repository search with Find-PSRepository by specifying the -Source PSGallery parameter:

In step 12, you discover the PowerShellGet commands containing the verb Find:

In steps 13 - 18, you use the Find-* commands to store the available commands, modules, DSC resources, and scripts into variables, then explore what is available using Out-GridView (including using the built-in filter capability to search for a module), for example:

In step 19, you review the install commands in the PowerShellGet module. Their functions are very similar:

In step 20, the TreeSize module looks like an interesting tool to inspect folders and their sizes. Install it by using the Install-Module cmdlet. You use the -Verbose switch to get more information about what the cmdlet is doing:

After confirming the Untrusted repository pop up dialog, PowerShell installs the module.
In step 21, you see that the code available on PSGallery, as well as other public repositories, is just that, public. You must choose to trust the code you download from the internet to take advantage of the functionality provided by that code. To trust this repository and disable prompting, use the command (at your own risk and responsibility):
Set-PSRepository -Name PSGallery -InstallationPolicy TrustedIn step 22, you evaluate and test the module:

In step 23, uninstalling a module is simple:

In step 24, if you prefer, download code and inspect it before installing, using Save-Module, then browse the module's files in Windows Explorer:

In step 25, after reviewing the code, import the module by locating the .psm1 file which defines the module, using Get-ChildItem, then piping that filename to Import-Module:

In step 26, you uninstall the module from your session and delete the module's folder. You may, of course, wish to keep the module!
There are a wealth of other resources in the PSGallery—you use the Find-* cmdlets to explore the online resources you can download and use:

The PowerShellGet module enables search for commands, DSC resources, modules, role capabilities, a feature of Just Enough Administration (JEA), and scripts. You can download and use these various tools, or leverage them to build your own custom scripts.
Change the font size
Change margin width
Change background colour