日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义炫酷powershell

發布時間:2023/12/31 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义炫酷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.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

僅當配置文件的路徑和文件名與 profileprofile 變量中指定的文件名將該文件保存到在此變量中指定的路徑下。

如果在記事本中創建配置文件,請將文件名用引號括起來,以保留 PS1 文件擴展名。例如:

"Microsoft.PowerShell_profile.ps1"

如果沒有引號,則記事本會將 .txt 文件擴展名追加到文件,而 Windows PowerShell 將無法識別它。

使用配置文件存儲日常使用的別名、函數和變量。一個非常有用的函數會在您最喜愛的文本編輯器中打開用戶配置文件。例如,以下命令會創建一個名為 pro 的函數,該函數用于在記事本中打開用戶配置文件。

function pro { notepad $profile }

有了設計良好的配置文件,就可以更輕松地使用 Windows PowerShell 和管理系統

總結

以上是生活随笔為你收集整理的自定义炫酷powershell的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。