-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathsetup.ps1
More file actions
233 lines (199 loc) · 7.4 KB
/
Copy pathsetup.ps1
File metadata and controls
233 lines (199 loc) · 7.4 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
$profileSourceUri = 'https://github.com/ChrisTitusTech/powershell-profile/raw/main/Microsoft.PowerShell_profile.ps1'
$themeSourceUri = 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/cobalt2.omp.json'
function Enable-Tls12 {
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
} catch {
Write-Verbose "Unable to enable TLS 1.2 explicitly: $_"
}
}
Enable-Tls12
function Test-IsAdministrator {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal]$identity
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Test-Command {
param([Parameter(Mandatory)][string]$Name)
$null -ne (Get-Command -Name $Name -ErrorAction SilentlyContinue)
}
function Save-UriToFile {
param(
[Parameter(Mandatory)][string]$Uri,
[Parameter(Mandatory)][string]$OutFile
)
$client = New-Object System.Net.WebClient
try {
$client.DownloadFile($Uri, $OutFile)
} finally {
$client.Dispose()
}
}
function Get-ProfileDir {
switch ($PSVersionTable.PSEdition) {
'Core' { Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'PowerShell'; break }
'Desktop' { Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'WindowsPowerShell'; break }
default { throw "Unsupported PowerShell edition: $($PSVersionTable.PSEdition)" }
}
}
function Test-InternetConnection {
$response = $null
try {
$request = [System.Net.WebRequest]::Create('https://github.com')
$request.Method = 'HEAD'
$request.Timeout = 10000
$response = $request.GetResponse()
return $true
} catch {
Write-Warning 'Internet connection is required but GitHub is not reachable.'
return $false
} finally {
if ($response) {
$response.Close()
}
}
}
function Save-ProfileBackup {
param([Parameter(Mandatory)][string]$ProfilePath)
if (-not (Test-Path -Path $ProfilePath -PathType Leaf)) {
return $null
}
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$backupPath = Join-Path (Split-Path -Path $ProfilePath -Parent) "oldprofile-$timestamp.ps1"
Copy-Item -Path $ProfilePath -Destination $backupPath -Force
return $backupPath
}
function Install-Profile {
param(
[Parameter(Mandatory)][string]$SourceUri,
[Parameter(Mandatory)][string]$ProfilePath
)
$profileDir = Split-Path -Path $ProfilePath -Parent
if (-not (Test-Path -Path $profileDir)) {
New-Item -Path $profileDir -ItemType Directory -Force | Out-Null
}
$tempProfile = Join-Path $env:TEMP 'Microsoft.PowerShell_profile.ps1'
try {
Save-UriToFile -Uri $SourceUri -OutFile $tempProfile
$backupPath = Save-ProfileBackup -ProfilePath $ProfilePath
Copy-Item -Path $tempProfile -Destination $ProfilePath -Force
Write-Host "PowerShell profile installed to [$ProfilePath]."
if ($backupPath) {
Write-Host "Previous profile backed up to [$backupPath]."
}
} finally {
Remove-Item -Path $tempProfile -ErrorAction SilentlyContinue
}
}
function Install-WinGetPackage {
param(
[Parameter(Mandatory)][string]$Id,
[Parameter(Mandatory)][string]$Name
)
if (-not (Test-Command winget)) {
Write-Warning "winget was not found. Skipping $Name."
return $false
}
try {
winget install --id $Id --exact --source winget --accept-source-agreements --accept-package-agreements --silent
if ($LASTEXITCODE -ne 0) {
Write-Warning "winget failed to install $Name. Exit code: $LASTEXITCODE"
return $false
}
return $true
} catch {
Write-Warning "Failed to install $Name. Error: $_"
return $false
}
}
function Install-OhMyPoshTheme {
param(
[string]$ThemeName = 'cobalt2',
[string]$ThemeUri = $themeSourceUri
)
$profileDir = Get-ProfileDir
if (-not (Test-Path -Path $profileDir)) {
New-Item -Path $profileDir -ItemType Directory -Force | Out-Null
}
$themePath = Join-Path $profileDir "$ThemeName.omp.json"
try {
Save-UriToFile -Uri $ThemeUri -OutFile $themePath
Write-Host "Oh My Posh theme installed to [$themePath]."
return $themePath
} catch {
Write-Warning "Failed to download Oh My Posh theme. Error: $_"
return $null
}
}
function Get-InstalledFontName {
try {
Add-Type -AssemblyName System.Drawing -ErrorAction Stop
return (New-Object System.Drawing.Text.InstalledFontCollection).Families.Name
} catch {
Write-Warning "Unable to inspect installed fonts. Error: $_"
return @()
}
}
function Install-NerdFont {
param(
[string]$FontName = 'CascadiaCode',
[string]$FontDisplayName = 'CaskaydiaCove NF',
[string]$Version = '3.2.1'
)
if ((Get-InstalledFontName) -contains $FontDisplayName) {
Write-Host "Font [$FontDisplayName] is already installed."
return $true
}
$fontZipUrl = "https://github.com/ryanoasis/nerd-fonts/releases/download/v$Version/$FontName.zip"
$zipFilePath = Join-Path $env:TEMP "$FontName.zip"
$extractPath = Join-Path $env:TEMP $FontName
try {
Remove-Item -Path $extractPath -Recurse -Force -ErrorAction SilentlyContinue
Save-UriToFile -Uri $fontZipUrl -OutFile $zipFilePath
Expand-Archive -Path $zipFilePath -DestinationPath $extractPath -Force
$fontShellFolder = (New-Object -ComObject Shell.Application).Namespace(0x14)
Get-ChildItem -Path $extractPath -Recurse -Filter '*.ttf' | ForEach-Object {
if (-not (Test-Path "C:\Windows\Fonts\$($_.Name)")) {
$fontShellFolder.CopyHere($_.FullName, 0x10)
}
}
Write-Host "Font [$FontDisplayName] installed."
return $true
} catch {
Write-Warning "Failed to install $FontDisplayName. Error: $_"
return $false
} finally {
Remove-Item -Path $extractPath -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $zipFilePath -Force -ErrorAction SilentlyContinue
}
}
function Install-TerminalIconsModule {
try {
Install-Module -Name Terminal-Icons -Repository PSGallery -Scope CurrentUser -Force -SkipPublisherCheck
Write-Host 'Terminal-Icons module installed.'
return $true
} catch {
Write-Warning "Failed to install Terminal-Icons. Error: $_"
return $false
}
}
if (-not (Test-IsAdministrator)) {
Write-Warning 'Please run this script as an Administrator.'
return
}
if (-not (Test-InternetConnection)) {
return
}
try {
[Environment]::SetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', '1', [EnvironmentVariableTarget]::Machine)
} catch {
Write-Warning "Unable to set PowerShell telemetry opt-out. Error: $_"
}
$profilePath = $PROFILE.CurrentUserCurrentHost
Install-Profile -SourceUri $profileSourceUri -ProfilePath $profilePath
Install-WinGetPackage -Id 'JanDeDobbeleer.OhMyPosh' -Name 'Oh My Posh' | Out-Null
Install-WinGetPackage -Id 'ajeetdsouza.zoxide' -Name 'zoxide' | Out-Null
Install-OhMyPoshTheme | Out-Null
Install-NerdFont | Out-Null
Install-TerminalIconsModule | Out-Null
Write-Host 'Setup completed. Restart PowerShell to load the profile.'