forked from ChrisTitusTech/powershell-profile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
144 lines (128 loc) · 5.63 KB
/
setup.ps1
File metadata and controls
144 lines (128 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Ensure the script can run with elevated privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "Please run this script as an Administrator!"
break
}
# Function to test internet connectivity
function Test-InternetConnection {
try {
Test-Connection -ComputerName www.google.com -Count 1 -ErrorAction Stop | Out-Null
return $true
}
catch {
Write-Warning "Internet connection is required but not available. Please check your connection."
return $false
}
}
# Function to install Nerd Fonts
function Install-NerdFonts {
param (
[string]$FontName = "CascadiaCode",
[string]$FontDisplayName = "CaskaydiaCove NF",
[string]$Version = "3.2.1"
)
try {
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$fontFamilies = (New-Object System.Drawing.Text.InstalledFontCollection).Families.Name
if ($fontFamilies -notcontains "${FontDisplayName}") {
$fontZipUrl = "https://github.com/ryanoasis/nerd-fonts/releases/download/v${Version}/${FontName}.zip"
$zipFilePath = "$env:TEMP\${FontName}.zip"
$extractPath = "$env:TEMP\${FontName}"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFileAsync((New-Object System.Uri($fontZipUrl)), $zipFilePath)
while ($webClient.IsBusy) {
Start-Sleep -Seconds 2
}
Expand-Archive -Path $zipFilePath -DestinationPath $extractPath -Force
$destination = (New-Object -ComObject Shell.Application).Namespace(0x14)
Get-ChildItem -Path $extractPath -Recurse -Filter "*.ttf" | ForEach-Object {
If (-not(Test-Path "C:\Windows\Fonts\$($_.Name)")) {
$destination.CopyHere($_.FullName, 0x10)
}
}
Remove-Item -Path $extractPath -Recurse -Force
Remove-Item -Path $zipFilePath -Force
} else {
Write-Host "Font ${FontDisplayName} already installed"
}
}
catch {
Write-Error "Failed to download or install ${FontDisplayName} font. Error: $_"
}
}
# Check for internet connectivity before proceeding
if (-not (Test-InternetConnection)) {
break
}
# Profile creation or update
if (!(Test-Path -Path $PROFILE -PathType Leaf)) {
try {
# Detect Version of PowerShell & Create Profile directories if they do not exist.
$profilePath = ""
if ($PSVersionTable.PSEdition -eq "Core") {
$profilePath = "$env:userprofile\Documents\Powershell"
}
elseif ($PSVersionTable.PSEdition -eq "Desktop") {
$profilePath = "$env:userprofile\Documents\WindowsPowerShell"
}
if (!(Test-Path -Path $profilePath)) {
New-Item -Path $profilePath -ItemType "directory"
}
Invoke-RestMethod https://github.com/ChrisTitusTech/powershell-profile/raw/main/Microsoft.PowerShell_profile.ps1 -OutFile $PROFILE
Write-Host "The profile @ [$PROFILE] has been created."
Write-Host "If you want to make any personal changes or customizations, please do so at [$profilePath\Profile.ps1] as there is an updater in the installed profile which uses the hash to update the profile and will lead to loss of changes"
}
catch {
Write-Error "Failed to create or update the profile. Error: $_"
}
}
else {
try {
$backupPath = Join-Path (Split-Path $PROFILE) "oldprofile.ps1"
Move-Item -Path $PROFILE -Destination $backupPath -Force
Invoke-RestMethod https://github.com/ChrisTitusTech/powershell-profile/raw/main/Microsoft.PowerShell_profile.ps1 -OutFile $PROFILE
Write-Host "✅ PowerShell profile at [$PROFILE] has been updated."
Write-Host "📦 Your old profile has been backed up to [$backupPath]"
Write-Host "⚠️ NOTE: Please back up any persistent components of your old profile to [$HOME\Documents\PowerShell\Profile.ps1] as there is an updater in the installed profile which uses the hash to update the profile and will lead to loss of changes"
}
catch {
Write-Error "❌ Failed to backup and update the profile. Error: $_"
}
}
# OMP Install
try {
winget install -e --accept-source-agreements --accept-package-agreements JanDeDobbeleer.OhMyPosh
}
catch {
Write-Error "Failed to install Oh My Posh. Error: $_"
}
# Font Install
Install-NerdFonts -FontName "CascadiaCode" -FontDisplayName "CaskaydiaCove NF"
# Final check and message to the user
if ((Test-Path -Path $PROFILE) -and (winget list --name "OhMyPosh" -e) -and ($fontFamilies -contains "CaskaydiaCove NF")) {
Write-Host "Setup completed successfully. Please restart your PowerShell session to apply changes."
} else {
Write-Warning "Setup completed with errors. Please check the error messages above."
}
# Choco install
try {
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
catch {
Write-Error "Failed to install Chocolatey. Error: $_"
}
# Terminal Icons Install
try {
Install-Module -Name Terminal-Icons -Repository PSGallery -Force
}
catch {
Write-Error "Failed to install Terminal Icons module. Error: $_"
}
# zoxide Install
try {
winget install -e --id ajeetdsouza.zoxide
Write-Host "zoxide installed successfully."
}
catch {
Write-Error "Failed to install zoxide. Error: $_"
}