.net, .net core, Powershell

Using PowerShell to install the latest .NET Core 2 preview

I’ve been living on the bleeding edge and programming with the .NET Core 2 preview, which lives at https://github.com/dotnet/cli.

Reasonably frequently I find that I get error messages from the compiler saying there’s been some kind of file binary mismatch between versions, and I resolve this by getting rid of previous versions of .NET Core 2, and clearing out my .NET and Nuget caches.

Since a new build comes out every day, I wrote a simple PowerShell script to automate this process. I’ve highlighted (in red) some paths in the script which you might have to change in your setup if you want to use the script.

This is a pretty savage way to clear dotnet out – it deletes a lot of stuff. I use it on my development machine which I’m happy to trash. If you use this script, treat it very carefully!

Write-Host "About to clear .NET cache from my profile..."
$dotnetProfileFolder = "C:\Users\Jeremy\.dotnet"
Remove-Item $dotnetProfileFolder\* -recurse

# https://jeremylindsayni.wordpress.com/2016/05/24/fixing-rogue-behaviour-in-nuget-by-clearing-the-caches/
Write-Host "About to clear Nuget Cache..."
nuget locals all -clear

# https://blog.jourdant.me/post/3-ways-to-download-files-with-powershell
Write-Host "About to delete existing .NET Core binaries..."
$dotNetSdkFolder = "C:\Program Files\dotnet"
Remove-Item $dotNetSdkFolder\* -recurse

Write-Host "About to download latest .NET Core 2 binaries..."
$url = "https://dotnetcli.blob.core.windows.net/dotnet/Sdk/master/dotnet-dev-win-x64.latest.zip"
$output = "$dotNetSdkFolder\dotnet-dev-win-x64.latest.zip"

Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $output

# https://www.howtogeek.com/tips/how-to-extract-zip-files-using-powershell/
Write-Host "About to unzip latest .NET Core 2 binaries..."
$shell = new-object -com shell.application
$zip = $shell.NameSpace($output)
foreach($item in $zip.items())

Write-Host "Done - dotnet version installed is:"
dotnet --version