-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell_profile.ps1
212 lines (192 loc) · 6.14 KB
/
Microsoft.PowerShell_profile.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
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
# C:\Users\uv\OneDrive\PowerShell\Microsoft.PowerShell_profile.ps1
#echo "we have been used"
function Write-TimedOutput {
param (
[string]$Message,
[string]$ForegroundColor
)
# Build the command dynamically (stupid windows)
$parameters = @{}
if ($ForegroundColor) { $parameters["ForegroundColor"] = $ForegroundColor }
Write-Host "$(Get-Date): $Message" @parameters
}
# Get only the directory of the script
$currentScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Path
Write-TimedOutput "The current script's directory is: $currentScriptDirectory"
$modules = @(
"PSReadLine",
"posh-git",
"oh-my-posh"
)
function IsAdmin {
# Check if the current user has administrative rights
$isAdmin=([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
# Record the end time
return $isAdmin
}
# used in InstallNewSystem.ps1
$fallBackScope = "User"
function Add-ToPathIfNotExist {
param (
[Parameter(Mandatory = $true)]
[string]$NewPath,
[Parameter()]
[string]$scope = "Machine" # Machine or User
)
# Get the current PATH
$currentPath = [System.Environment]::GetEnvironmentVariable('PATH', $scope)
# Split the PATH into an array
$pathArray = $currentPath -split ';'
# Check if the new path already exists in the PATH
if ($pathArray -notcontains $NewPath) {
# Add the new path
$newPathValue = $currentPath + ';' + $NewPath
try {
# Set the new PATH value
if (-not (IsAdmin)) {
$scope=$fallBackScope
}
[System.Environment]::SetEnvironmentVariable('PATH', $newPathValue, $scope)
Write-TimedOutput "Added '$NewPath' to the $scope PATH" -ForegroundColor Green
}
catch {
Write-Host "Failed to add '$NewPath' to the system PATH. Error: $_\r\nTrying for User" -ForegroundColor Red
}
}
else {
Write-TimedOutput "'$NewPath' already exists in the system PATH." -ForegroundColor Yellow
}
}
function lnk {
param ([string]$MyPath)
if ($MyPath -like "*.lnk") {
(New-Object -ComObject WScript.Shell).CreateShortcut($MyPath).TargetPath
} else {
(Get-Item -Path $MyPath).Target
}
}
function grep {
param(
[string]$Pattern,
[string[]]$Paths
)
if ($Paths) {
Get-Content -Path $Paths | Select-String -Pattern $Pattern
} else {
$input | Select-String -Pattern $Pattern
}
}
# shitty powershell cannot do proper aliases, so we need functions
function gs { git status }
function gip { git push }
# gc is builtin so we have to use gic
function gic {
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$args
)
git commit @args
}
function ga {
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$args
)
git add @args
}
# Wrapper function for easier use
function raa {
param (
[string]$ScriptToRun,
[string[]]$ScriptArgs = @()
)
RunAsAdmin -ScriptToRun $ScriptToRun -ScriptArgs $ScriptArgs
}
function show-aliases {
$alc=$(Get-Alias).Count
Write-TimedOutput "Profile reloaded. $alc Aliases"
}
# the reload does not replace functions already loaded in the context, dunnow how to fix that except to restart the shell
function reload {
. $PROFILE
}
# try to emulate linux which
function which {
param (
[Parameter(Mandatory = $true)]
[string]$Command
)
if (-not $Command -or $Command -eq "") {
Write-Output "Usage: which -Command <command_name> # linux style"
return
}
# Call the Get-CommandType script
$scriptPath = "$currentScriptDirectory\Scripts\Get-CommandType.ps1"
if (Test-Path $scriptPath) {
. $scriptPath -Command $Command
} else {
Write-Output "Get-CommandType script not found at $scriptPath"
}
}
function gd { git diff }
function Show-Env { Get-ChildItem Env: }
function Show-Path { $env:PATH -split ';' }
# the aliases
Set-Alias env Show-Env
Set-Alias ls Get-ChildItem
Set-Alias cat Get-Content
Set-Alias path Show-Path
Set-Alias np notepad++.exe
Set-Alias reboot Restart-Computer
# Function to wrap VLC command
function vlc {
param (
[Parameter(Mandatory=$false)]
[string[]]$Args
)
& "C:\Program Files\VideoLAN\VLC\vlc.exe" @Args
}
# Create an alias 'll' for listing the current folder and its children as JSON
function ListAsJson {
Get-ChildItem -Path "." -Recurse
}
# Check if alias 'll' exists and remove it if it does
if (Test-Path Alias:ll) {
Write-Host "Removing existing alias 'll'..." -ForegroundColor Yellow
Remove-Item Alias:ll
}
# Set the alias
Set-Alias -Name ll -Value ListAsJson
Write-Host "New alias 'll' ListAsJson" -ForegroundColor Green
# Import the Chocolatey Profile that contains the necessary code to enable
# tab-completions to function for `choco`.
# Be aware that if you are missing these lines from your profile, tab completion
# for `choco` will not function.
# See https://ch0.co/tab-completion for details.
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
$DefaultInstallModuleOptions = " -Scope AllUsers -Force -Confirm:$false -ErrorAction Stop"
# install useful modules
foreach ($module in $modules) {
# Check if the module is already installed
if (!(Get-Module -ListAvailable -Name $module)) {
try {
$installCommand = "Install-Module -Name $module $DefaultInstallModuleOptions"
Write-Output "Installing $module..."
Invoke-Expression $installCommand
Write-TimedOutput "$module installed successfully."
} catch {
Write-TimedOutput "Failed to install ${module}: $_" -ForegroundColor Red
}
} else {
Write-TimedOutput "$module is already installed."
}
}
# path management
#$time = Measure-Command {
Add-ToPathIfNotExist -NewPath "$currentScriptDirectory\Scripts"
#}
#Write-Output "Add-ToPathIfNotExist took $time." -ForegroundColor Yellow
show-aliases