A use for default parameters – default powershellget repository

When you use Find-Module by default all repositories are searched

£> Find-Module -Name Pester | ft Version, Name, Repository -a

Version Name   Repository
——- —-   ———-
3.2.0   Pester PSGallery
3.2.0   Pester PowerShellModules

If you don’t give a module name that could be a lot of data to sort through. If you are running an internal repository you may want to check that repository first and only use other repositories if that one doesn’t contain the module.

£> Find-Module -Name Pester -Repository PowerShellModules  | ft Version, Name, Repository -a

Version Name   Repository
——- —-   ———-
3.2.0   Pester PowerShellModules

This means that you have to type the repository name each time. It would be better if you could make a particular repository the default.  One way to do this is to define default parameters. This functionality was introduced in PowerShell 3.0

£> $PSDefaultParameterValues.Add(“Find-Module:Repository”, ‘PowerShellModules’)
£> $PSDefaultParameterValues

Name                           Value
—-                           —–
Find-Module:Repository         PowerShellModules

£> Find-Module -Name Pester | ft Version, Name, Repository -a

Version Name   Repository
——- —-   ———-
3.2.0   Pester PowerShellModules

You can override the default if you wish

£> Find-Module -Name Pester -Repository PSGallery  | ft Version, Name, Repository -a

Version Name   Repository
——- —-   ———-
3.2.0   Pester PSGallery

You can do the same for install-module so that it will default to your internal repository

£> $PSDefaultParameterValues.Add(“Install-Module:Repository”, ‘PowerShellModules’)

£> Install-Module -Name Pester -Force -Verbose
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules’;
IsTrusted = ‘True’; IsRegistered = ‘True’.
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules’;
IsTrusted = ‘True’; IsRegistered = ‘True’.
VERBOSE: Using the specified source names : ‘PowerShellModules’.
VERBOSE: Getting the provider object for the OneGet Provider ‘NuGet’.
VERBOSE: The specified Location is ‘http://localhost:81/nuget/PowerShellModules’ and OneGetProvider is ‘NuGet’.
VERBOSE: In PSModule Provider – ‘Get-InstalledPackage’.
VERBOSE: The specified Location is ‘NuGet’ and OneGetProvider is ‘NuGet’.
VERBOSE: Downloading module ‘Pester’ with version ‘3.2.0’ from the repository
http://localhost:81/nuget/PowerShellModules’.
VERBOSE: NuGet: Installing ‘Pester 3.2.0’.
VERBOSE: NuGet: Successfully installed ‘Pester 3.2.0’.
VERBOSE: Module ‘Pester’ was installed successfully.

-Force was used as the module is already installed.

Your default parameters now look like this

£> $PSDefaultParameterValues

Name                           Value
—-                           —–
Find-Module:Repository         PowerShellModules
Install-Module:Repository      PowerShellModules

Add the two lines

$PSDefaultParameterValues.Add(“Find-Module:Repository”, ‘PowerShellModules’)

$PSDefaultParameterValues.Add(“Install-Module:Repository”, ‘PowerShellModules’)

To your profile and your defaults will be available every time you start PowerShell

This entry was posted in PowerShell v5, PowerShellGet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s