dotnet / dotnet/docs

Powershell examples don't work in older powershell versions (4.0 and lower)

Open
#36,240 0 comments 0 reactions 0 assignees View on GitHub
app-compat/subsvc dotnet-framework/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

Powershell examples do not take into an account older versions of powershell that depend on older .net Framework. Specifically in powershell v2, the 'Get-ItemPropertyValue' cmdlet doesn't exist. Here's what worked for me.

```powershell
$release = Get-ItemProperty -LiteralPath 'HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Release
switch ($release.Release) {
{ $_ -ge 533320 } { $version = '4.8.1 or later'; break }
{ $_ -ge 528040 } { $version = '4.8'; break }
{ $_ -ge 461808 } { $version = '4.7.2'; break }
{ $_ -ge 461308 } { $version = '4.7.1'; break }
{ $_ -ge 460798 } { $version = '4.7'; break }
{ $_ -ge 394802 } { $version = '4.6.2'; break }
{ $_ -ge 394254 } { $version = '4.6.1'; break }
{ $_ -ge 393295 } { $version = '4.6'; break }
{ $_ -ge 379893 } { $version = '4.5.2'; break }
{ $_ -ge 378675 } { $version = '4.5.1'; break }
{ $_ -ge 378389 } { $version = '4.5'; break }
default { $version = $null; break }
}
if ($version) {
Write-Host ".NET Framework Version: $version"
}
else {
Write-Host ".NET Framework Version 4.5 or later is not detected. Proceeding with installation..."
Write-Host "Downloading and installing .NET Framework 4.8..."
Invoke-WebRequest -Uri $DownloadURL -OutFile $DownloadPath
Start-Process -FilePath $DownloadPath -ArgumentList '/q' -Wait
Remove-Item -Path $DownloadPath
Write-Host ".NET Framework 4.8 has been installed successfully."
}
```
---
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: 2115da38-e95e-46fc-1e2d-da88ef92c6cf
* Version Independent ID: a3790e8a-0654-851b-c991-ab7382025406
* Content: [Determine which .NET Framework versions are installed - .NET Framework](https://learn.microsoft.com/en-ca/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed)
* Content Source: [docs/framework/migration-guide/how-to-determine-which-versions-are-installed.md](https://github.com/dotnet/docs/blob/main/docs/framework/migration-guide/how-to-determine-which-versions-are-installed.md)
* Product: **dotnet-framework**
* Technology: **dotnet-appcompat**
* GitHub Login: @gewarren
* Microsoft Alias: **gewarren**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.