自定义炫酷powershell
来源:互联网 发布:淘宝分销什么意思 编辑:程序博客网 时间:2024/06/04 18:34
自定义炫酷powershell(美化)
- linux上的bash和zsh之类的命令行终端炫酷无比。
- window上的cmd和powershell丑的不忍直视。
- 很久之前不知参考谁的一篇文章自定义了一下,还算勉强能看得过去。重装电脑时候发现了,便记录一下。
- 自定义代码不怎么难,谁要是有时间精力去github上专门开一个项目,肯定能收获很多 star !!
怎么使用自定义配置文件不多说了,请看本文最后面,或者官方文档
https://technet.microsoft.com/zh-cn/library/bb613488
先看效果
直接上干货
#以后要 使用 ll 而不是 ls了。set-alias ll Get-ChildItemColor function prompt { # $my_path 获取当前所在目录 $my_path = $(get-location).toString() $my_pos = ($my_path).LastIndexOf("\") + 1 # 下面的 if-else 语句用来获得文件路径的最后一个目录名 # 比如 c:/user/xiaoming , 则 $my_path_tail 的内容是 xiaoming # 主要为了命令行终端的提示简洁一些, 根据需要自己修改 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 定义了终端提示格式。 Write-Host ("[") -nonewline -foregroundcolor 'Cyan' Write-Host ("Blueky") -nonewline -foregroundcolor 'Cyan' Write-Host (" @ ") -nonewline -foregroundcolor 'Cyan' Write-Host ("WIN10 ") -nonewline -foregroundcolor 'Cyan' Write-Host ($my_path_tail) -nonewline -foregroundcolor 'Cyan' Write-Host ("]#") -nonewline -foregroundcolor 'Cyan' 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: blueky #> # 这个函数用来做正则匹配,并为不同的文件配置不同的颜色。 $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 } # 在powershell终端里面输入 show-allcolor 就可以查看颜色,用来帮助自定义颜色主题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') }
- 我使用了“仅本用户”的配置文件,在我的文档–>>WindowsPowerShell文件夹下,配置文件的文件名叫Microsoft.PowerShell_profile.ps1
- 把上面的代码放到配置文件里,重新启动powershell就好了。
- 记得把powershell窗口的 属性–>颜色–>屏幕背景 颜色改成 48,9,36 (RGB: 48,9,36 是Ubuntu终端的背景色,这个看喜好修改。)
下面是有关powershell初始化配置文件(相当于.bashrc之类的)基础概括:
了解配置文件
在 Windows PowerShell 中可以有四个不同的配置文件。配置文件按加载顺序列出。较特定的配置文件优先于较不特定的配置文件(如果它们适用)。
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
此配置文件适用于所有用户和所有 shell。%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
此配置文件适用于所有用户,但仅适用于 Microsoft.PowerShell shell。%UserProfile%\My Documents\WindowsPowerShell\profile.ps1
此配置文件仅适用于当前用户,但会影响所有 shell。%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
此配置文件仅适用于当前用户和 Microsoft.PowerShell shell。
创建配置文件
在创建或者导入变量、别名或函数,或者添加 Windows PowerShell 管理单元时,这些元素只是添加到当前会话中。如果退出该会话或者关闭窗口,这些元素将丢失。
若要保存经常使用的变量、别名、函数和命令并使它们可以在每个 Windows PowerShell 会话中使用,请将它们添加到 Windows PowerShell 配置文件中。
还可以创建、共享和分发配置文件,以便在较大的企业中强制实施 Windows PowerShell 的统一视图。
Windows PowerShell 配置文件不是自动创建的。若要创建配置文件,请在指定位置中创建具有指定名称的文本文件。通常,将使用特定于用户、特定于 shell 的配置文件,这种配置文件称为 Windows PowerShell 用户配置文件。此配置文件的位置存储在 $profile 变量中。
若要显示 Windows PowerShell 配置文件的路径,请键入:
$profile
若要确定是否已经在系统上创建了 Windows PowerShell 配置文件,请键入:
test-path $profile
如果存在配置文件,则响应为 True:否则响应为 False。
若要创建 Windows PowerShell 配置文件,请键入:
new-item -path $profile -itemtype file -force
若要在记事本中打开配置文件,请键入:
notepad $profile
若要创建其他配置文件之一,如适用于所有用户和所有 shell 的配置文件,请键入:
new-item -path $env:windir\System32\WindowsPowerShell\v1.0\profile.ps1 -itemtype file -force
仅当配置文件的路径和文件名与
如果在记事本中创建配置文件,请将文件名用引号括起来,以保留 PS1 文件扩展名。例如:
"Microsoft.PowerShell_profile.ps1"
如果没有引号,则记事本会将 .txt 文件扩展名追加到文件,而 Windows PowerShell 将无法识别它。
使用配置文件存储日常使用的别名、函数和变量。一个非常有用的函数会在您最喜爱的文本编辑器中打开用户配置文件。例如,以下命令会创建一个名为 pro 的函数,该函数用于在记事本中打开用户配置文件。
function pro { notepad $profile }
有了设计良好的配置文件,就可以更轻松地使用 Windows PowerShell 和管理系统
- 自定义炫酷powershell
- 自定义powershell模块
- 用PowerShell自定义版本控制
- 使用自定义命令扩展 Windows PowerShell
- PowerShell如何使用自定义公共函数
- PowerShell
- PowerShell
- PowerShell
- PowerShell
- PowerShell
- PowerShell
- PowerShell
- powershell
- Powershell
- PowerShell
- Powershell
- powershell
- PowerShell
- 动态规划之四
- Enumeration与Iterator迭代器的比较分析学习记录
- 微信每天处理2.05亿通话 运营商只剩卖流量
- 解决maven 中bean is not defined问题
- Centos7安装Redis单机版和集群
- 自定义炫酷powershell
- leetcode: 34. Search for a Range
- Spring MVC简单执行流程
- 数据结构实验之二叉树一:树的同构
- Python 常见类型错误
- 5.5
- Getchar函数
- objective,top1 error,top5error
- leetcode: 35. Search Insert Position