Skip to content

Instantly share code, notes, and snippets.

@fabricesemti80
Last active February 25, 2022 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabricesemti80/4b1d84458d3d0cf245ca1cfcbbc0c19b to your computer and use it in GitHub Desktop.
Save fabricesemti80/4b1d84458d3d0cf245ca1cfcbbc0c19b to your computer and use it in GitHub Desktop.
my-powershell-profile

How to use the two files

# urls
$modern_gist = "https://gist.githubusercontent.com/fabricesemti80/4b1d84458d3d0cf245ca1cfcbbc0c19b/raw/c86a7f0bfb6174dfec1c517166c2754aba496dde/modern-profile.ps1"
$legacy_gist = "https://gist.githubusercontent.com/fabricesemti80/4b1d84458d3d0cf245ca1cfcbbc0c19b/raw/12447d7faf1099c30c81dfead472ab98f60aa204/legacy-profile.ps1"
# execute
Invoke-WebRequest $modern_gist -OutFile $profile.CurrentUserAllHosts 
Invoke-WebRequest $legacy_gist -OutFile "$env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1"
Invoke-WebRequest $legacy_gist -OutFile "$env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"

These few lines (please make sure the gist URL-s are up-to date; check them here: https://gist.github.com/fabricesemti80/4b1d84458d3d0cf245ca1cfcbbc0c19b) should replace your PowerShell profile with a custom one (according to my preference). From there you can adjust them.

  • the moder one works with the more modern tools (VSCode, Windows Terminal)

  • the modern one would work with the legacy tools (ISE, built-in PS), but even if you change the font type, ISE would still display just a bunch of numbers instead of the symbols, so you might want to use a different - not equivalent! - approach

Starship

As the modern profile currently uses starship, you may want to visit the site of this excelent tool! (I do not take credit for it or affiliated to them - just my prefernce, since it works on Win and Unix systems as well ;-) )

Disclaimer

This is not the final form, I might adjust my settings. As I made this for myself - in an effort to standardise my env in work and home computers - use it at your own risk!

<#
#? ref: https://hodgkins.io/ultimate-powershell-prompt-and-git-setup
https://github.com/joonro/Get-ChildItemColor
https://github.com/dahlbyk/posh-git
#>
#* Import modules
<#
https://github.com/devblackops/Terminal-Icons
#>
$modules = @('posh-git')#, 'Get-ChildItemColor', 'Terminal-Icons')
foreach ($module in $modules) {
if (-not (Get-Module -Name $module -ListAvailable)) {
Install-Module -Name $module -Repository PSGallery -Force -AllowClobber
}
}
foreach ($modul in $modules) {
Import-Module -Name $module -Force
}
#* Test if admin
function Test-Administrator {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
#* Check if admin
function prompt {
$realLASTEXITCODE = $LASTEXITCODE
Write-Host
## Reset color, which can be messed up by Enable-GitColors
# $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor
if (Test-Administrator) {
# Use different username if elevated
Write-Host '(Elevated) ' -NoNewline -ForegroundColor Red
}
Write-Host "$ENV:USERNAME" -NoNewline -ForegroundColor DarkYellow
Write-Host ' | ' -NoNewline
Write-Host "ps:$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" -NoNewline -ForegroundColor White
Write-Host ' | ' -NoNewline
Write-Host "$ENV:COMPUTERNAME" -NoNewline -ForegroundColor Yellow
if ($null -ne $s) {
# color for PSSessions
Write-Host " (`$s: " -NoNewline -ForegroundColor White
Write-Host "$($s.Name)" -NoNewline -ForegroundColor DarkYellow
Write-Host ') ' -NoNewline -ForegroundColor White
}
Write-Host ' | ' -NoNewline
Write-Host $($(Get-Location) -replace ($env:USERPROFILE).Replace('\', '\\'), '~') -NoNewline -ForegroundColor Green
Write-Host ' | ' -NoNewline
Write-Host (Get-Date -Format G) -NoNewline -ForegroundColor Magenta
Write-Host ' : ' -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE
Write-VcsStatus
Write-Host ''
return '> '
}
#* Colorize get childitem
Set-Alias ls Get-ChildItemColor -Option AllScope -Force
Set-Alias dir Get-ChildItemColor -Option AllScope -Force
#* Set location to home
Set-Location -Path ~\Documents
<#
#? ref:
https://hodgkins.io/ultimate-powershell-prompt-and-git-setup
https://github.com/joonro/Get-ChildItemColor
https://github.com/dahlbyk/posh-git
https://stackoverflow.com/questions/34820975/git-clone-redirect-stderr-to-stdout-but-keep-errors-being-written-to-stderr/47232450#47232450
https://github.com/devblackops/Terminal-Icons
https://starship.rs/installing/#chocolatey
#>
#* --------------------------------------------------------------------------- Import modules
$modules = @('posh-git', 'Get-ChildItemColor', 'Terminal-Icons')
foreach ($module in $modules) {
if (-not (Get-Module -Name $module -ListAvailable)) {
Install-Module -Name $module -Scope CurrentUser -Repository PSGallery -Force -AllowClobber
}
}
foreach ($modul in $modules) {
Import-Module -Name $module -Force
}
#* --------------------------------------------------------------------------- Update the prompt
Invoke-Expression (&starship init powershell)
#* --------------------------------------------------------------------------- Colorize get childitem
Set-Alias ls Get-ChildItemColor -Option AllScope -Force
Set-Alias dir Get-ChildItemColor -Option AllScope -Force
#* --------------------------------------------------------------------------- Set up env
$env:GIT_REDIRECT_STDERR = '2>&1'
Set-Location -Path ~\Documents
#* --------------------------------------------------------------------------- Change window title
$currentTitle = $Host.UI.RawUI.WindowTitle; $Host.UI.RawUI.WindowTitle = $(whoami) + '@' + $(hostname) + " [$currentTitle]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment