PowerShell – update .NET

How to check the .NET version on remote clients? How to update the outdated versions of .NET on remote hosts? The answer is below…

$computers=Get-Content '\\host\shares\!scripts\!source\!computers.txt'
function GetDotNetVersion {
     param (
        [string] $computer)
        $dotNetRegKey=Invoke-Command -computerName $computer -scriptBlock {(Get-ItemProperty -Path 'HKLM:SOFTWARE\Microsoft\Net Framework Setup\NDP\v4\Full\' -Name "Release").Release}
        return $dotNetRegKey

foreach($computer in $computers) {
    if(Test-Connection -ComputerName $computer -Count 1 -Quiet) {
        Try {           
            if($dotNetVersion -lt $dotNetAcctualVersion) {
                Invoke-Expression "msg.exe * /SERVER:$computer /TIME:3600 'Nie wyłączaj komputera - trwa aktualizacja!'"

                Write-host "Copying files: $computer" -foreground 'gray'
                Copy-Item -Path $source -Destination \\$computer\$destination -Force -Recurse
                Write-host "Installation: $computer" -foreground 'gray'
                Invoke-Command -computerName $computer -ScriptBlock {powershell.exe 'c:\install\net\NDP47-KB3186497-x86-x64-AllOS-ENU.exe' /q /norestart}

                Invoke-Expression "msg.exe * /SERVER:$computer /TIME:21600 'Komputer został zaktualiowany - proszę go ponownie uruchomić, w przeciwnym razie za 6 godzin nastąpi automatyczny (wymuszony) restart.'"
                Invoke-Expression "shutdown /m $computer /r /f /c 'Restart' /t 21600"

                Write-host "updated: $computer" -foreground 'green'
                $computer >> $successLog
            } else {
                Write-host "$computer':' $dotNetVersion" -foreground 'green'
                $computer + ": " + $dotNetVersion >> $successLog
        } Catch {
            Write-host "error: $computer" -foreground 'red'
            $computer >> $errorLog
    } else {
        Write-host "offline: $computer" -foreground 'yellow'
        $computer >> $unreachableLog


Leave a Reply

Your email address will not be published. Required fields are marked *