workstation/roles/home-cli/files/Microsoft.PowerShell_profile.ps1

162 lines
6.3 KiB
PowerShell

# Put this in the location of $PROFILE. For PowerShell 5+ this is either
# C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
# or
# C:\Users\USERNAME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
function Prompt {
if ($IsWindows -or ($env:OS -eq "Windows_NT")) {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
return "${env:USERNAME}@$(${env:COMPUTERNAME}.ToLower())# "
} else {
return "${env:USERNAME}@$(${env:COMPUTERNAME}.ToLower())> "
}
} elseif ($IsLinux) {
return "$(id -un)@$(hostname -s)> "
} else {
return "PS> "
}
}
function ADUserInfo {
param (
[Parameter(Mandatory)]
[string]$User
)
Import-Module ActiveDirectory -ErrorAction Stop
$ADPropertiesCommon = @(
'SamAccountName'
'UserPrincipalName'
'DisplayName'
'Enabled'
'PasswordExpired'
'LockedOut'
'CannotChangePassword'
'PasswordNeverExpires'
'GivenName'
'Surname'
'DistinguishedName'
'EmployeeID'
'Title'
'Department'
'Created'
'Modified'
'PasswordLastSet'
)
$ADPropertiesGetOnly = @(
'msDS-UserPasswordExpiryTimeComputed'
)
$ADPropertiesPrintOnly = @(
@{Name = "PasswordExpiresDate"; Expression = { [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed") } }
)
$ADPropertiesGet = $ADPropertiesCommon + $ADPropertiesGetOnly
$ADPropertiesPrint = $ADPropertiesCommon + $ADPropertiesPrintOnly
Get-ADUser -Properties $ADPropertiesGet -Identity $User | Select-Object -Property $ADPropertiesPrint
}
function mkcd {
param (
[Parameter(Mandatory)]
[string]$Path
)
New-Item -ItemType Directory -Path $Path
Set-Location -Path $Path
}
function Set-AcpLocale {
# Set the default en-GB locale.
Set-WinUserLanguageList -LanguageList en-GB -Force
# Get the default locale back.
$AcpLocale = Get-WinUserLanguageList
# Remove the British layout and add the US layout.
$AcpLocale[0].InputMethodTips.Remove("0809:00000809") | Out-Null
$AcpLocale[0].InputMethodTips.Add("0809:00000409") | Out-Null
# Set the locale to our customised version.
Set-WinUserLanguageList -LanguageList $AcpLocale -Force
# Clean up.
Remove-Variable AcpLocale
}
if ($IsWindows -or ($env:OS -eq "Windows_NT")) {
function who {
& "$env:SystemRoot\System32\query.exe" user
}
# Drop elevation privileges.
$env:__COMPAT_LAYER = "RunAsInvoker"
}
if ($nvim = Get-Command nvim.exe -ErrorAction SilentlyContinue) {
Set-Alias -Name vi -Value $nvim.Source
}
Remove-Variable -Name nvim
# Force UTF-8 output.
$OutputEncoding = [System.Text.Encoding]::UTF8
Set-PSReadLineOption -BellStyle None
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineOption -HistorySaveStyle SaveNothing
Remove-PSReadLineKeyHandler -Chord Tab
Set-PSReadLineKeyHandler -Chord UpArrow -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchBackward()
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
Set-PSReadLineKeyHandler -Chord DownArrow -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchForward()
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
Set-PSReadLineKeyHandler -Chord Ctrl+LeftArrow -Function BackwardWord
Set-PSReadLineKeyHandler -Chord Ctrl+RightArrow -Function ForwardWord
Set-PSReadLineKeyHandler -Chord Ctrl+Backspace -Function BackwardKillWord
Set-PSReadLineKeyHandler -Chord Ctrl+Delete -Function KillWord
Set-PSReadLineKeyHandler -Chord Shift+Spacebar -Function SelfInsert
# Colours for a light-coloured terminal.
# https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/using-light-theme?view=powershell-7.2
if ($null -ne $PSStyle) {
$ISETheme = @{
Command = $PSStyle.Foreground.FromRGB(0x0000FF)
Comment = $PSStyle.Foreground.FromRGB(0x006400)
ContinuationPrompt = $PSStyle.Foreground.FromRGB(0x0000FF)
Default = $PSStyle.Foreground.FromRGB(0x0000FF)
Emphasis = $PSStyle.Foreground.FromRGB(0x287BF0)
Error = $PSStyle.Foreground.FromRGB(0xE50000)
InlinePrediction = $PSStyle.Foreground.FromRGB(0x93A1A1)
Keyword = $PSStyle.Foreground.FromRGB(0x00008b)
#ListPrediction = $PSStyle.Foreground.FromRGB(0x06DE00)
Member = $PSStyle.Foreground.FromRGB(0x000000)
Number = $PSStyle.Foreground.FromRGB(0x800080)
Operator = $PSStyle.Foreground.FromRGB(0x757575)
Parameter = $PSStyle.Foreground.FromRGB(0x000080)
String = $PSStyle.Foreground.FromRGB(0x8b0000)
Type = $PSStyle.Foreground.FromRGB(0x008080)
Variable = $PSStyle.Foreground.FromRGB(0xff4500)
#ListPredictionSelected = $PSStyle.Background.FromRGB(0x93A1A1)
Selection = $PSStyle.Background.FromRGB(0x00BFFF)
}
Set-PSReadLineOption -Colors $ISETheme
$PSStyle.Formatting.FormatAccent = "`e[32m"
$PSStyle.Formatting.TableHeader = "`e[32m"
$PSStyle.Formatting.ErrorAccent = "`e[36m"
$PSStyle.Formatting.Error = "`e[31m"
$PSStyle.Formatting.Warning = "`e[33m"
$PSStyle.Formatting.Verbose = "`e[33m"
$PSStyle.Formatting.Debug = "`e[33m"
$PSStyle.Progress.Style = "`e[33m"
$PSStyle.FileInfo.Directory = $PSStyle.Background.FromRgb(0x2f6aff) +
$PSStyle.Foreground.BrightWhite
$PSStyle.FileInfo.SymbolicLink = "`e[36m"
$PSStyle.FileInfo.Executable = "`e[95m"
$PSStyle.FileInfo.Extension['.ps1'] = "`e[36m"
$PSStyle.FileInfo.Extension['.ps1xml'] = "`e[36m"
$PSStyle.FileInfo.Extension['.psd1'] = "`e[36m"
$PSStyle.FileInfo.Extension['.psm1'] = "`e[36m"
Remove-Variable ISETheme
}
# Keep this at the end.
if (Test-Path "$PSScriptRoot/local.ps1") {
. "$PSScriptRoot/local.ps1"
}