Skip to content

Commit fba8f55

Browse files
committed
Add Install-PowerShellAsDotNetTool.ps1
1 parent ad53ee6 commit fba8f55

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<#
2+
.SYNOPSIS
3+
Install PowerShell as a .NET global tool, which does not require Administrator privileges.
4+
.DESCRIPTION
5+
This script installs PowerShell as a .NET global tool, which does not require Administrator privileges. It begins by
6+
downloading the required .NET Tool install script, which is then used to install PowerShell.
7+
.NOTES
8+
Author: Sam Erde (@SamErde)
9+
Date: 2025-02-12
10+
#>
11+
12+
# Download the .NET CLI install script if it is not found.
13+
if ( (Get-Command -Name 'dotnet' -ErrorAction SilentlyContinue) ) {
14+
Write-Verbose "dotnet is already installed."
15+
} else {
16+
$DownloadPath = Join-Path -Path $env:TEMP -ChildPath 'dotnet-install.ps1'
17+
try {
18+
Invoke-WebRequest 'https://dot.net/v1/dotnet-install.ps1' -OutFile $DownloadPath
19+
Unblock-File -Path $DownloadPath
20+
} catch {
21+
Write-Error "Failed to download dotnet-install.ps1 to '$DownloadPath'."
22+
throw $_
23+
}
24+
25+
# Install the dotnet tool. The script installs the latest LTS release by default.
26+
# The current stable release is required for PowerShell 7.5, which depends on .NET 9.
27+
# Need to add a check for the appropriate version of .NET.
28+
try {
29+
.$DownloadPath -InstallDir '~/.dotnet' -Channel 'STS' # use of 'Current' is deprecated.
30+
} catch {
31+
throw $_
32+
}
33+
34+
}
35+
36+
dotnet dev-certs https --trust
37+
38+
# Install PowerShell and add $HOME\.dotnet\tools to the PATH.
39+
# Need to add error handling for when the required version of .NET is missing.
40+
try {
41+
dotnet tool install --global PowerShell
42+
$env:PATH += ';' + [System.IO.Path]::Combine($HOME, '.dotnet', 'tools')
43+
} catch {
44+
throw $_
45+
}
46+
47+
# Clean up.
48+
Remove-Variable -Name DownloadPath
49+
if (Test-Path -Path DownloadPath) {
50+
Remove-Item -Path $DownloadPath -Confirm:$false
51+
}

0 commit comments

Comments
 (0)