APPENDIX C

image

NuGet Package Manager Console PowerShell Reference

Throughout this book, you’ve seen the NuGet Package Manager Console making use of PowerShell. Although those chapters already covered the most important NuGet commands, this appendix lists all available NuGet commands exposed in the NuGet Package Manager Console.

This appendix builds on top of the contents of this book and will provide references to other parts of this book as well. However, this appendix is not meant to be a full PowerShell reference; there are plenty of good books on that topic.

Parts of this appendix are based on the information available on the official NuGet web site at http://docs.nuget.org/docs/reference/package-manager-console-powershell-reference. The NuGet documentation (from the Outercurve Foundation) is licensed under Creative Commons license BY 3.0 (http://creativecommons.org/licenses/by/3.0/).

Support for Common Parameters

Note that the PowerShell commands listed in this appendix all support the following set of common PowerShell cmdlet parameters, indicated by the [<CommonParameters>] option:

  • Verbose
  • Debug
  • ErrorAction
  • ErrorVariable
  • WarningAction
  • WarningVariable
  • OutBuffer
  • OutVariable

For more-detailed information, you can simply type Get-Help About_CommonParameters in the NuGet Package Manager Console if the PowerShell help documents are installed on your system.

Adding Binding Redirects

This command adds binding redirects to the app.config or web.config file:

Add-BindingRedirect [-ProjectName] <string>

It has not been explicitly mentioned earlier in this book because, as of NuGet v1.2, this command is run automatically when needed during package installation. The available parameter for this command is shown in Table C-1.

Table C-1. Available Option for the NuGet Add-BindingRedirect Command

Option Description Required
ProjectName Specifies the project to analyze and  add binding redirects to. True

Getting a Package or a Set of Packages

This command gets a package or a set of packages available from the package source:

Get-Package -Source <string> [-ListAvailable] [-Updates] [-ProjectName] [-Filter•
 <string>] [-First <int>] [-Skip <int>] [-IncludePrerelease] [-AllVersions]

This command defaults to showing only the list of installed packages. Use the -ListAvailable flag to list packages available from the package source. Available options for this command are shown in Table C-2.

Table C-2. Available Options for the NuGet Get-Package Command

Option Description Required
Source Specifies the URL or directory path for the package source containing the package to install. When set to a local file system path, Source can be either absolute or relative to the current directory. If omitted, NuGet looks in the currently selected package source to find the corresponding package URL. No
ListAvailable Gets packages available from the online package source. False
ProjectName Specifies the project to get installed packages from. If omitted, the command will return installed packages for the entire solution. False
Updates Gets the latest versions of packages that have an update available from the package source. False
Filter Specifies a filter string used to narrow down the list of packages returned. The filter is searched for in the package ID, the description, and tags. False
First Specifies the number of packages to return from the beginning of the list. False
Skip Skips (does not return) the specified number of packages, counting from the beginning of the list. False
IncludePrerelease Indicates whether to include prerelease packages in the returned results. False
AllVersions By default, only the latest version of a package is shown. This option displays all available versions of a package. False

Using this command, you can easily loop through all installed packages on the MyProject project. The following command prints all installed package IDs for the current project into the Package Manager Console window:

Get-Package -ProjectName MyProject | % { Write-Host "$_.Id is installed" }

It can also be used to search for packages. The following example searches for the jQuery package from all configured package sources:

Get-Package -ListAvailable -Filter jQuery

The package source that should be queried can be specified explicitly. The following example searches for the jQuery package from the www.myget.org/F/pronuget feed:

Get-Package -ListAvailable -Filter jQuery -Source https://www.myget.org/F/pronuget

Getting Project Information

This command gets the specified project. If none is specified, it returns the default project (which is selected from the drop-down in the Package Manager Console toolbar). The project is returned as an EnvDTE.Project instance.

Get-Project [[-Name] <string>] [-All] [<CommonParameters>]

Available parameters for this command are shown in Table C-3.

Table C-3. Available Options for the NuGet Get-Project Command

Option Description Required
Name Specifies the project to return. If omitted, the default project selected in the Package Manager Console is returned. False
All Returns every project in the solution that supports NuGet. False

An example of calling Get-Project is shown in Figure C-1; it illustrates the output including the ProjectName, Type, and FullName properties of the object.

9781430260011_AppC-01.jpg

Figure C-1. Example output of the Get-Project cmdlet in NuGet Package Manager Console

This command opens up quite a few interesting opportunities to further automate the way we work with NuGet, and by extension, Visual Studio.

An example use case could be to install a package in multiple target projects at once, based on some selection criteria. For instance, let’s say we want to install the NUnit package into all of our testing projects, which are named using the convention *Tests. This is not something you can easily achieve by using the user interface dialog boxes of the NuGet Visual Studio extension. Smart usage of the Get-Project command, however, allows you to achieve this very easily, as shown in Listing C-1.

Listing C-1.  Installing a Package in Selected Target Projects by Using the Get-Project Cmdlet

Get-Project -All | Where { $_.Name.EndsWith("Tests ") } | Install-Package NUnit

When combining the Get-Project command with the Get-Package command, you could also list all installed packages for each project. Listing C-2 shows you how you can do this by using a single line in PowerShell.

Listing C-2.  Listing All Installed Packages for All Projects in the Current Visual Studio Solution

Get-Project -All | % { Write-Host $_.ProjectName; Get-Package -ProjectName •
$_.ProjectName | % { Write-Host "  $_.Id is installed" } }

Installing Packages

This command installs a NuGet package and, by default, its dependencies into the target project:

Install-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [[-Version]
 <string>] [[-Source] <string>] [-IncludePrerelease] [-FileConflictAction] [<CommonParameters>]

Available parameters for this command are shown in Table C-4.

Table C-4. Available Parameters for the NuGet Install-Package Command

Option Description Required
Id Specifies the ID of the package to install. True
IgnoreDependencies Installs only this package and not its dependencies. False
ProjectName Specifies the project to install the package into. If omitted, the default project is chosen. False
Version Specifies the version of the package to install. If omitted, defaults to the latest version. False
Source Specifies the URL or directory path for the package source containing the package to install. When set to a local file system path, Source can be either absolute or relative to the current directory. If omitted, NuGet looks in the currently selected package source to find the corresponding package URL. False
IncludePrerelease Indicates whether this command will consider prerelease packages. If omitted, only stable packages are considered. False
FileConflictAction Specifies the action to take, when asked to overwrite or ignore existing files referenced by the project. Possible values are Overwrite, Ignore, and None. None is the default value. Note that by default, the Install-Package cmdlet will prompt for every file where a conflict has been encountered during package installation. False

Opening Package Pages

This command will open the browser pointing to ProjectUrl, LicenseUrl, or ReportAbuseUrl of the specified package.

Open-PackagePage -Id <string> [-Version] [-Source] [-License] [-ReportAbuse] [-PassThru]•
 [<CommonParameters>]

Available parameters for this command are shown in Table C-6.

Table C-6. Available Parameters for the NuGet Open-PackagePage Command

Option Description Required
Id Specifies the ID of the NuGet package to search for. False
Version Specifies the version of the package to search for. If omitted, defaults to the latest version. False
Source Specifies the source of the repository to search for the package. If omitted, defaults to the selected source in the package source drop-down control. False
License Indicates that the cmdlet should open the LicenseUrl of the specified package. If neither LicenseUrl nor ReportAbuseUrl is set, the cmdlet will open the ProjectUrl by default. False
ReportAbuse Indicates that the cmdlet should open the ReportAbuseUrl of the specified package. If neither LicenseUrl nor ReportAbuseUrl is set, the cmdlet will open the ProjectUrl by default. False
PassThru If specified, the cmdlet will return the value of the requested URL. False

Uninstalling Packages

This command uninstalls a NuGet package. If other packages depend on this package, the command will fail unless the –Force option is specified.

Uninstall-Package [-Id] <string> [-RemoveDependencies] [-Force] [-Version <string>]•
 [-ProjectName <string>] [<CommonParameters>]

Available parameters for this command are shown in Table C-7.

Table C-7. Available Parameters for the NuGet Uninstall-Package Command

Option Description Required
Id Specifies the package ID of the package to uninstall. True
RemoveDependencies Uninstalls the package and its dependencies that are not depended on by other packages. False
Force Forces uninstalling this package and its dependencies, whether they are used by other packages or not. This is the case only if -RemoveDependencies is set. False
Version Indicates the version of the package to uninstall. If omitted, defaults to the latest version. False
ProjectName Specifies the project to uninstall the package from. If omitted, the default project that is selected from the drop-down in the Package Manager Console is chosen. False

Updating Packages

This command updates a package and its dependencies to a newer version:

Update-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [-Version•
 <string>] [-Safe] [-Source <string>] [-IncludePrerelease] [-FileConflictAction] • [-Reinstall] [<CommonParameters>]

Available parameters for this command are shown in Table C-8.

Table C-8. Available Parameters for the NuGet Update-Package Command

Option Description Required
Id Specifies the package ID of the package to update. If omitted, every package is updated. False
IgnoreDependencies By default, only the package specified is updated. Adding the -IgnoreDependencies switch updates all of the package’s dependencies as well. False
ProjectName Specifies the project containing the package to update. If omitted, the package is updated in every project with the package installed. False
Version Specifies the version that the package will be upgraded to. If omitted, defaults to the latest version. False
Safe Constrains upgrades to newer versions with the same major and minor version components. For example, if version 1.0.0 of a package is installed, and versions 1.0.1, 1.0.2, and 1.1 are available in the feed, the -Safe flag updates the package to 1.0.2. False
Source Specifies the URL or directory path for the package source containing the packages to update. When set to a local file system path, Source can be either absolute or relative to the current directory. If omitted, NuGet looks in the currently selected package source to find the corresponding package URL. False
IncludePrerelease Indicates whether to include prereleases when searching for updates. If omitted, only stable packages are considered. False
FileConflictAction Specifies the action to take, when asked to overwrite or ignore existing files referenced by the project. Possible values are Overwrite, Ignore, and None. Note that by default, the Update-Package cmdlet will prompt for every file where a conflict has been encountered during package installation. False
Reinstall Reinstalls the currently installed package versions. This can be very useful, for example, when switching target frameworks or when a project file is broken because of NuGet packages. The reinstall switch reinstalls the current version of the package; its dependencies may change depending on the version constraints specified in the depending package. False

Chaining PowerShell Cmdlets

By chaining cmdlets, we can perform some very nice operations in our projects. How about retrieving all distinct license URLs for all packages that have been installed in a solution?

Get-Package | Open-PackagePage -License -WhatIf -PassThru | %{ $_.OriginalString }

Or maybe we want to create a list of all packages installed in our solution?

Get-Package | %{ $_.Id + " " + $_.Version } | Select-Object -unique
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.37.196