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

220 lines
8.5 KiB
PowerShell
Raw Normal View History

2022-01-18 11:48:49 +00:00
# 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")) {
2022-03-02 11:56:41 +00:00
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
2024-11-19 12:18:03 +00:00
return "# "
}
2022-09-08 15:10:57 +00:00
}
2024-11-19 12:18:03 +00:00
return "> "
2022-01-18 11:48:49 +00:00
}
2022-03-31 11:51:03 +00:00
function ADUserInfo {
2023-06-26 14:16:30 +00:00
param (
[Parameter(Mandatory)]
[string]$User
2022-03-31 11:51:03 +00:00
)
Import-Module ActiveDirectory -ErrorAction Stop
$ADPropertiesCommon = @(
'SamAccountName'
'UserPrincipalName'
'DisplayName'
'Enabled'
'PasswordExpired'
'LockedOut'
'CannotChangePassword'
'PasswordNeverExpires'
'GivenName'
'Surname'
'DistinguishedName'
'EmployeeID'
'Title'
'Department'
'Created'
'Modified'
2022-04-01 13:35:40 +00:00
'PasswordLastSet'
2022-03-31 11:51:03 +00:00
)
$ADPropertiesGetOnly = @(
'msDS-UserPasswordExpiryTimeComputed'
)
$ADPropertiesPrintOnly = @(
2023-06-26 14:16:30 +00:00
@{Name = "PasswordExpiresDate"; Expression = { [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed") } }
2022-03-31 11:51:03 +00:00
)
$ADPropertiesGet = $ADPropertiesCommon + $ADPropertiesGetOnly
$ADPropertiesPrint = $ADPropertiesCommon + $ADPropertiesPrintOnly
Get-ADUser -Properties $ADPropertiesGet -Identity $User | Select-Object -Property $ADPropertiesPrint
}
2023-10-03 07:55:07 +00:00
function mkcd {
2023-06-26 14:16:30 +00:00
param (
[Parameter(Mandatory)]
[string]$Path
2023-05-09 13:43:56 +00:00
)
New-Item -ItemType Directory -Path $Path
Set-Location -Path $Path
}
2024-06-05 09:21:07 +00:00
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.
2024-06-05 09:24:45 +00:00
$AcpLocale[0].InputMethodTips.Remove("0809:00000809") | Out-Null
$AcpLocale[0].InputMethodTips.Add("0809:00000409") | Out-Null
2024-06-05 09:21:07 +00:00
# Set the locale to our customised version.
Set-WinUserLanguageList -LanguageList $AcpLocale -Force
# Clean up.
Remove-Variable AcpLocale
}
2023-10-03 07:55:07 +00:00
if ($IsWindows -or ($env:OS -eq "Windows_NT")) {
function who {
& "$env:SystemRoot\System32\query.exe" user
}
2024-01-11 11:30:53 +00:00
# Drop elevation privileges.
$env:__COMPAT_LAYER = "RunAsInvoker"
2023-10-03 07:55:07 +00:00
}
2024-03-18 13:14:45 +00:00
if ($nvim = Get-Command nvim.exe -ErrorAction SilentlyContinue) {
Set-Alias -Name vi -Value $nvim.Source
}
Remove-Variable -Name nvim
2024-01-17 08:02:51 +00:00
# Force UTF-8 output.
$OutputEncoding = [System.Text.Encoding]::UTF8
2024-06-10 06:31:07 +00:00
Set-PSReadLineOption -BellStyle None
Set-PSReadLineOption -EditMode Emacs
2024-07-01 15:38:19 +00:00
Set-PSReadLineOption -HistorySaveStyle SaveNothing
2024-09-24 07:19:22 +00:00
Remove-PSReadLineKeyHandler -Chord Tab
Set-PSReadLineKeyHandler -Chord UpArrow -ScriptBlock {
2022-03-02 11:56:41 +00:00
[Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchBackward()
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
2022-01-18 11:48:49 +00:00
}
2024-09-24 07:19:22 +00:00
Set-PSReadLineKeyHandler -Chord DownArrow -ScriptBlock {
2022-03-02 11:56:41 +00:00
[Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchForward()
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
2022-01-18 11:48:49 +00:00
}
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
2022-03-02 11:56:41 +00:00
2025-01-27 11:48:35 +00:00
# Set terminal to light mode.
foreach ($Property in $Host.PrivateData.PSObject.Properties.Name) {
if ($Property -match '.*ForegroundColor$') {
$Host.PrivateData.$Property = $Host.UI.RawUI.ForegroundColor
}
if ($Property -match '.*BackgroundColor$') {
$Host.PrivateData.$Property = $Host.UI.RawUI.BackgroundColor
}
}
2025-02-13 10:41:49 +00:00
function Set-AntDarkMode {
if ($PSVersionTable.PSVersion.Major -le 5) {
# $PSStyle doesn't exist in PowerShell 5.1, so import the external module.
Import-Module -Name PSStyle
}
$DefaultTheme = @{
2025-02-13 12:08:53 +00:00
Command = "$([char]0x1b)[93m"
Comment = "$([char]0x1b)[32m"
ContinuationPrompt = "$([char]0x1b)[33m"
Default = "$([char]0x1b)[33m"
Emphasis = "$([char]0x1b)[96m"
Error = "$([char]0x1b)[91m"
InlinePrediction = "$([char]0x1b)[38;5;238m"
Keyword = "$([char]0x1b)[92m"
ListPrediction = "$([char]0x1b)[33m"
ListPredictionSelected = "$([char]0x1b)[48;5;238m"
ListPredictionTooltip = "$([char]0x1b)[38;5;238m"
Member = "$([char]0x1b)[37m"
Number = "$([char]0x1b)[97m"
Operator = "$([char]0x1b)[90m"
Parameter = "$([char]0x1b)[90m"
Selection = "$([char]0x1b)[35;43m"
String = "$([char]0x1b)[36m"
Type = "$([char]0x1b)[37m"
Variable = "$([char]0x1b)[92m"
2025-02-13 10:41:49 +00:00
}
2025-02-13 12:08:53 +00:00
$PSStyle.Formatting.FormatAccent = "$([char]0x1b)[32;1m"
$PSStyle.Formatting.TableHeader = "$([char]0x1b)[32;1m"
$PSStyle.Formatting.ErrorAccent = "$([char]0x1b)[36;1m"
$PSStyle.Formatting.Error = "$([char]0x1b)[31;1m"
$PSStyle.Formatting.Warning = "$([char]0x1b)[33;1m"
$PSStyle.Formatting.Verbose = "$([char]0x1b)[33;1m"
$PSStyle.Formatting.Debug = "$([char]0x1b)[33;1m"
$PSStyle.Progress.Style = "$([char]0x1b)[33;1m"
$PSStyle.FileInfo.Directory = "$([char]0x1b)[44;1m"
$PSStyle.FileInfo.SymbolicLink = "$([char]0x1b)[36;1m"
$PSStyle.FileInfo.Executable = "$([char]0x1b)[32;1m"
2025-02-13 10:41:49 +00:00
Set-PSReadLineOption -Colors $DefaultTheme
}
2025-02-13 09:58:56 +00:00
function Set-AntLightMode {
# https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/using-light-theme
if ($PSVersionTable.PSVersion.Major -le 5) {
# $PSStyle doesn't exist in PowerShell 5.1, so import the external module.
Import-Module -Name PSStyle
}
$ISETheme = @{
2025-02-13 12:08:53 +00:00
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)
2025-02-13 09:58:56 +00:00
}
2025-02-13 10:41:49 +00:00
2025-02-13 12:08:53 +00:00
$PSStyle.Formatting.FormatAccent = "$([char]0x1b)[32m"
$PSStyle.Formatting.TableHeader = "$([char]0x1b)[32m"
$PSStyle.Formatting.ErrorAccent = "$([char]0x1b)[36m"
$PSStyle.Formatting.Error = "$([char]0x1b)[31m"
$PSStyle.Formatting.Warning = "$([char]0x1b)[33m"
$PSStyle.Formatting.Verbose = "$([char]0x1b)[33m"
$PSStyle.Formatting.Debug = "$([char]0x1b)[33m"
$PSStyle.Progress.Style = "$([char]0x1b)[33m"
$PSStyle.FileInfo.Directory = $PSStyle.Background.FromRgb(0x2f6aff) +
$PSStyle.Foreground.BrightWhite
$PSStyle.FileInfo.SymbolicLink = "$([char]0x1b)[36m"
$PSStyle.FileInfo.Executable = "$([char]0x1b)[95m"
2025-02-13 09:58:56 +00:00
if ($PSVersionTable.PSVersion.Major -gt 5) {
2025-02-13 12:08:53 +00:00
$PSStyle.FileInfo.Extension['.ps1'] = "$([char]0x1b)[36m"
2025-02-13 10:41:49 +00:00
$PSStyle.FileInfo.Extension['.ps1xml'] = "$([char]0x1b)[36m"
2025-02-13 12:08:53 +00:00
$PSStyle.FileInfo.Extension['.psd1'] = "$([char]0x1b)[36m"
$PSStyle.FileInfo.Extension['.psm1'] = "$([char]0x1b)[36m"
2025-01-27 11:48:35 +00:00
}
2025-02-13 10:41:49 +00:00
2025-02-13 09:58:56 +00:00
Set-PSReadLineOption -Colors $ISETheme
2025-01-27 11:48:35 +00:00
}
2025-02-13 09:58:56 +00:00
Set-AntLightMode
2025-01-27 11:48:35 +00:00
2023-06-26 14:16:42 +00:00
# Keep this at the end.
2024-04-26 09:19:50 +00:00
if (Test-Path "$PSScriptRoot/local.ps1") {
. "$PSScriptRoot/local.ps1"
2023-06-26 14:16:42 +00:00
}