Search code examples
c#powershellvisual-studionugetpackage-manager-console

Save Install-package output from package manager console into file


I have the following simple shell command:

PM> ipconfig | Out-File -FilePath c:\log.txt 

This command works fine, the content of log.txt is successfully added into log.txt file.

However it doesn't work for Install-Package command, the below command doesn't save output into the provided file:

PM> Install-Package Newtonsoft.Json  | Out-File c:\log2.txt 

Attempting to gather dependency information for package 'Newtonsoft.Json.13.0.3' with respect to project 'ConsoleApp5', targeting 
...

The log2.txt is created, but it has no content inside. How to save all possible content (especially from Package Manager Console) into the file?


Solution

  • They are different.

    Please take a look of the official document of Out-File:

    Description of the Out-File

    enter image description here

    ipconfig is a command which writes its output directly to the standard output stream (stdout), which is easily redirected to a file using Out-File. However, Install-Package (a cmdlet from the NuGet Package Manager Console in Visual Studio) doesn't write its output in the same manner.

    Due to the principle of obtaining information from Out-File, you cannot obtain and output any information at all by doing this. Because this powershell command not only does not output to the standard output stream, it does not even output to the standard error stream. The following command can confirm this:

    Install-Package Newtonsoft.Json > output.log 2> error.log
    

    Therefore, using technologies based on stdout or stderr cannot achieve your needs.

    What you can use is powershell's own text capture command to output some information. You can see if this is what you need:

    # Start the transcript, specifying the output file path
    Start-Transcript -Path "C:\installation_transcript.txt"
    
    # Run your command here
    Install-Package Newtonsoft.Json -Verbose
    
    # Stop the transcript when done
    Stop-Transcript