Powershell: Install-dotNET4 function

This function will download and install ,NET 4.0. It uses the Get-Framework-Versions function to determine if the installation is necessary or not. Internet Connectivity will be required as the script auto downloads the setup file (and sleeps for 360 seconds... I had a function in there to monitor for install completion at first, turns out the setup file spawns so many childprocesses the function just got confused and locked up -_-)

Alternatively you could drop the installation file in the folder specified on the $folderPath variable too. That will skip the download and use the file. This function easily adapts in to other versions f.e. I use it for Powershell 3 installs as well!

_Function install-dotNet4 () {
    if(($InstalledDotNET -eq "4.0") -or ($InstalledDotNET -eq "4.0c")){
        write-host ".NET 4.0 Framework is already installed" -foregroundcolor Green
    } else{
    
        #set a var for the folder you are looking for
        $folderPath = 'C:\Temp'

        #Check if folder exists, if not, create it
        if (Test-Path $folderpath){
            Write-Host "The folder $folderPath exists." -ForeGroundColor Green
        } else{
            Write-Host "The folder $folderPath does not exist, creating..." -NoNewline -ForegroundColor Red
            New-Item $folderpath -type directory | Out-Null
            Write-Host " - done!" -ForegroundColor Green
        }

        # Check if file exists, if not, download it
        $file = $folderPath+"\dotNetFx40_Full_x86_x64.exe"
        if (Test-Path $file){
            write-host "The file $file exists." -ForeGroundColor Green
        } else {
            #Download Microsoft .Net 4.0 Framework
            Write-Host "Downloading Microsoft .Net 4.0 Framework..." -nonewline -ForeGroundColor DarkYellow
            $clnt = New-Object System.Net.WebClient
            $url = "http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40\_Full\_x86\_x64.exe"
            $clnt.DownloadFile($url,$file)
            Write-Host " - done!" -ForegroundColor Green
        }
        #Install Microsoft .Net Framework
        Write-Host "Installing Microsoft .Net Framework..." -nonewline -ForegroundColor DarkYellow
        $dotNET4 = $folderPath+"\dotNetFx40_Full_x86_x64.exe /quiet /norestart"
        Invoke-Expression $dotNET4
        write-host " - done!" -ForegroundColor Green
        start-sleep -seconds 360
    }
}_

This article is part of the GWB Archives. Original Author: marc dekeyser

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.