Windows 系统之 PowerShell

1 PowerShell PSReadLine

1.1 初始化

  • 1)查看 powershell 配置文件位置:echo $PROFILE

  • 2)第一次使用时可能没有该配置文件,所以我们要创建该文件:

    • 输入 notepad $PROFILE,保存,我这里保存到 D:\System\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

1.2 PSReadLine 配置

  • 1)DOS 下安装,命令:Install-Module -Name PSReadLine -AllowClobber -Force

  • 2)简单配置(通过 notepad $PROFILE 打开配置文件,复制下面脚本):

    # 切换路径
    function ll 	{ ls }
    function cdesktop { cd ${Home}\Desktop }
    
    # 选择模式
    Set-PSReadLineOption -EditMode VI
    # 使能智能预测
    Set-PSReadLineOption -PredictionSource History
    # 关闭智能预测
    # Set-PSReadLineOption -PredictionSource None
    
    # 设置快捷键:上下方向键-搜索历史命令
    Set-PSReadLineOption -HistorySearchCursorMovesToEnd
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
    
    # 命令行编辑快捷键
    Set-PSReadLineKeyHandler -Key Ctrl+h -Function BackwardDeleteChar
    Set-PSReadLineKeyHandler -Key Ctrl+d -Function DeleteChar
    Set-PSReadLineKeyHandler -Key Ctrl+w -Function BackwardDeleteWord
    Set-PSReadLineKeyHandler -Key Alt+d -Function DeleteEndOfWord
    Set-PSReadLineKeyHandler -Key Ctrl+u -Function BackwardDeleteInput
    Set-PSReadLineKeyHandler -Key Ctrl+k -Function DeleteToEnd
    
    # 光标移动快捷键
    Set-PSReadLineKeyHandler -Key Ctrl+a -Function BeginningOfLine
    Set-PSReadLineKeyHandler -Key Ctrl+e -Function MoveToEndOfLine
    Set-PSReadLineKeyHandler -Key Ctrl+b -Function ViForwardChar
    Set-PSReadLineKeyHandler -Key Ctrl+f -Function ViBackwardChar
    Set-PSReadLineKeyHandler -Key Alt+b -Function BackwardWord
    Set-PSReadLineKeyHandler -Key Alt+f -Function NextWord
    
  • 3)配置完成测试:打开 PowerShell 后,输入 “ff” 后,可以看到会补全历史命令,这里可以通过上下箭头来选择历史命令,或通过向右箭头补全整行命令:

  • 3)搜索可用配置:Get-PSReadLineKeyHandler -Bound | findstr “Move”

  • 官网:https://learn.microsoft.com/en-us/powershell/module/psreadline/

  • 参考:https://blog.csdn.net/qq_34548075/article/details/120108864

1.3 别名

  • 1)按照如下格式编写别名(将以下内容添加到 ps1 文件中即可):

    # 语法:
    function 别名 { 需要替代的命令 }  
    
    # 示例:
    function cdesktop   { cd ${Home}\Desktop }  # 切换路径到桌面
    
  • 2)使用别名生效:Set-ExecutionPolicy RemoteSigned

  • 3)查看命令的别名:Get-Alias。(比较多时配合 findstr 命令更好看):

  • 4)参考:

    • https://blog.csdn.net/lei_qi/article/details/106592404

2 PowerShell 中的历史命令

  • 1)查询历史命令:

    get-history             # 查看历史命令
    invoke-history -id [n]  # 执行某个历史命令
    
    # 使用别名执行
    h               # 查看历史命令
    r -id [n]       # 执行某个历史命令
    
  • 2)Crtl + R:搜索以前输入的命令,回车执行

  • 参考:

    • https://www.cnblogs.com/iBinary/p/12425743.html
    • https://docs.microsoft.com/zh-cn/previous-versions/technet-magazine/hh241048(v=msdn.10)

3 使用 curl 命令

  • 1)POST 请求
# 把参数转换为 UTF-8 编译
$body=[System.Text.Encoding]::UTF8.GetBytes("txStatus=10&txDesc=测试")

# 发送请求
curl -method POST -body $body  http://localhost:9080/tx/save | Select -ExpandProperty Content
  • 参考:
    • https://www.jianshu.com/p/1bcf857eb876
    • https://www.cnblogs.com/bro-ma/p/13067718.html

4 文件拖动上传

通过 trzsz 实现。Windows 下推荐通过 scoop 安装 trzsz,所以这里先安装 scoop

  • 1)安装 scoop:

    # 下载 get.scoop.sh 脚本(任选一)
    wget https://get.scoop.sh -o get.scoop.sh
    invoke-webrequest https://get.scoop.sh -o get.scoop.sh
    # 指定安装目录
    $env:SCOOP='D:\Software\Scoop'
    [Environment]::SetEnvironmentVariable('SCOOP', $env:SCOOP, 'User')
    # 安装
    iwr -useb get.scoop.sh | iex
    
    • 注意:不要使用管理员终端窗口
  • 2)安装 trzsz:

    scoop bucket add extras
    scoop install trzsz
    
  • 3)使用 trzsz 登录宿主机:

    trzsz -d ssh root@192.168.0.103
    
  • 4)宿主机安装 trzsz:

    # 修改源
    echo '[trzsz]
    name=Trzsz Repo
    baseurl=https://yum.fury.io/trzsz/
    enabled=1
    gpgcheck=0' | sudo tee /etc/yum.repos.d/trzsz.repo
    
    # 安装
    sudo yum install trzsz
    
  • 5)拖动即上传。

  • 参考:

    • https://zhuanlan.zhihu.com/p/617266698
    • https://zhuanlan.zhihu.com/p/463284082
    • https://www.cnblogs.com/sunsky303/p/16519755.html

5 升级 PowerShell

  • 1)查看 PowerShell 当前版本:

    $psversiontable
    
    Name                           Value
    ----                           -----
    PSVersion                      7.4.2
    ......
    
  • 2)直接输入 winget 可查看其使用说明:

    winget
    
    ......
    使用情况: winget  [<命令>] [<选项>]
    ......
    
  • 3)升级 PowerShell

    winget search Microsoft.PowerShell
    
    名称               ID                           版本    源
    ---------------------------------------------------------------
    PowerShell         Microsoft.PowerShell         7.4.2.0 winget
    PowerShell Preview Microsoft.PowerShell.Preview 7.5.0.2 winget
    
    winget upgrade Microsoft.PowerShell
    
    winget uninstall Microsoft.PowerShell
    
    winget install Microsoft.PowerShell
    
  • 参考:https://zhuanlan.zhihu.com/p/401439255

6 方法查询

使用 System.Math 类中的方法进行数学运算时,可以使用以下语句获取 System.Math 中的静态方法:

[Math] | Get-Member -Static

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值