Skip to content

Powershell101

Updated: at 17:51

探索 Powershell

配置 powershell

  1. 安装 powershell

  2. 配置 windows terminal

  3. 安装 scoop - A command-line installer for Windows

  4. 安装 Winget - Install and manage applications

  5. 安装 git for windows - Offer a lightweight, native set of tools

    winget install -e --id Git.Git
  6. 安装 gccneovim

    scoop install gcc neovim
  7. powershell 配置文件 - about _profiles

    The $PROFILE variable

    The $profile automatic variable stores the paths to the Powershell profiles that are available in the current session.

    The $PROFILE variable stores the path to the “Current User, Current Host” profile. The other profiles are saved in note properties of the $PROFILE variable.

    $profile

    
    Split-Path $PROFILE.CurrentUserCurrentHOST
    
    mkdir ~/.config/powershell
    nvim ~/.config/powershell/user_profile.ps1

    创建 $PROFILE.CurrentUserCurrentHost 文件 ,并添加 . $env:USEPROFILE\.config\powershell\user_profile.ps1 内容,重新启动终端,这时配置文件已生效。

    # Create a profile
    New-Item $PROFILE.CurrentUserCurrentHost
    
    # or 
    nvim $PROFILE
  8. 安装 posh-gitoh-my-posh

    • Prompt for Git repositories
    • A prompt theme engine for any shell
    Install-Module posh-git -Scope CurrentUser -Force
    Install-Module oh-my-posh -Scope CurrentUser -Force

    编辑 ~/.config/powershell/user-profile.ps1

    # Prompt
    Import-Module posh-git
    Import-Module oh-my-posh
    Set-PoshPrompt <themename>
  9. 自定义 prompt

  10. 安装 nvm

    scoop install nvm
  11. 安装 terminal icons

    # Install-Module
    Install-Module -Name Terminal-Icons -Repository PSGallery -Force
    
    # or use scoop
    scoop bucket add extras
    scoop install terminal-icons
    
    Import-Module Terminal-Icons
  12. 安装 z

    Install-Module -Name z -Force 
  13. 安装 PSReadLine - autocompletion

    How to use PSReadLine

    Install-Module -Name PowerShellGet -Force
    Install-Module -Name PSReadLine -Force -SkipPublisherCheck -AllowPrerelease

    To start using, just import the module:

    Import-Module PSReadLine

    To view the current key bindings:

    Get-PSReadLineKeyHandler

    To use Emacs key bindings, you can use:

    Set-PSReadLineOption -EditMode Emacs

    Specifies the source for PSReadLine to get predictive suggestions.

    Set-PSReadLineOption -PredictionSource Hisory

    Sets the style for the display of the predictive text. The default is InlineView.

    Set-PSReadLineOption -PredictionViewStyle ListView

    # user_profile.ps1
    Import-Module PSReadLine
    Set-PSReadLineOption -EditMode Emacs
    Set-PSReadLineOption -PredictionSource HisoryAndPlugin
    Set-PSReadLineOption -PredictionViewStyle ListView
  14. 安装 fzf PSFzf

    scoop install fzf
    Install-Module -Name PSFzf -Scope CurrentUser -Force

about_Environment_Provider

The environment Environment provider lets you get, add, change, clear, delete environment variables and values in Powershell.

Windows and Powershell use Environment variables to store persistent information that affect system and process execution.

Unlike PowerShell variables, environment variables are not subject to scope constraints.

The Environment provider supports the following cmdlets.

about_Environment_Variables

Environment variables store information about the operating system environment.

The environment variables store data that is used by the operating system and other programs.

about_CommonParameters

描述可与任何 cmdlet 一起使用的参数。****

常见问题

Windows terminal 下 git bash 乱码问题

在相应的 git-for-windows 的安装路径文件 **\Git\etc\bash.bashrc 末尾添加

# 让ls和dir命令显示中文和颜色 
alias ls='ls --show-control-chars --color' 
alias dir='dir -N --color' 
# 设置为中文环境,使提示成为中文 
export LANG="zh_CN" 
# 输出为中文编码 
export OUTPUT_CHARSET="utf-8"

# 可以输入中文 
set meta-flag on 
set output-meta on 
set convert-meta off

命令

技巧

  1. 按tab 键可自动补全命令
  2. Reset your video devicer: Ctrl+shift+win+B

资源

https://www.powershellgallery.com/packages/PSFzf/2.0.0

https://www.powershellgallery.com/ test