自定义炫酷powershell

自定义炫酷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
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Powershell是一种由微软开发的命令行Shell和脚本语言。它是Windows操作系统中的一种强大的管理工具,可以帮助用户自动化任务、管理系统、编写脚本等。 如果你的计算机运行的是Windows 8或更高版本,那么你应该已经安装了名为Windows PowerShellPowerShell版本。它与最新版本的PowerShell略有不同,但仍可用于学习和使用。 要启动PowerShell,有几种方法可以使用。一种方法是直接在命令提示符(cmd)中执行powershell命令,这将切换到PowerShell环境。另一种方法是在开始菜单中输入powershell并启动PowerShell应用程序。还可以通过开始菜单中的"附件"文件夹中的"PowerShell"启动PowerShell。 使用PowerShell,你可以执行各种系统管理任务,例如管理文件和文件夹、配置网络设置、安装和卸载软件等。你还可以编写和运行自己的脚本,以自动化重复性的任务。PowerShell提供了丰富的命令和功能,使得管理和配置Windows系统变得更加高效和便捷。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [PowerShell简介](https://blog.csdn.net/zaoyuxuan/article/details/129052643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Windows PowerShell命令大全](https://blog.csdn.net/zx77588023/article/details/120148287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值