One recurring challenge I face is that every time a new version of .NET is released, I need to install it on my system and then update my older projects to the new .NET version. While this often brings new features and removes some old ones, the process remains cumbersome.
Wouldn’t it be simpler if there were a way to update .NET packages with a single CLI command? In this post, I’ll share a solution that makes the process significantly easier.
Step 1: Install the Required Tool
To manage and update NuGet packages in your .NET projects, you need the dotnet-outdated-tool. Install it globally on your system using the following command:
dotnet tool install --global dotnet-outdated-tool
Step 2: Create a Script for Automation
Instead of manually updating each project, you can use a PowerShell script to automate the process. This script scans all .csproj
files in your directory and updates them with minimal effort.
- Create a file named
Update-NuGet.ps1
in the root directory of your projects. - Add the following code to the file:
# Get all .csproj files in the current directory and subdirectories
Get-ChildItem -Recurse -Filter *.csproj | ForEach-Object {
$project = $_.FullName
Write-Host "Updating packages for project: $project"
dotnet outdated $project --upgrade
}
Write-Host "All projects updated!"
Step 3: Run the Script
Navigate to the folder where you saved the Update-NuGet.ps1
file and run the script using the command:
./Update-NuGet.ps1
The script will locate all .csproj
files in the directory and its subdirectories, update the NuGet packages, and display a message once all projects are updated.
Benefits of This Method
- Time-Saving: No need to manually update each project.
- Automation: Simplifies the process for managing multiple projects.
- Efficiency: Updates all your projects in one go with a single command.