3

currently I am trying to install software using Windows Package Management from a local repository. Right now I am using the nuget provider to get the package. My problem is, that I want to execute a script after the Install-Package command. I found several answers that are directing to a install.ps1or init.ps1file in a tools folder of the nuget package, but I thing this is related to Visual Studio nuget packages.

How do I start a PowerShell script automatically after the Install-Package command running via PowerShell?

1 Answer 1

1

Yes, you are correct! You would need to use init.ps1 or install.ps1 if you're using NuGet as a Provider. Unfortunately install.sp1 is no longer supported in Visual Studio 2017.

If the package in question, is not available in NuGet then you might want to use a different provider.

You can take a look at the PackageManagement cmdlets

You would have to manage the providers and sources depending upon the available packages.

Create your own Local Package Feed

If you want to create your own local repository: Creating Local NuGet Repository

To call a Powershell script after Install-Package command, create a file called as install.ps1 and add the below code:

Install-Package -Name 7Zip4Powershell
<Your stuff, for e.g;> 
Get-ChildItem
Get-Hotfix

To run the script: powershell.exe -ExecutionPolicy Bypass -File install.ps1

If you have a code apart from Install-Package in a different ps1 file, then you can call the script:

Install-Package -Name 7zip4powershell
.\MyCode.ps1

Check out this link: Running PowerShell scripts during NuGet package installation and removal

Package Management in Windows Powershell

Lastly, if you really want to see whats the code inside Install-Package, check out: Disassemble Powershell cmdlet Hope this helps!

6
  • Hi Rajiv Iyer, The article links to the topic I mentioned above, that everything I find is related to Visual Studio. But I do not have a Nuget Package with any Visual Studio Libraries - I have a Nuget Package with, let's say, 7zip I want do install in Windows.
    – Hunv
    Commented Jul 6, 2019 at 20:03
  • You can create a wrapper (ps1) file which contains the commands to install 7zip and do the other required stuff
    – Rajiv Iyer
    Commented Jul 8, 2019 at 9:55
  • Yes, but how is that script executed using PowerShell only? i.e. the command Install-Package
    – Hunv
    Commented Jul 9, 2019 at 10:40
  • Do you just want to execute the Install-Package Command that will do everything? Can you also elaborate your query? I have updated my post.
    – Rajiv Iyer
    Commented Jul 9, 2019 at 11:20
  • I have a package I created on my own in my own Repository. I can use Install-Package and Remove-Package commands. So technically that works so far. As far as I see, these commands simply unpack files from the nuget-Package and does not execute anything. Because I want to run everything from one command ("Install-Package") without a second command, I asked this questions. Your Link "Running PowerShell scripts during NuGet package installation and removal" is again just for Visual Studio that executes this install.ps1-file after the nuget package was extracted.Maybe Chocolatey is my only option?
    – Hunv
    Commented Jul 16, 2019 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .