修改powershell颜色
默认的powershell启动时加载配置文件可以在以下路径中寻找,如没有 PowerShell_profile.ps1 或者 Profile.ps1 在相应的路径下新建即可。
$Home:当前使用者的主目录
$PsHome:存放 Windows PowerShell 的目录
# 当前用户,所有主机有效
$Home\Documents\Profile.ps1
# 当前用户,当前主机有效
$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
# 所有用户,当前主机有效
$PsHome\Microsoft.PowerShell_profile.ps1
# 所有用户,所有主机有效
$PsHome\Profile.ps1
- 文件内容如下
set-alias ll Get-ChildItemColor
function prompt
{
$my_path = $(get-location).toString()
$my_pos = ($my_path).LastIndexOf("\") + 1
if( $my_pos -eq ($my_path).Length ) { $my_path_tail = $my_path }
else { $my_path_tail = ($my_path).SubString( $my_pos, ($my_path).Length - $my_pos ) }
Write-Host ("♠ ") -nonewline -foregroundcolor 'DarkCyan'
Write-Host ($my_path_tail) -nonewline -foregroundcolor 'DarkCyan'
Write-Host (" $") -nonewline -foregroundcolor 'Magenta'
return " "
}
function Get-ChildItemColor {
<#
.Synopsis
Returns childitems with colors by type.
.Description
This function wraps Get-ChildItem and tries to output the results
color-coded by type:
Directories - Cyan
Compressed - Red
Executables - Green
Text Files - Gray
Image Files - Magenta
Others - Gray
.ReturnValue
All objects returned by Get-ChildItem are passed down the pipeline
unmodified.
.Notes
NAME: Get-ChildItemColor
AUTHOR: Tojo2000 <tojo2000@tojo2000.com>
#>
$regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase `
-bor [System.Text.RegularExpressions.RegexOptions]::Compiled)
$fore = $Host.UI.RawUI.ForegroundColor
$compressed = New-Object System.Text.RegularExpressions.Regex(\.(zip|tar|gz|rar|7z|tgz|bz2),$regex_opts)
$executable = New-Object System.Text.RegularExpressions.Regex(\.(exe|bat|cmd|py|pl|ps1|psm1|vbs|rb|reg|sh), $regex_opts)
$text_files = New-Object System.Text.RegularExpressions.Regex(\.(txt|cfg|conf|ini|csv|log), $regex_opts)
$image_files = New-Object System.Text.RegularExpressions.Regex(\.(bmp|jpg|png|gif|jpeg), $regex_opts)
Invoke-Expression ("Get-ChildItem $args") |
%{
if ($_.GetType().Name -eq 'DirectoryInfo') { $Host.UI.RawUI.ForegroundColor = 'Cyan' }
elseif ($compressed.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Red' }
elseif ($executable.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Green' }
elseif ($text_files.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Gray' }
elseif ($image_files.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Magenta' }
else { $Host.UI.RawUI.ForegroundColor = 'Gray' }
echo $_
$Host.UI.RawUI.ForegroundColor = $fore
}
}
function Show-Color( [System.ConsoleColor] $color )
{
$fore = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = $color
echo ($color).toString()
$Host.UI.RawUI.ForegroundColor = $fore
}
function Show-AllColor
{
Show-Color('Black')
Show-Color('DarkBlue')
Show-Color('DarkGreen')
Show-Color('DarkCyan')
Show-Color('DarkRed')
Show-Color('DarkMagenta')
Show-Color('DarkYellow')
Show-Color('Gray')
Show-Color('DarkGray')
Show-Color('Blue')
Show-Color('Green')
Show-Color('Cyan')
Show-Color('Red')
Show-Color('Magenta')
Show-Color('Yellow')
Show-Color('White')
}
网友评论