-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell_profile.ps1
568 lines (531 loc) · 27.9 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#The Ultimate PowerShell Profile. Sets up environment for Windows PowerShell (5) and PowerShell 7+ profiles on Windows and Linux.
#Copies itself to the default PowerShell folders from GitHub URL in the $script:UltimatePSProfile.psprofile_link variable.
#Will measure it's own performance and skip parts like looking for new module versions if the profile is taking too long to load.
#Adds some shortcuts for Linux-Like terminal operation in Windows.
#Only runs admin functions when the session is run as admin.
#Inspired by https://github.com/ChrisTitusTech/powershell-profile which was inspired from https://gist.github.com/timsneath/19867b12eee7fd5af2ba
#https://github.com/itoleck/UltimatePowerShellProfile
#--------------------------------------------------------------------------------------
#Setup the initial object to hold all of the script settings. Using object so as not to clutter PS session with variables.
#Most of these can be edited for your local system.
#stopwatch Start tracking the time that the script is running
#max_profileload_seconds Set the maximum time in seconds to determine if the profile should look to install modules that are not available in the profile
#extra_profile_script Calls an extra .ps1 script at the end of this profile script. Use full path, i.e. (.\script.ps1, c:\script.ps1) Used for your own personal commands.
#psprofile_link PowerShell profile URL. You can copy the code in this script and create a GitHub. Update this property.
#psprofile_repo_path Local repo folder for this Ultimate PowerShell Profile. Used for the Copy-Profile command
#global_modules Which modules to load in this profile if the environment is admin(scope global) *Windows only
# If forking this script and adding your own modules, please set back these defaults when submitting PR.
#local_modules Which modules to load in this profile if the environment is non-admin user (scope local) *Windows only
# If forking this script and adding your own modules, please set back these defaults when submitting PR.
#linux_modules Which modules to load in this profile if the environment is non-admin user (scope local) *Linux only
# If forking this script and adding your own modules, please set back these defaults when submitting PR.
#profile_editors A list of text editors, in order of precedence.
# If forking this script and adding your own editors, please set back these defaults when submitting PR.
Function Private:CreateUltimatePSProfileVars {
$script:UltimatePSProfile = [PSCustomObject]@{
extra_profile_script = "" #Calls an extra .ps1 script at the end of this profile script. Used for your own personal commands.
psprofile_link = "https://raw.githubusercontent.com/itoleck/UltimatePowerShellProfile/main/Microsoft.PowerShell_profile.ps1"
psprofile_repo_path = ""
stopwatch = [system.diagnostics.stopwatch]::StartNew()
max_profileload_seconds = 5
mydocuments_path = ""
oh_my_posh_theme = ""
gh_repo_base_folder = ""
system_temp = ""
global_modules = 'Az','Az.Accounts','AzureAD','MSOnline','Az.CostManagement','Microsoft.Graph'
local_modules = 'PSUtil','PowerShellGet','Terminal-Icons','Carbon','CredentialManager','PnP.PowerShell','ImportExcel','WifiTools','ExchangeOnlineManagement','MicrosoftTeams','PSScriptAnalyzer','AzureSaveMoney'
linux_modules = 'PowerShellGet'
profile_editors = 'code','powershell_ise.exe','notepad++.exe','notepad.exe','nano'
}
}
#--------------------------------------------------------------------------------------
#Set some paths if this session is running in Windows
Function Private:SetScriptPaths {
if ($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
if ($script:UltimatePSProfile.psprofile_link.length -lt 1) {
$script:UltimatePSProfile.psprofile_link = "https://raw.githubusercontent.com/itoleck/UltimatePowerShellProfile/main/Microsoft.PowerShell_profile.ps1"
}
if ($script:UltimatePSProfile.psprofile_repo_path.length -lt 1) {
$script:UltimatePSProfile.psprofile_repo_path = "$env:USERPROFILE\source\repos\itoleck\UltimatePowerShellProfile\"
}
if ($script:UltimatePSProfile.gh_repo_base_folder.length -lt 1) {
$script:UltimatePSProfile.gh_repo_base_folder = "$env:USERPROFILE\source\repos\"
}
if ($script:UltimatePSProfile.system_temp.length -lt 1) {
$script:UltimatePSProfile.system_temp = $env:TEMP + "\"
}
if ($script:UltimatePSProfile.mydocuments_path.length -lt 1) {
$script:UltimatePSProfile.mydocuments_path = [System.Environment]::GetFolderPath('Personal') + "\"
}
if ($script:UltimatePSProfile.oh_my_posh_theme.length -lt 1) {
$script:UltimatePSProfile.oh_my_posh_theme = "$env:APPDATA\Local\Programs\oh-my-posh\themes\agnoster.omp.json"
}
}
#Set some paths if this session is running in Linux
if ($IsLinux) {
if ($script:UltimatePSProfile.psprofile_repo_path.length -lt 1) {
$script:UltimatePSProfile.psprofile_repo_path = [System.Environment]::GetFolderPath('Personal') + "/source/repos/itoleck/UltimatePowerShellProfile/"
}
if ($script:UltimatePSProfile.gh_repo_base_folder.length -lt 1) {
$script:UltimatePSProfile.gh_repo_base_folder = [System.Environment]::GetFolderPath('Personal') + "/source/repos/"
}
if ($script:UltimatePSProfile.system_temp.length -lt 1) {
$script:UltimatePSProfile.system_temp = "/tmp/"
}
if ($script:UltimatePSProfile.mydocuments_path.length -lt 1) {
$script:UltimatePSProfile.mydocuments_path = [System.Environment]::GetFolderPath('Personal') + "/"
}
}
}
#--------------------------------------------------------------------------------------
#Create this profile from GH if it does not exist
Function Private:CreateProfileIfNotExist {
if (!(Test-Path -Path $PROFILE)) {
try {
#Use My Documents for the download because Downloads path is not available unless you use pinvoke
Invoke-WebRequest -Uri $script:UltimatePSProfile.psprofile_link -OutFile "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1"
Write-Output "Downloaded profile from $($script:UltimatePSProfile.psprofile_link)"
} catch {
Write-Output "Error downloading profile from $($script:UltimatePSProfile.psprofile_link)"
}
try {
if ($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)WindowsPowerShell" -Force -Verbose
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)PowerShell" -Force -Verbose
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1" -Force -Verbose
} else {
if ($IsLinux) {
try {
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$profile" -Force -Verbose
}
catch {
New-Item -ItemType Directory -Path "$($script:UltimatePSProfile.mydocuments_path).config/powershell/"
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path).config/powershell/" -Force -Verbose
}
}
}
}
catch {
Write-Output "Error copying profile from $($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1 to $($script:UltimatePSProfile.mydocuments_path)\WindowsPowerShell\ or $($script:UltimatePSProfile.mydocuments_path)\PowerShell\"
}
} else {
Write-Output "Profile already exists. Run Install-Profile to update Ultimate PowerShell Profile from $($script:UltimatePSProfile.psprofile_link)"
}
}
#--------------------------------------------------------------------------------------
#Setup command window title. Gets overwritten in Windows Terminal.
Function Private:SetWindowTitle {
$Host.UI.RawUI.WindowTitle = "PowerShell {0} Running in {1}" -f $PSVersionTable.PSVersion.ToString(), $Host.Name
}
#--------------------------------------------------------------------------------------
#Set some PowerShell settings that can help to reduce module import errors
#https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/5.1/Microsoft.PowerShell.Core/About/about_Preference_Variables.md
Function Private:IncreasePowerShell5Counts {
if ($PSVersionTable.PSVersion.Major -eq 5 ) {
$MaximumFunctionCount = 8192
$MaximumVariableCount = 8192
$MaximumAliasCount = 8192
$MaximumHistoryCount = 8192
}
}
#--------------------------------------------------------------------------------------
#Set variables for user and admin status
Function Private:SetAdminStatus {
if ($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
$script:identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$script:principal = New-Object Security.Principal.WindowsPrincipal $script:identity
$script:isAdmin = $script:principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
} else {
if ($IsLinux -and (id -u) -eq 0) {
$script:isAdmin = $true
}
}
}
#--------------------------------------------------------------------------------------
#Set PowerShell Gallery as a trusted source for module installation
Function Private:SetPowerShellGalleryTrust {
if (Get-Module -ListAvailable -Name PowerShellGet) {
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Write-Output "Set PSGallery source (https://www.powershellgallery.com/) as trusted"
} else {
Write-Output "PowerShellGet PowerShell module not installed. Run Install-Module -Name PowerShellGet -Scope AllUsers"
}
}
#--------------------------------------------------------------------------------------
#Setup PSReadLine
Function Private:StartPSReadLine {
if ($host.Name -eq 'ConsoleHost') {
if (-not(Get-Module -ListAvailable -Name PSReadLine)) {
Install-Module PSReadLine -Scope CurrentUser -AllowPrerelease
Import-Module PSReadLine -Scope Local -AllowPrerelease
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
} else {
Write-Output ("PSReadLine {0}" -f (Get-Module PSReadLine).Version.ToString())
}
}
}
#--------------------------------------------------------------------------------------
#Oh-my-posh setup
Function Private:StartOhMyPosh {
if (($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) -and ($host.Name -match "ConsoleHost")) {
if (Get-Command oh-my-posh) {
if ($PSVersionTable.PSVersion.Major -eq 5 ) {
oh-my-posh.exe prompt init powershell --config $script:UltimatePSProfile.oh_my_posh_theme | Invoke-Expression
Enable-PoshTransientPrompt
Write-Output "Enabled oh-my-posh for Windows PowerShell"
}
if ($IsWindows -and $PSVersionTable.PSVersion.Major -eq 7 ) {
oh-my-posh.exe prompt init pwsh --config $script:UltimatePSProfile.oh_my_posh_theme | Invoke-Expression
Enable-PoshTransientPrompt
Write-Output "Enabled oh-my-posh for Windows PowerShell 7"
Write-Output "oh-my-posh theme set to: $($script:UltimatePSProfile.oh_my_posh_theme)"
}
} else {
Write-Output "Oh-my-posh not installed. No fancy prompt for you."
}
} else {
Write-Output "PowerShell 7+ not running in Windows Console Host. Oh-my-posh not available."
}
}
#--------------------------------------------------------------------------------------
#Commands to manage this profile
#Make it easy to edit this profile once it's installed
Function Install-Profile {
try {
#Use My Documents for the download because Downloads path is not available unless you use p/invoke
Invoke-WebRequest -Uri $script:UltimatePSProfile.psprofile_link -OutFile "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1"
Write-Output "Downloaded profile from $($script:UltimatePSProfile.psprofile_link)"
} catch {
Write-Output "Error downloading profile from $($script:UltimatePSProfile.psprofile_link)"
}
try {
if ($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)WindowsPowerShell" -Force -Verbose
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)PowerShell" -Force -Verbose
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1" -Force -Verbose
} else {
if ($IsLinux) {
Copy-Item -Path "$($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1" -Destination "$profile.CurrentUserCurrentHost" -Force -Verbose
}
}
}
catch {
Write-Output "Error copying profile from $($script:UltimatePSProfile.mydocuments_path)Microsoft.PowerShell_profile.ps1 to $($script:UltimatePSProfile.mydocuments_path)WindowsPowerShell or $($script:UltimatePSProfile.mydocuments_path)PowerShell"
}
}
#--------------------------------------------------------------------------------------
#Open this profile in an editor
Function Edit-Profile {
#Determine which editor to use
$isopen = $false
foreach ($editor in $script:UltimatePSProfile.profile_editors) {
Write-Output "Trying to open $($profile.CurrentUserCurrentHost) in $editor"
if (!($isopen) -and ((Get-Command -Name $editor).Length -gt 0)) {
try {
Start-Process $editor -ArgumentList ($profile.CurrentUserCurrentHost)
$isopen = $true
Write-Output "Opened $($profile.CurrentUserCurrentHost) in $editor"
break
}
catch {
<#Do this if a terminating exception happens#>
}
}
}
If ($isopen = $false) {
Write-Output "Could not find a suitable PowerShell script editor on your machine."
}
}
#--------------------------------------------------------------------------------------
#Copy this file from a local GitHub repo folder to the Windows PowerShell and PowerShell folders
#Used when developing the script on a machine for fast testing. Run Copy-ProfilesFromLocalRepo, then Sync-Profile
Function Copy-ProfilesFromLocalRepo {
try {
Write-Output "Copying profile to PowerShell folders."
if ($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
Copy-Item -Path "$($script:UltimatePSProfile.psprofile_repo_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)WindowsPowerShell" -Force -Verbose
Copy-Item -Path "$($script:UltimatePSProfile.psprofile_repo_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)PowerShell" -Force -Verbose
Copy-Item -Path "$($script:UltimatePSProfile.psprofile_repo_path)Microsoft.PowerShell_profile.ps1" -Destination "$($script:UltimatePSProfile.mydocuments_path)WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1" -Force -Verbose
} else {
if ($IsLinux) {
Copy-Item -Path "$($script:UltimatePSProfile.psprofile_repo_path)Microsoft.PowerShell_profile.ps1" -Destination "$profile.CurrentUserCurrentHost" -Force -Verbose
}
}
}
catch {
Write-Output "Error copying profile to PowerShell profile folders."
}
}
#--------------------------------------------------------------------------------------
#Just reload the profile in the current window
Function Sync-Profile {
& $profile
}
Set-Alias -Name Restore-Profile -Value Sync-Profile
#--------------------------------------------------------------------------------------
#Install PowerShell modules if the script execution time is within limit
#Need to run the global module installs in an administrator PowerShell.
#Check to make sure profile load stays fast. If script has executed > the set limit don't worry about installing modules that are not available
function InstallandLoadModules() {
#Just bypassing Linux root user/Global modules for now. Load them all in user/local
if ($IsLinux) {
$result_str = ""
foreach ($module in $script:UltimatePSProfile.linux_modules) {
if (-not(Get-Module -ListAvailable -Name $module)) {
$result_str = $result_str + "$module(Installing) "
Install-Module $module -Scope CurrentUser
Import-Module $module -Scope Local
} else {
$result_str = $result_str + "$module(Available) "
}
}
Write-Output $result_str
} else {
if ($script:isAdmin -eq $true) { #Maybe check if the global modules are available and if they are not, let user know to run as admin to install
$result_str = ""
foreach($global_module in $script:UltimatePSProfile.global_modules) {
if (-not(Get-Module -ListAvailable -Name $global_module)) {
$result_str = $result_str + "$global_module(Installing) "
Install-Module $global_module -Scope AllUsers
Import-Module $global_module -Scope Global
} else {
$result_str = $result_str + "$global_module(Available) "
}
}
}
Write-Output $result_str
$result_str = ""
foreach ($module in $script:UltimatePSProfile.local_modules) {
if (-not(Get-Module -ListAvailable -Name $module)) {
$result_str = $result_str + "$module(Installing) "
Install-Module $module -Scope CurrentUser
Import-Module $module -Scope Local
} else {
$result_str = $result_str + "$module(Available) "
}
}
Write-Output $result_str
}
}
Register-ArgumentCompleter -Native -CommandName az -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$completion_file = New-TemporaryFile
$env:ARGCOMPLETE_USE_TEMPFILES = 1
$env:_ARGCOMPLETE_STDOUT_FILENAME = $completion_file
$env:COMP_LINE = $wordToComplete
$env:COMP_POINT = $cursorPosition
$env:_ARGCOMPLETE = 1
$env:_ARGCOMPLETE_SUPPRESS_SPACE = 0
$env:_ARGCOMPLETE_IFS = "`n"
$env:_ARGCOMPLETE_SHELL = 'powershell'
az 2>&1 | Out-Null
Get-Content $completion_file | Sort-Object | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_)
}
Remove-Item $completion_file, Env:\_ARGCOMPLETE_STDOUT_FILENAME, Env:\ARGCOMPLETE_USE_TEMPFILES, Env:\COMP_LINE, Env:\COMP_POINT, Env:\_ARGCOMPLETE, Env:\_ARGCOMPLETE_SUPPRESS_SPACE, Env:\_ARGCOMPLETE_IFS, Env:\_ARGCOMPLETE_SHELL
}
#Begin script
CreateUltimatePSProfileVars
SetScriptPaths
CreateProfileIfNotExist
SetWindowTitle
IncreasePowerShell5Counts
SetAdminStatus
SetPowerShellGalleryTrust
StartPSReadLine
StartOhMyPosh
#--------------------------------------------------------------------------------------
#Install extra functions if the script is still within load time limit
if ($script:UltimatePSProfile.stopwatch.ElapsedMilliseconds -lt ($script:UltimatePSProfile.max_profileload_seconds * 1000)) {
#Other useful functions
if ($script:isAdmin -eq $true) {
if ($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
Function edithosts {notepad.exe "$env:SystemRoot\System32\drivers\etc\hosts"}
} else {
Function edithosts {nano}
}
$Host.UI.RawUI.WindowTitle += " [ADMIN]"
}
#Function to set the local Python env using pyenv-win
Function Set-PythonVersion($pyver) {
Set-Content -Path .\.python-version -Value $pyver
python -m venv .\.venv
}
Function Activate-Python {
if(Test-Path -Path ".\.venv") {
.\.venv\Scripts\Activate.ps1
} else {
Write-Output "No .venv folder in this folder. Create a Python env with Set-PythonVersion 3.x.x"
}
}
Function os {
if ($IsWindows) {
[System.Environment]::OSVersion
} else {
uname -a
}
}
Function Get-PubIP {
(Invoke-WebRequest http://ifconfig.me/ip ).Content
}
#Compute file hashes - useful for checking successful downloads
Function md5 { Get-FileHash -Algorithm MD5 $args }
Function sha1 { Get-FileHash -Algorithm SHA1 $args }
Function sha256 { Get-FileHash -Algorithm SHA256 $args }
#Does the the rough equivalent of dir /s /b. For example, dirs *.png is dir /s /b *.png
Function dirs {
if ($args.Count -gt 0) {
Get-ChildItem -Recurse -Include "$args" | Foreach-Object FullName
} else {
Get-ChildItem -Recurse | Foreach-Object FullName
}
}
Function Test-CommandExists {
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
try { if(Get-Command $command) { RETURN $true } }
Catch { Write-Host "$command does not exist"; RETURN $false }
Finally { $ErrorActionPreference = $oldPreference }
}
Function find-file($name) {
Get-ChildItem -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object {
$place_path = $_.directory
Write-Output "${place_path}\${_}"
}
}
#--------------------------------------------------------------------------------------
#Functions that only load if a certain OS
if ($IsWindows -or ($PSVersionTable.PSVersion.Major -eq 5)) {
#Linux-Like functions and commands
#Set UNIX-like aliases for the admin command, so sudo <command> will run the command
#with elevated rights.
Set-Alias -Name su -Value admin
Set-Alias -Name sudo -Value admin
Function uptime {
if ($PSVersionTable.PSVersion.Major -eq 5 ) {
Get-WmiObject win32_operatingsystem |
Select-Object @{EXPRESSION={ $_.ConverttoDateTime($_.lastbootuptime)}} | Format-Table -HideTableHeaders
}
if ($IsWindows -and $PSVersionTable.PSVersion.Major -eq 7 ) {
net statistics workstation | Select-String "since" | foreach-object {$_.ToString().Replace('Statistics since ', '')}
}
}
Function touchuni($file) {
"" | Out-File $file -Encoding unicode
}
Function touchutf8($file) {
"" | Out-File $file -Encoding ascii
}
Function df {
Get-Volume
}
Function disks {
Get-PhysicalDisk
}
Function pkill($name) {
Get-Process $name -ErrorAction SilentlyContinue | Stop-Process -Force
}
Function head {
param(
[Parameter(Mandatory=$true)]
[System.IO.FileInfo] $File,
[Parameter(Mandatory=$false)]
[int] $Rows = 10
)
Get-Content -Head $Rows -Path $File
}
Function tail {
param(
[Parameter(Mandatory=$true)]
[System.IO.FileInfo] $File,
[Parameter(Mandatory=$false)]
[int] $Rows = 10
)
Get-Content -Tail $Rows -Path $File
}
Function grepstr {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline = $true)]
[AllowEmptyString()]
[string] $InputObject,
[Parameter(Mandatory=$true)]
[string] $SearchString
)
$InputObject | Select-String -Pattern $SearchString -SimpleMatch
}
Function grepfile {
param(
[Parameter(Mandatory=$true)]
[string] $Path,
[Parameter(Mandatory=$true)]
[string] $SearchString
)
Get-Content -Path $Path | Select-String -Pattern $SearchString -SimpleMatch
}
Function ipconfig {
ipconfig.exe /all
}
Function cleardns {
ipconfig.exe /flushdns
}
Function n { notepad.exe $args }
Function n++ {
$npp=(Get-Itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\notepad++.exe")
Start-Process -FilePath ($npp.'(default)') -ArgumentList $args
}
Function tm { taskmgr.exe }
#Drive shortcuts
Function HKLM: { Set-Location HKLM: }
Function HKCU: { Set-Location HKCU: }
Function Env: { Set-Location Env: }
#Simple Function to start a new elevated process. If arguments are supplied, then
#a single command is started with admin rights; if not, then a new admin instance of PowerShell is started.
Function admin {
if ($PSVersionTable.PSVersion.Major -eq 5 ) {
if ($args.Count -gt 0) {
$argList = "& '" + $args + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $argList
} else {
Start-Process "$psHome\powershell.exe" -Verb runAs
}
}
if ($IsWindows -and $PSVersionTable.PSVersion.Major -eq 7 ) {
if ($args.Count -gt 0) {
$argList = "& '" + $args + "'"
Start-Process "$psHome\pwsh.exe" -Verb runAs -ArgumentList $argList
} else {
Start-Process "$psHome\pwsh.exe" -Verb runAs
}
}
}
}
} else {
Write-Output "Skipping extra setting up PowerShell functions as the profile took > $($script:UltimatePSProfile.max_profileload_seconds) seconds to load."
}
#--------------------------------------------------------------------------------------
#Install modules if the script is still within load time limit and not already available in session
if ($script:UltimatePSProfile.stopwatch.ElapsedMilliseconds -lt ($script:UltimatePSProfile.max_profileload_seconds * 1000)) {
InstallandLoadModules
} else {
Write-Output "Skipping module installs as the profile took > $($script:UltimatePSProfile.max_profileload_seconds) seconds to load."
}
#--------------------------------------------------------------------------------------
#Run the extra profile script for personal settings and commands
Write-Output "Running user's extra script: $($script:UltimatePSProfile.extra_profile_script)"
if (($script:UltimatePSProfile.extra_profile_script.length -gt 0) -and (Test-Path -Path $script:UltimatePSProfile.extra_profile_script)) {
. $script:UltimatePSProfile.extra_profile_script
}
#End of script
#Remind user which functions are available in the console
Write-Output "The following functions were set by profile:"
Get-ChildItem -Path Function:\ | Where-Object{$_.Source.ToString().Length -lt 1} | Select-Object Name | Format-Wide -AutoSize
#Don't forget to stop the stopwatch
$script:UltimatePSProfile.stopwatch.stop()
if ($IsLinux) {
Write-Output "Loading personal and system profiles took $($script:UltimatePSProfile.stopwatch.ElapsedMilliseconds) ms."
[Environment]::GetEnvironmentVariable('MOTD_SHOWN')
}