Published on

Shell: Update all NuGet dependencies in your solution/directory

So, my VS Code environment is messed up on my local machine, and it's definitely not a good thing, especially when you want to update all the NuGet packages in your solution (note, in my case, there was > 100).

Anyways, after some googling I couldn't find any open-sourced solutions, which would help me to update all NuGet packages at the same time. PAINFUL!

I am definitely not spending my time going 1 by 1 project and updating a dependency 1 by 1. Total waste of time.

So, if you're like me, here is a bash script, which will loop through your .csproj files, and update all NuGet packages.

Use with caution!

#!/bin/bash

# Find all .csproj files recursively in the root directory
project_files=$(find . -name "*.csproj")

# Loop through each project file and update its dependencies
for file in $project_files; do
    project_directory=$(realpath "$(dirname "$file")")

    echo "Found $project_directory..."

    # Navigate to the project directory
    pushd "$project_directory" || continue
    echo "Inside $project_directory..."

    # Get a list of outdated packages
    package_references=$(dotnet list package --outdated)

    # Update each package to its latest version
    while read -r line; do
        if [[ $line == *">"* ]]; then
            package=$(echo "$line" | awk '{ print $2 }')
            current_version=$(echo "$line" | awk '{ print $4 }')
            latest_version=$(echo "$line" | awk '{ print $5 }')

            echo "Updating $package from $current_version to $latest_version..."
            dotnet add package "$package" --version "$latest_version"
        fi
    done <<< "$package_references"

    # Return to the root directory
    popd
done

Example output:

Found /mnt/c/Dev/proodos/src/Shared/Shared.TransferObjects...
Inside /mnt/c/Dev/proodos/src/Shared/Shared.TransferObjects...
Updating FluentValidation from 11.5.0 to 11.5.2...