How to use nuget.exe from powershell? - powershell

I want to use the get-package command from powershell but everytime i try to use it or another nuget command, it brings up an error. How do i solve this?

NuGet.exe can be downloaded from https://www.nuget.org/downloads, but that doesn't provide the PowerShell cmdlets.
As the reference docs for NuGet's PowerShell docs say:
The commands listed here are specific to the Package Manager Console in Visual Studio, and differ from the Package Management module commands that are available in a general PowerShell environment. Specifically, each environment has commands that are not available in the other, and commands with the same name may also differ in their specific arguments.
So, NuGet's commands like Get-Package only work in Visual Studio's Package Manager Console.

Related

Why is are some NuGet commands only accessible when using Package Manager Console and seemingly not available as part of the nuget CLI?

I can use NuGet from Cmd via prefixing commands with nuget ... as one would expect.
But then it seems like there are certain commands that have to be executed explicitly from the Package Manager Console in Visual Studio. For example, Update-package -reinstall.
Why is this? Am I using two different versions of NuGet?
By NuGet CLI I take it that you mean NuGet.exe. The difference that you are noticing is by design.
The NuGet CLI is a commandline tool which supports commands like restore, pack, install etc. You need to invoke it using nuget because that is the name of the tool.
The Package Manager Console in Visual Studio is a wrapper around Powershell and the the commands are consistent with the powershell cmdlet guidelines.
It is possible that you are using different versions. You can check this by -
NuGet CLI -
Please run nuget help E.g. -
Package Manager Console -
When you launch the console in VIsual Studio, it should display the version. E.g. -

Nuget CLI not available even with VS 2017 RC3

The Nuget Distribution pages states:
NuGet 4.x is included in the Visual Studio 2017 installation. Latest
NuGet releases are delivered as part of Visual Studio updates.
Batteries are included!
It seems those batteries are flat. In both Package Manager Console and extenal PS windows, when I type nuget, I get the error:
The term 'nuget' is not recognized as the name of a cmdlet, function,
script file, or operable program.
What do I have to do to fix this? If it's installed with VS 2017, surely that install should add it to the PATH variable?
I don't want to use the VS Command Console, even if it is still with 2017, as I have retired all my usage of that relic to PowerShell consoles.
All I had to do was add the location of nuget.exe to the PATH environment variable for my user and all users, The latter is up to you, but I always run VS as Administrator.
Sages of yore used to always advise against doing this, but MS in all their divine wisdom has made life as a web developer using VS so difficult under a non-admin account, I see nary a mention of said advice, even since around VD 2010 even.
You'd think that with Nuget being installed as part of Visual Studio, the least they could do is add amend that bloody variable as part of the VS installation.

How to Import Nuget Module to powershell script [duplicate]

I was trying to use my favorite source control from the Package Manager console in Visual Studio 2010. I had issues described in another topic. For now the best answer is to move all command-line stuff from Visual Studio to bare PowerShell.
But this case the Visual Studio related commandlets are not working. For example, most crucial one — I cannot run Update-Database command from Entity Framework.
Is it possible to register somehow Visual Studio specific commandlets for the current project in PowerShell? Or make PowerShell automatically take current project context from the packages subfolder?
The NuGet PowerShell commands rely on being run from within Visual Studio so will not work outside in the normal PowerShell running from the command line.
You can however use migrate.exe which ships with the EntityFramework NuGet package and use that from the command line to update your database.
As a prototype I put together a way to use NuGet PowerShell commands from the normal PowerShell command line using SharpDevelop. Unfortunately at the moment the EntityFramework NuGet package does not work with SharpDevelop.
Another interesting project is StudioShell which provides a new DTE: drive inside Visual Studio but can also be used outside from the command line. I do not believe it supports NuGet PowerShell commands being run from the normal PowerShell command line.
I can find the NuGet.psd1 file at:
C:\Program Files (x86)\Microsoft Visual Studio
12.0\Common7\IDE\Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1.
However, when you try to load it:
PS> Import-Module $pathToNuGetPsd1 -Force -NoClobber -Scope Global
Import-Module : The name of the current Windows PowerShell host is: 'ConsoleHost'.
The module 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\
Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1' requires the following Windows
PowerShell host: 'Package Manager Host'.
I think we're out of luck. It has to be run from the Package Manager Host and requires things from Visual Studio as stated by Matt.
To solve my problem I used Chocolatey to install NuGet.CommandLine and then used NuGet.bat to do what I needed. It is a little more work and may not work in all cases depending on what you're trying to do.
Chocolatey: https://github.com/chocolatey/chocolatey/wiki/Installation
NuGet.CommandLine:
PS> cinst NuGet.CommandLine
I'm not terribly familiar with the Visual Studio cmdlets, but you can import a module into your PowerShell session by using Import-Module -Name <ModuleName>. You can list the available (aka. "installed") PowerShell modules by using `Get-Module -ListAvailable'.
My guess would be that the Visual Studio cmdlets are contained with its own PowerShell module, but it's quite possible that it's not "installed" to one of the standard locations in $env:PSModulePath. If this is the case, then you might need to locate the module directory and import either the .psd1 or .psm1 file directly, and pass that into: Import-Module -Name <FullPathToModuleFile>.
As an example of the above, take notice of where the Windows Azure PowerShell module is located: http://trevorsullivan.net/2012/06/07/introducing-microsofts-official-windows-azure-powershell-module/
It's under the Program Files directory, and is not immediately available to PowerShell, unless you import the module from its fully qualified path (the .psd1 module manifest file).
Hope this helps.

Is there a way to make the VS Package Manager commands available on Powershell Console?

Is there a way to make the VS Package Manager commands available on Powershell Console? I looked over this link but I was unable to understand the NuGet philosophy.
I know that I can access it in Visual Studio but, let me explain my approach and goal.
I want to learn F# and for this use a lot its REPL. So basicaly, I open a Powershell console, change to my working folder, create a file with some code, fire the REPL, try various things.
In this kind of loop makes sense to have means to install various packages without starting VS and having to create a VS project.
Thanks!
Please refer to below screenshots:
The problem is that the PowerShell cmdlets that NuGet makes available inside Visual Studio rely heavily on Visual Studio so they are not supported outside of Visual Studio from the command line.
From the command line you have NuGet.exe which supports the following which are project related:
Downloading NuGet packages. Basically restoring the missing NuGet packages as defined in a packages.config file. NuGet.exe install or NuGet.exe restore
Updating a NuGet package in a project. This will update the reference information but not run PowerShell scripts. NuGet update
Having the NuGet PowerShell cmdlets available on the command line is something I have looked at doing using the cmdlets that SharpDevelop provides. It is possible to fully support NuGet install/update/uninstall and have the PowerShell scripts execute on the project but in this case it requires an entire IDE, in this case SharpDevelop, to be available, at least its assemblies, even though the IDE is not run. This is needed to support updating the project file using the Visual Studio project model (EnvDTE) from within any PowerShell scripts that run.
Nuget commands cannot be used outside of Visual Studio. However, if you were change to use Paket instead of Nuget, then you can use Paket from a command line outside of Visual Studio to manage your project's packages.
The getting started page is here.

Can't use out-gridview since installing PowerGUI with Powershell V3

Just wondering if anyone seen this and knows a workaround.
I'm Running Windows 7 64bit and just upgraded to Powershell V3. Since then, i can't use out-gridview commands. I get the following error.
To use the Out-GridView cmdlet, install the Windows PowerShell Integrated Scripting Environment feature from Server Manager. (Could not load file or assembly '
Microsoft.PowerShell.GraphicalHost, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file sp
ecified.)
I have opened the native MS ISE and can run out-gridview withgout error.
PowerGUI Script Editor launches using PowerShell v.2, even though you have PowerShell v.3 installed. Launching Script Editor with the "-version 3.0" option resolved this problem for me.
There are two things you can check:
When you installed PowerShell V3, did you also make sure to install the PowerShell Integrated Scripting Environment (ISE).
Make sure you have Microsoft .NET Framework v3.5 with Service Pack 1 installed also.
Source of answer
I fixed it by changing -Version 2.0 to -Version 3.0
Right click on Exchange Management shell and choose properties. Find above in TARGET.