Execute Script using Powershell Install-Package

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?

Answer

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!

Attribution
Source : Link , Question Author : Hunv , Answer Author : Rajiv Iyer

Leave a Comment