forked from ChrisTitusTech/powershell-profile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ps1
126 lines (112 loc) · 4.94 KB
/
setup.ps1
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
# 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 {
$testConnection = Test-Connection -ComputerName www.google.com -Count 1 -ErrorAction Stop
return $true
}
catch {
Write-Warning "Internet connection is required but not available. Please check your connection."
return $false
}
}
# 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 {
Get-Item -Path $PROFILE | Move-Item -Destination "oldprofile.ps1" -Force
Invoke-RestMethod https://github.com/ChrisTitusTech/powershell-profile/raw/main/Microsoft.PowerShell_profile.ps1 -OutFile $PROFILE
Write-Host "The profile @ [$PROFILE] has been created and old profile removed."
Write-Host "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
try {
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$fontFamilies = (New-Object System.Drawing.Text.InstalledFontCollection).Families.Name
if ($fontFamilies -notcontains "CaskaydiaCove NF") {
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFileAsync((New-Object System.Uri("https://github.com/ryanoasis/nerd-fonts/releases/download/v3.2.1/CascadiaCode.zip")), ".\CascadiaCode.zip")
while ($webClient.IsBusy) {
Start-Sleep -Seconds 2
}
Expand-Archive -Path ".\CascadiaCode.zip" -DestinationPath ".\CascadiaCode" -Force
$destination = (New-Object -ComObject Shell.Application).Namespace(0x14)
Get-ChildItem -Path ".\CascadiaCode" -Recurse -Filter "*.ttf" | ForEach-Object {
If (-not(Test-Path "C:\Windows\Fonts\$($_.Name)")) {
$destination.CopyHere($_.FullName, 0x10)
}
}
Remove-Item -Path ".\CascadiaCode" -Recurse -Force
Remove-Item -Path ".\CascadiaCode.zip" -Force
}
}
catch {
Write-Error "Failed to download or install the Cascadia Code font. Error: $_"
}
# 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: $_"
}