Skip to content

Commit bd064a8

Browse files
committed
Automate PowerShell Updates via API check and winget
- Fold Environment Variables into profile functions
1 parent bcac374 commit bd064a8

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

install.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Catch {
3232
Write-Host -ForegroundColor Red " Fail ❌"
3333
}
3434

35+
# Make sure PowerShell is up to date
36+
Update-PowerShellCore
37+
3538
# Cleanup PS Module version of oh-my-posh
3639
Get-Module oh-my-posh -ListAvailable -ErrorAction SilentlyContinue | Uninstall-Module -Force -Verbose
3740

profile_functions.psm1

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ function Set-ProfileEnvironment ([hashtable]$Variables) {
368368

369369
ForEach ($Variable in $Variables.GetEnumerator()) {
370370
Write-Host -ForegroundColor White "Setting Environment Variable: " -NoNewline
371-
Write-Host -ForegroundColor Magenta "$($Variable.Name)".PadRight(100).Substring(0,($NameLen +2)) -NoNewline
372-
Write-Host "= $($Variable.Value): ".PadRight(200).Substring(0,($ValLen + 2)) -NoNewline
371+
Write-Host -ForegroundColor Magenta "$($Variable.Name)".PadRight(100).Substring(0, ($NameLen + 2)) -NoNewline
372+
Write-Host "= $($Variable.Value): ".PadRight(200).Substring(0, ($ValLen + 2)) -NoNewline
373373
# Set Environment Variable
374374
[System.Environment]::SetEnvironmentVariable($Variable.Name, $Variable.Value, "Machine")
375375
Write-Host -ForegroundColor Green " done ✅"
@@ -391,5 +391,35 @@ function Initialize-OhMyPosh {
391391
}
392392
}
393393

394+
function Update-PowerShellCore {
395+
$UpdateCheck = [pscustomobject]@{
396+
Current = [version]$PSVersionTable.PSVersion | Select-Object Major, Minor, Build;
397+
Latest = [version](Invoke-RestMethod -Uri "https://aka.ms/pwsh-buildinfo-stable" | Select-Object -ExpandProperty ReleaseTag | % { $_ -replace "v" }) | Select-Object Major, Minor, Build
398+
}
399+
$CurrentSemantic = ($UpdateCheck.Current.psobject.properties | ForEach-Object { $_.Value }) -join "."
400+
$LatestSemantic = ($UpdateCheck.Latest.psobject.properties | ForEach-Object { $_.Value }) -join "."
401+
$VersionSummary = "[ current: $($CurrentSemantic) latest: $($LatestSemantic) ]"
402+
403+
$NoUpdate = if (
404+
$UpdateCheck.Current.Major -eq $UpdateCheck.Latest.Major -and
405+
$UpdateCheck.Current.Minor -eq $UpdateCheck.Latest.Minor -and
406+
$UpdateCheck.Current.Build -eq $UpdateCheck.Latest.Build
407+
) { $true }else { $false }
408+
if ($NoUpdate) {
409+
Write-Host "PowerShell is up to date $VersionSummary"
410+
}
411+
else {
412+
Write-Host "PowerShell needs to be updated $VersionSummary"
413+
if (!(Get-Command -Name pwsh -ErrorAction SilentlyContinue)) {
414+
winget install --id Microsoft.PowerShell --exact
415+
}
416+
else {
417+
Write-Host -ForegroundColor White "Run " -NoNewline
418+
Write-Host -ForegroundColor Yellow "winget install --id Microsoft.PowerShell --exact"
419+
EXIT
420+
}
421+
}
422+
}
423+
394424

395425
Export-ModuleMember *-*

readme.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,31 +134,18 @@ Neovim looks for it's configuration by default on windows in this directory.
134134

135135
Method for getting latest version of powershell from winget
136136

137-
```powershell
138-
$winget = winget search --Id Microsoft.PowerShell --exact;
139-
$PSLatest = $winget | where-object {
140-
$_ -match "Microsoft.PowerShell"
141-
} | ForEach-Object {
142-
$_ -split "\s+" | Select-Object -Index 2
143-
}
144-
```
137+
*manually*
145138

146139
```powershell
147-
Get-Command pwsh -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Version
140+
winget install --id Microsoft.PowerShell --exact
148141
```
149142

150-
This method might work better
143+
*programmatically*
151144

152-
```PowerShell
153-
$UpdateCheck = [pscustomobject]@{
154-
Current=[version]($PSVersionTable.PSVersion | Select-Object Major,Minor,@{N="Build";E={$_.Patch}});
155-
Latest=[version](Invoke-RestMethod -Uri "https://aka.ms/pwsh-buildinfo-stable" | Select-Object -ExpandProperty ReleaseTag | %{ $_ -replace "v"}) | Select-Object Major,Minor,Build
156-
}
157-
$NoUpdate = if (
158-
$UpdateCheck.Current.Major -eq $UpdateCheck.Latest.Major -and
159-
$UpdateCheck.Current.Minor -eq $UpdateCheck.Latest.Minor -and
160-
$UpdateCheck.Current.Build -eq $UpdateCheck.Latest.Build
161-
) { $true }else { $false }
145+
```powershell
146+
# Profile Module Function\
147+
# Invoked when running .\Install.ps1
148+
Update-PowerShellCore
162149
```
163150

164151
## Git Config

0 commit comments

Comments
 (0)