PowerShellGet stops working because of TLS Settings

Today I wanted to install a new module on my Management System and it came up with some errors like:

Unable to find repository ‘https://www.powershellgallery.com/api/v2/
MSG:UnableToDownload «https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409» «»

I found some posts pointing to a wrong URL which made me look into the wrong direction. Finally I found this: https://answers.microsoft.com/en-us/windows/forum/windows_7-performance/trying-to-install-program-using-powershell-and/4c3ac2b2-ebd4-4b2a-a673-e283827da143

The solution is to enable tls12 in the shell/framework:

 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

This changes it for a the current powershell session.

To fix this for all sessions, you need to do whats described in : Azure AD Connect: TLS 1.2 enforcement for Azure Active Directory Connect | Microsoft Docs

Workaround before fix: Add this to your Powershell Profile:

$tls = [Net.ServicePointManager]::SecurityProtocol
If ($tls.value__ -ne 4032) {[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls, [System.Net.SecurityProtocolType]::Tls11,[System.Net.SecurityProtocolType]::Tls12}