pwsh 2023
1. powershell 用安装包安装,它会配置些环境变量
2. include 多文件
$ownSscriptRoot = 'abs:\path\to\PowerShell\'
$workPS = $ownSscriptRoot + "work.ps1"
3. 配置文件 location
windows 配置全局的
C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
mac
/Users/helloWorld/.config/powershell/Microsoft.PowerShell_profile.ps1
4. 例
git
gs
gg
ggs
ggc
gl
gls
glc
glf
gld
gla
glaf
glad
visual studio
visual studio command window
vsc
env
print all env variable
catEnv
print all path variable
catPath
network
get ipv4 addr
ipv4a
配置文件内容
# include ######################################################################
#$ownSscriptRoot = 'abs:\path\to\PowerShell\'
#$workPS = $ownSscriptRoot + "work.ps1"
#Import-Module $workPS
################################################################################
# complete #####################################################################
# Shows navigable menu of all options when hitting Tab
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
# Autocompletion for arrow keys
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
################################################################################
# git ##########################################################################
function Get-GitSStatus { & git status }
New-Alias -Name gs -Value Get-GitSStatus -Force -Option AllScope
function Get-GitGraph { & git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aI%C(reset) %C(dim white)(%ar) %C(bold white)- %an - %C(reset)%C(dim white)%aE%C(reset) %C(reset)%C(dim yellow)%d%C(reset)%n'' %C(dim white)%H%C(reset)%n'' %C(white)%s%C(reset)%C(reset)%n''' --all }
New-Alias -Name gg -Value Get-GitGraph -Force -Option AllScope
function Get-GitGraphC { & git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aI%C(reset) %C(dim white)(%ar) %C(bold white)- %an - %C(reset)%C(dim white)%aE%C(reset) %C(reset)%C(dim yellow)%d%C(reset)%n'' %C(dim white)%H%C(reset)%n'' %C(white)%s%C(reset)%C(reset)%n''' --all -c}
New-Alias -Name ggc -Value Get-GitGraphC -Force -Option AllScope
function Get-GitGraphS { & git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aI%C(reset) %C(dim white)(%ar) %C(bold white)- %an - %C(reset)%C(dim white)%aE%C(reset) %C(reset)%C(dim yellow)%d%C(reset)%n'' %C(dim white)%H%C(reset)%n'' %C(white)%s%C(reset)%C(reset)%n''' --all --stat }
New-Alias -Name ggs -Value Get-GitGraphS -Force -Option AllScope
function Get-GitGraphL { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --first-parent $(git branch --show-current)}
New-Alias -Name gl -Value Get-GitGraphL -Force -Option AllScope
function Get-GitGraphLS { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --stat --first-parent $(git branch --show-current)}
New-Alias -Name gls -Value Get-GitGraphLS -Force -Option AllScope
function Get-GitGraphLC { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' -c --first-parent $(git branch --show-current)}
New-Alias -Name glc -Value Get-GitGraphLC -Force -Option AllScope
function Get-GitGraphLF { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --name-only --first-parent $(git branch --show-current)}
New-Alias -Name glf -Value Get-GitGraphLF -Force -Option AllScope
function Get-GitGraphLD { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --dirstat=changes --first-parent $(git branch --show-current)}
New-Alias -Name gld -Value Get-GitGraphLD -Force -Option AllScope
function Get-GitGraphLA { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --all}
New-Alias -Name gla -Value Get-GitGraphLA -Force -Option AllScope
function Get-GitGraphLAF { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --name-only --all}
New-Alias -Name glaf -Value Get-GitGraphLAF -Force -Option AllScope
function Get-GitGraphLAD { & git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --dirstat=changes --all}
New-Alias -Name glad -Value Get-GitGraphLAD -Force -Option AllScope
#alias gs='git status'
#
#function glDef {
# git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --first-parent $(git branch --show-current)
#}
#
#function glsDef {
# git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --stat --first-parent $(git branch --show-current)
#}
#
#function glcDef {
# git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' -c --first-parent $(git branch --show-current)
#}
#
#function glfDef {
# git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --name-only --first-parent $(git branch --show-current)
#}
#
#function gldDef {
# git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --dirstat=changes --first-parent $(git branch --show-current)
#}
#
#
#alias gg="git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aI%C(reset) %C(dim white)(%ar) %C(bold white)- %an - %C(reset)%C(dim white)%aE%C(reset) %C(reset)%C(dim yellow)%d%C(reset)%n'' %C(dim white)%H%C(reset)%n'' %C(white)%s%C(reset)%C(reset)%n''' --all"
#alias ggc="git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aI%C(reset) %C(dim white)(%ar) %C(bold white)- %an - %C(reset)%C(dim white)%aE%C(reset) %C(reset)%C(dim yellow)%d%C(reset)%n'' %C(dim white)%H%C(reset)%n'' %C(white)%s%C(reset)%C(reset)%n''' --all -c"
#alias ggs="git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aI%C(reset) %C(dim white)(%ar) %C(bold white)- %an - %C(reset)%C(dim white)%aE%C(reset) %C(reset)%C(dim yellow)%d%C(reset)%n'' %C(dim white)%H%C(reset)%n'' %C(white)%s%C(reset)%C(reset)%n''' --all --stat"
#
#alias gl=glDef
#alias gls=glsDef
#alias glc=glcDef
#alias glf=glfDef
#alias gld=gldDef
#
#alias gla="git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --all"
#alias glaf="git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --name-only --all"
#alias glad="git log --pretty=format:'%Cgreen%h %C(dim white)%H %C(dim white)%aI %ar %C(reset)%C(bold blue)%an %C(reset)%C(dim white)%aE %C(bold red)%d %C(reset)%C(white)%s' --dirstat=changes --all"
################################################################################
# m$vc ##########################################################################
if ($IsLinux) {
#Write-Host "Linux"
}
elseif ($IsMacOS) {
#Write-Host "macOS"
}
elseif ($IsWindows) {
function StartVS64Console { & cmd /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" }
New-Alias -Name vsc -Value StartVS64Console -Force -Option AllScope
}
################################################################################
# netWork ##########################################################################
if ($IsLinux) {
#Write-Host "Linux"
}
elseif ($IsMacOS) {
#Write-Host "macOS"
}
elseif ($IsWindows) {
function getIPV4Addr {(Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.IPAddress}
New-Alias -Name ipv4a -Value getIPV4Addr
}
################################################################################
# windows ######################################################################
if ($IsLinux) {
#Write-Host "Linux"
}
elseif ($IsMacOS) {
#Write-Host "macOS"
}
elseif ($IsWindows) {
function Start-HibernationS { rundll32.exe PowrProf.dll, SetSuspendState 0,1,0 }
New-Alias -Name ph -Value Start-HibernationS -Force -Option AllScope
function ImplOfclrOS { Clear-RecycleBin -Force }
New-Alias -Name clrOs -Value ImplOfclrOS -Force -Option AllScope
}
################################################################################
# windows path env #############################################################
<#
.SYNOPSIS
Modify the PATH environment variable.
.DESCRIPTION
Set-PathVariable allows you to add or remove paths to your PATH variable at the specified scope with logic that prevents duplicates.
.PARAMETER AddPath
A path that you wish to add. Can be specified with or without a trailing slash.
.PARAMETER RemovePath
A path that you wish to remove. Can be specified with or without a trailing slash.
.PARAMETER Scope
The scope of the variable to edit. Either Process, User, or Machine.
If you specify Machine, you must be running as administrator.
.EXAMPLE
Set-PathVariable -AddPath C:\tmp\bin -RemovePath C:\path\java
This will add the C:\tmp\bin path and remove the C:\path\java path. The Scope will be set to Process, which is the default.
Set-PathVariable -AddPath "C:\tmp abc\bin" -Scope Machine
Set-PathVariable -RemovePath C:\tmp\bin -Scope Machine
Set-PathVariable -RemovePath "C:\tmp abc\bin" -Scope Machine
.INPUTS
.OUTPUTS
.NOTES
Author: ThePoShWolf
.LINK
#>
Function Set-PathVariable {
param (
[string]$AddPath,
[string]$RemovePath,
[ValidateSet('Process', 'User', 'Machine')]
[string]$Scope = 'Process'
)
if ($IsWindows) {
$regexPaths = @()
if ($PSBoundParameters.Keys -contains 'AddPath') {
$regexPaths += [regex]::Escape($AddPath)
}
if ($PSBoundParameters.Keys -contains 'RemovePath') {
$regexPaths += [regex]::Escape($RemovePath)
}
$arrPath = [System.Environment]::GetEnvironmentVariable('PATH', $Scope) -split ';'
foreach ($path in $regexPaths) {
$arrPath = $arrPath | Where-Object { $_ -notMatch "^$path\\?" }
}
$value = ($arrPath + $addPath) -join ';'
[System.Environment]::SetEnvironmentVariable('PATH', $value, $Scope)
}
else {
echo "not avaliable on current system."
}
}
Function Get-ENV {
# gci env:* | select Name,Value
# Get-ChildItem Env:* | Select-Object -Property Name,Value
# [System.EnvironmentVariableTarget]::Machine
# [System.EnvironmentVariableTarget]::User
# [System.EnvironmentVariableTarget]::Process
# Get-ChildItem env: | Format-List
echo ""
echo "Begin ENV: ========================================================="
echo ""
echo "Process ENV Begin : --------------------------------------------------"
[System.Environment]::GetEnvironmentVariables('Process') | Format-List
echo "Process ENV End : --------------------------------------------------"
if ($IsWindows) {
echo ""
echo "User ENV Begin : --------------------------------------------------"
[System.Environment]::GetEnvironmentVariables('User') | Format-List
echo "User ENV End : --------------------------------------------------"
echo ""
echo "Machine ENV Begin : --------------------------------------------------"
[System.Environment]::GetEnvironmentVariables('Machine') | Format-List
echo "Machine ENV End : --------------------------------------------------"
}
echo ""
echo "End ENV: ========================================================="
echo ""
}
New-Alias -Name catEnv -Value Get-ENV -Force -Option AllScope
Function Get-ENV-PATH {
# gci env:* | select Name,Value
# Get-ChildItem Env:* | Select-Object -Property Name,Value
# [System.EnvironmentVariableTarget]::Machine
# [System.EnvironmentVariableTarget]::User
# [System.EnvironmentVariableTarget]::Process
# Get-ChildItem env: | Format-List
echo ""
echo "Begin ENV Path: ===================================================="
echo ""
echo "Process ENV Path Begin : ---------------------------------------------"
[System.Environment]::GetEnvironmentVariable('PATH','Process') | Format-List
echo "Process ENV Path End : ---------------------------------------------"
if ($IsWindows) {
echo ""
echo "User ENV Path Begin : ---------------------------------------------"
[System.Environment]::GetEnvironmentVariable('PATH', 'User') | Format-List
echo "User ENV Path End : ---------------------------------------------"
echo ""
echo "Machine ENV Path Begin : ---------------------------------------------"
[System.Environment]::GetEnvironmentVariable('PATH', 'Machine') | Format-List
echo "Machine ENV Path End : ---------------------------------------------"
}
echo ""
echo "End ENV Path: ===================================================="
echo ""
}
New-Alias -Name catPath -Value Get-ENV-PATH -Force -Option AllScope
################################################################################
Function DirCopyImpl {
#$strFolderName="c:\d\f"
#If (Test-Path $strFolderName){
# Remove-Item $strFolderName -Recurse -Force
#}
$tpath = "c:\d\f"
If(!(test-path -PathType container $tpath))
{
New-Item -ItemType Directory -Path $tpath
}
Copy-Item -Path "c:\d\e\*" -Destination $tpath -Recurse -Force
}
New-Alias -Name justCopy -Value DirCopyImpl -Force -Option AllScope
Function x_MultiCmd {
cd c:\d\e
tree
make
echo "just do it"
cd -
}
New-Alias -Name xxWhat -Value x_MultiCmd -Force -Option AllScope
old git
function Get-GitSStatus { & git status }
New-Alias -Name gs -Value Get-GitSStatus -Force -Option AllScope
function Get-GitGraph { & git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aD%C(reset) %C(dim white)(%ar) %C(dim white)- %an%C(reset) %C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset)' --all }
New-Alias -Name gg -Value Get-GitGraph -Force -Option AllScope
function Get-GitGraphC { & git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aD%C(reset) %C(dim white)(%ar) %C(dim white)- %an%C(reset) %C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset)' --all -c }
New-Alias -Name ggc -Value Get-GitGraphC -Force -Option AllScope
function Get-GitGraphS { & git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(dim white)%aD%C(reset) %C(dim white)(%ar) %C(dim white)- %an%C(reset) %C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset)' --all --stat }
New-Alias -Name ggs -Value Get-GitGraphS -Force -Option AllScope
function Get-GitGraphL { & git log --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --date=short}
New-Alias -Name ggl -Value Get-GitGraphL -Force -Option AllScope