微软官方Powershell文档:https://learn.microsoft.com/zh-cn/powershell/
命令详细说明,在PDF的最后面:

- 一、Powershell及命令简介
 - 
- [1.1 命令格式](#1.1 命令格式)
 - [1.2 命令的别名](#1.2 命令的别名)
 
 - 二、cmdlet别名
 - 三、cmdlet分类介绍
 - 
- [3.1 基础命令](#3.1 基础命令)
 - 
- 
- [1. Get-Command](#1. Get-Command)
 - [2. Get-Help](#2. Get-Help)
 - [3. Set-Variable](#3. Set-Variable)
 - [4. Get-Variable](#4. Get-Variable)
 - [5. Remove-Variable](#5. Remove-Variable)
 - [6. Clear-Variable](#6. Clear-Variable)
 
 
 - 
 - [3.2 管理命令](#3.2 管理命令)
 - 
- 
- [1. Get-Process](#1. Get-Process)
 - [2. Stop-Process](#2. Stop-Process)
 - [3. Restart-Service](#3. Restart-Service)
 - [4. Get-Service](#4. Get-Service)
 - [5. Start-Service 和 Stop-Service](#5. Start-Service 和 Stop-Service)
 - [6. Get-EventLog](#6. Get-EventLog)
 
 
 - 
 - [3.3 实用工具命令](#3.3 实用工具命令)
 - 
- 
- [1. Sort-Object](#1. Sort-Object)
 - [2. Measure-Object](#2. Measure-Object)
 - [3. ConvertTo-Json 和 ConvertFrom-Json](#3. ConvertTo-Json 和 ConvertFrom-Json)
 - [4. Select-Object](#4. Select-Object)
 - [5. Where-Object](#5. Where-Object)
 - [6. Out-File](#6. Out-File)
 - [7. Compare-Object](#7. Compare-Object)
 
 
 - 
 - [3.4 提供程序命令](#3.4 提供程序命令)
 - 
- 
- [1. Get-PSProvider](#1. Get-PSProvider)
 - [2. Get-PSDrive](#2. Get-PSDrive)
 - [3. New-PSDrive](#3. New-PSDrive)
 - [4. Remove-PSDrive](#4. Remove-PSDrive)
 - [5. Set-Location 和 Get-Location](#5. Set-Location 和 Get-Location)
 - [6. Get-Item 和 Set-Item](#6. Get-Item 和 Set-Item)
 
 
 - 
 - [3.5 模块和包命令](#3.5 模块和包命令)
 - 
- 
- [1. Import-Module](#1. Import-Module)
 - [2. Remove-Module](#2. Remove-Module)
 - [3. Get-Module](#3. Get-Module)
 - [4. Install-Module](#4. Install-Module)
 - [5. Uninstall-Module](#5. Uninstall-Module)
 - [6. Find-Module](#6. Find-Module)
 - [7. Save-Module](#7. Save-Module)
 
 
 - 
 - [3.6 调试命令](#3.6 调试命令)
 - 
- 
- [1. Set-PSBreakpoint (sbp)](#1. Set-PSBreakpoint (sbp))
 - [2. Get-PSBreakpoint](#2. Get-PSBreakpoint)
 - [3. Remove-PSBreakpoint (rbp)](#3. Remove-PSBreakpoint (rbp))
 - [4. Get-PSCallStack](#4. Get-PSCallStack)
 - [5. Trace-Command](#5. Trace-Command)
 - [6. Enter-PSHostProcess 和 Exit-PSHostProcess](#6. Enter-PSHostProcess 和 Exit-PSHostProcess)
 
 
 - 
 - [3.7 远程命令](#3.7 远程命令)
 - 
- 
- [1. Enter-PSSession](#1. Enter-PSSession)
 - [2. Exit-PSSession](#2. Exit-PSSession)
 - [3. Invoke-Command](#3. Invoke-Command)
 - [4. New-PSSession](#4. New-PSSession)
 - [5. Remove-PSSession](#5. Remove-PSSession)
 - [6. Export-PSSession](#6. Export-PSSession)
 - [7. Enable-PSRemoting 和 Disable-PSRemoting](#7. Enable-PSRemoting 和 Disable-PSRemoting)
 
 
 - 
 
 - 四、特殊操作符
 - 
- 
- 
- [1. 算术操作符](#1. 算术操作符)
 - [2. 赋值操作符](#2. 赋值操作符)
 - [3. 比较操作符](#3. 比较操作符)
 - [4. 逻辑操作符](#4. 逻辑操作符)
 - [5. 位操作符](#5. 位操作符)
 - [6. 重定向操作符](#6. 重定向操作符)
 - [7. 特殊操作符](#7. 特殊操作符)
 - [8. 类型操作符](#8. 类型操作符)
 
 
 - 
 
 - 
 - 五、运行Windows命令行工具
 - 
- [5.1 常用命令行工具](#5.1 常用命令行工具)
 - 
- 
- [1. ipconfig](#1. ipconfig)
 - [2. netstat](#2. netstat)
 - [3. ping](#3. ping)
 - [4. tracert](#4. tracert)
 - [5. nslookup](#5. nslookup)
 - [6. sfc](#6. sfc)
 - [7. chkdsk](#7. chkdsk)
 - [8. systeminfo](#8. systeminfo)
 - [9. gpupdate](#9. gpupdate)
 - [10. tasklist](#10. tasklist)
 
 
 - 
 - [5.2 对应的cmdlet](#5.2 对应的cmdlet)
 
 - 六、运行用户程序
 - 
- [6.1 vim(nvim)](#6.1 vim(nvim))
 - [6.2 gcc / gdb](#6.2 gcc / gdb)
 
 
一、Powershell及命令简介
PowerShell 是一种用于自动化的命令行 shell 和脚本语言。
Wikipedia 包含以下关于 shell 的描述:
shell 通过提示用户输入、解释其输入,然后处理基础操作系统的输出来管理用户系统交互(非常类似于读取--求值--打印循环 [即 REPL])。
与其他 shell(如 bash 或 cmd.exe)类似,PowerShell 使你能够在系统上运行任何可用的命令,而不仅仅是 PowerShell 命令。
PowerShell 命令称为 cmdlet(读作 command-let)。 cmdlet 是 PowerShell 命令,而不是独立的可执行文件。 如果不先运行 PowerShell,则 PowerShell 命令无法在其他 shell 中运行。
PowerShell 是新式命令 shell,其中包括其他常用 shell 的最佳功能。 与大多数仅接受并返回文本的 shell 不同,PowerShell 接受并返回 .NET 对象。 shell 有多项功能可用于优化交互式用户体验。
1.1 命令格式
PowerShell 的命令遵循一个特定的命名约定,即"动词-名词"格式。这种结构使得命令易于阅读和理解。以下是 PowerShell cmdlet 的组成部分:
- 
动词 (Verb) : 描述 cmdlet 执行的操作。例如,
Get、Set、New、Remove等。 - 
名词 (Noun) : 描述 cmdlet 操作的对象或资源。例如,
Item、Service、Process等。 - 
参数 (Parameters) : 提供给 cmdlet 的额外信息,以影响其操作。参数通常以
-开头,例如-Name、-Path等。 - 
参数值 (Parameter Values) : 跟在参数后面的具体值。例如,在
Get-Service -Name "wuauserv"中,"wuauserv"是-Name参数的值。 
以下是一些 PowerShell cmdlet 的示例:
Get-Service: 获取系统上运行的所有服务的列表。Get-Process: 获取当前运行的所有进程的列表。New-Item: 创建一个新的项目,如文件或文件夹。Remove-Item: 删除一个项目,如文件或文件夹。
此外,PowerShell 还支持使用管道 (|) 将一个 cmdlet 的输出传递给另一个 cmdlet 作为输入。例如,Get-Process | Where-Object {$_.CPU -gt 100} 会获取所有 CPU 使用率超过 100 的进程。
1.2 命令的别名
PowerShell 中的许多命令(cmdlet)都有别名。别名是一个或多个 cmdlet 的短名称,它们提供了一种快速键入常用命令的方法,而不必完整地键入整个 cmdlet 名称。
例如:
Get-Command的别名是gcm。Get-Help的别名是help或man。Get-Process的别名是ps。Get-Item的别名是gi。
你可以使用 Get-Alias cmdlet 来查看所有可用的别名。例如,要查看 Get-Command 的所有别名,你可以运行:
            
            
              bash
              
              
            
          
          Get-Alias -Definition Get-Command
        如果你想查看所有的别名及其对应的 cmdlet,你可以简单地运行:
            
            
              bash
              
              
            
          
          Get-Alias
        此外,你还可以使用 Set-Alias cmdlet 来创建自己的别名。例如,如果你想为 Get-Service 创建一个别名 gs, 你可以运行:
            
            
              bash
              
              
            
          
          Set-Alias -Name gs -Value Get-Service
        这样,每次你键入 gs 时,它都会运行 Get-Service cmdlet。
此外,可以使用Tab补全命令。
二、cmdlet别名
这些别名是7.36版本中,使用Get-Alias命令获取的。
有少许别名可能与Linux bash命令相同,但参数一般不同。
| 别名 (Alias) | cmdlet 名称 (Cmdlet Name) | 作用 (Description) | 
|---|---|---|
| ? | Where-Object | 过滤对象 | 
| % | ForEach-Object | 对每个对象执行操作 | 
| ac | Add-Content | 添加内容到文件 | 
| cat | Get-Content | 获取文件内容 | 
| cd, chdir | Set-Location | 更改当前位置 | 
| clc | Clear-Content | 清除内容 | 
| clear, cls | Clear-Host | 清除控制台输出 | 
| clhy | Clear-History | 清除命令历史 | 
| cli | Clear-Item | 清除项目 | 
| clp | Clear-ItemProperty | 清除项目属性 | 
| clv | Clear-Variable | 清除变量 | 
| cnsn | Connect-PSSession | 连接到 PSSession | 
| compare | Compare-Object | 比较对象 | 
| copy, cp, cpi | Copy-Item | 复制项目 | 
| cpp | Copy-ItemProperty | 复制项目属性 | 
| cvpa | Convert-Path | 转换路径 | 
| dbp | Disable-PSBreakpoint | 禁用断点 | 
| del, erase | Remove-Item | 删除项目 | 
| diff | Compare-Object | 比较对象 | 
| dir | Get-ChildItem | 获取子项目列表 | 
| dnsn | Disconnect-PSSession | 断开 PSSession | 
| ebp | Enable-PSBreakpoint | 启用断点 | 
| echo | Write-Output | 输出内容 | 
| epal | Export-Alias | 导出别名 | 
| epcsv | Export-Csv | 导出为 CSV | 
| erase | Remove-Item | 删除指定项目 | 
| etsn | Enter-PSSession | 进入 PSSession | 
| exsn | Exit-PSSession | 退出 PSSession | 
| fc | Format-Custom | 自定义格式化 | 
| fhx | Format-Hex | 格式化为十六进制 | 
| fl | Format-List | 列表格式化 | 
| foreach | ForEach-Object | 对每个对象执行操作 | 
| ft | Format-Table | 表格格式化 | 
| fw | Format-Wide | 宽格式化 | 
| gal | Get-Alias | 获取别名 | 
| gbp | Get-PSBreakpoint | 获取断点 | 
| gc | Get-Content | 获取内容 | 
| gcb | Get-Clipboard | 获取剪贴板内容 | 
| gci | Get-ChildItem | 获取子项目 | 
| gcm | Get-Command | 获取命令 | 
| gcs | Get-PSCallStack | 获取调用堆栈 | 
| gdr | Get-PSDrive | 获取驱动器 | 
| gerr | Get-Error | 获取错误 | 
| ghy | Get-History | 获取历史 | 
| gi | Get-Item | 获取项目 | 
| gin | Get-ComputerInfo | 获取计算机信息 | 
| gjb | Get-Job | 获取作业 | 
| gl | Get-Location | 获取当前位置 | 
| gm | Get-Member | 获取成员 | 
| gmo | Get-Module | 获取模块 | 
| gp | Get-ItemProperty | 获取项目属性 | 
| gps | Get-Process | 获取进程 | 
| gpv | Get-ItemPropertyValue | 获取项目属性值 | 
| group | Group-Object | 对象分组 | 
| gsn | Get-PSSession | 获取 PSSession | 
| gsv | Get-Service | 获取服务 | 
| gtz | Get-TimeZone | 获取时区 | 
| gu | Get-Unique | 获取唯一值 | 
| gv | Get-Variable | 获取变量 | 
| h, history | Get-History | 获取历史 | 
| icm | Invoke-Command | 调用命令 | 
| iex | Invoke-Expression | 调用表达式 | 
| ihy | Invoke-History | 调用历史命令 | 
| ii | Invoke-Item | 调用项目 | 
| ipal | Import-Alias | 导入别名 | 
| ipcsv | Import-Csv | 导入 CSV | 
| ipmo | Import-Module | 导入模块 | 
| irm | Invoke-RestMethod | 调用 REST 方法 | 
| iwr | Invoke-WebRequest | 调用 Web 请求 | 
| kill | Stop-Process | 停止进程 | 
| ls | Get-ChildItem | 获取子项目 | 
| man | help | 获取帮助 | 
| md | mkdir | 创建目录 | 
| measure | Measure-Object | 测量对象 | 
| mi | Move-Item | 移动项目 | 
| mount | New-PSDrive | 创建新驱动器 | 
| move | Move-Item | 移动项目 | 
| mp | Move-ItemProperty | 移动项目属性 | 
| mv | Move-Item | 移动项目 | 
| nal | New-Alias | 创建新别名 | 
| ndr | New-PSDrive | 创建新驱动器 | 
| ni | New-Item | 创建新项目 | 
| nmo | New-Module | 创建新模块 | 
| nsn | New-PSSession | 创建新 PSSession | 
| nv | New-Variable | 创建新变量 | 
| ogv | Out-GridView | 输出到网格视图 | 
| oh | Out-Host | 输出到主机 | 
| popd | Pop-Location | 弹出位置 | 
| ps | Get-Process | 获取进程 | 
| pushd | Push-Location | 推送位置 | 
| pwd | Get-Location | 获取当前位置 | 
| r | Invoke-History | 调用历史命令 | 
| rbp | Remove-PSBreakpoint | 删除断点 | 
| rcjb | Receive-Job | 接收作业 | 
| rcsn | Receive-PSSession | 接收 PSSession | 
| rd | Remove-Item | 删除项目 | 
| rdr | Remove-PSDrive | 删除驱动器 | 
| ren | Rename-Item | 重命名项目 | 
| ri | Remove-Item | 删除项目 | 
| rjb | Remove-Job | 删除作业 | 
| rm, rmdir | Remove-Item | 删除项目 | 
| rmo | Remove-Module | 删除模块 | 
| rni | Rename-Item | 重命名项目 | 
| rnp | Rename-ItemProperty | 重命名项目属性 | 
| rp | Remove-ItemProperty | 删除项目属性 | 
| rsn | Remove-PSSession | 删除 PSSession | 
| rv | Remove-Variable | 删除变量 | 
| rvpa | Resolve-Path | 解析路径 | 
| sajb | Start-Job | 开始作业 | 
| sal | Set-Alias | 设置别名 | 
| saps | Start-Process | 开始进程 | 
| sasv | Start-Service | 开始服务 | 
| sbp | Set-PSBreakpoint | 设置断点 | 
| scb | Set-Clipboard | 设置剪贴板内容 | 
| select | Select-Object | 选择对象 | 
| set | Set-Variable | 设置变量 | 
| shcm | Show-Command | 显示命令 | 
| si | Set-Item | 设置项目 | 
| sl | Set-Location | 设置位置 | 
| sleep | Start-Sleep | 开始休眠 | 
| sls | Select-String | 选择字符串 | 
| sort | Sort-Object | 排序对象 | 
| sp | Set-ItemProperty | 设置项目属性 | 
| spjb | Stop-Job | 停止作业 | 
| spps | Stop-Process | 停止进程 | 
| spsv | Stop-Service | 停止服务 | 
| start | Start-Process | 开始进程 | 
| stz | Set-TimeZone | 设置时区 | 
| sv | Set-Variable | 设置变量 | 
| tee | Tee-Object | 分叉对象 | 
| type | Get-Content | 获取内容 | 
| where | Where-Object | 过滤对象 | 
| wjb | Wait-Job | 等待作业 | 
| write | Write-Output | 输出内容 | 
三、cmdlet分类介绍
如果你想了解一个命令的说明和参数,可以使用Get-Help 命令名称 的格式获取,Get-Help的别名是man,例如你要获取Get-Help这个命令的帮助文档,可以使用:
            
            
              bash
              
              
            
          
          man man
        帮助文档是英文的,如果你有困难,可以去文章开头的微软中国官网搜索该命令,有中文的详细解释。

3.1 基础命令
1. Get-Command
Get-Command 是一个非常有用的 cmdlet,它可以列出所有可用的 cmdlet、函数、脚本和别名。例如,要查找与进程相关的所有命令,可以使用:
            
            
              powershell
              
              
            
          
          Get-Command *process*
        2. Get-Help
Get-Help cmdlet 为用户提供了关于 cmdlet 和函数的详细信息。例如,要获取 Get-Process cmdlet 的帮助信息,可以使用:
            
            
              powershell
              
              
            
          
          Get-Help Get-Process
        此外,-Examples、-Detailed 和 -Full 参数可以提供更多的信息。
3. Set-Variable
Set-Variable cmdlet 允许用户创建或修改一个变量。例如,要创建一个名为 "myVar" 的变量并为其赋值 "Hello, PowerShell!",可以使用:
            
            
              powershell
              
              
            
          
          Set-Variable -Name myVar -Value "Hello, PowerShell!"
        4. Get-Variable
与 Set-Variable 相对应,Get-Variable cmdlet 可以获取一个或多个变量的值。例如,要获取 "myVar" 的值,可以使用:
            
            
              powershell
              
              
            
          
          Get-Variable myVar
        5. Remove-Variable
Remove-Variable cmdlet 允许用户删除一个或多个变量。例如,要删除 "myVar" 变量,可以使用:
            
            
              powershell
              
              
            
          
          Remove-Variable myVar
        6. Clear-Variable
Clear-Variable cmdlet 用于清除变量的值,但不删除变量本身。例如,要清除 "myVar" 变量的值,可以使用:
            
            
              powershell
              
              
            
          
          Clear-Variable myVar
        3.2 管理命令
1. Get-Process
Get-Process cmdlet 允许用户获取当前运行的进程信息。
            
            
              powershell
              
              
            
          
          Get-Process
        这将列出所有当前运行的进程。你还可以通过指定进程名称来获取特定的进程信息,例如:
            
            
              powershell
              
              
            
          
          Get-Process -Name notepad
        2. Stop-Process
Stop-Process cmdlet 用于终止一个或多个进程。例如,要终止名为 "notepad" 的进程,可以使用:
            
            
              powershell
              
              
            
          
          Stop-Process -Name notepad
        3. Restart-Service
Restart-Service cmdlet 允许用户重新启动一个或多个服务。例如,要重新启动 "wuauserv" 服务(Windows Update 服务),可以使用:
            
            
              powershell
              
              
            
          
          Restart-Service -Name wuauserv
        4. Get-Service
Get-Service cmdlet 提供了关于系统服务的信息。它可以列出所有服务或特定服务的状态。
            
            
              powershell
              
              
            
          
          Get-Service
        要获取特定服务的信息,例如 "wuauserv",可以使用:
            
            
              powershell
              
              
            
          
          Get-Service -Name wuauserv
        5. Start-Service 和 Stop-Service
这两个 cmdlet 分别用于启动和停止服务。例如,要停止 "wuauserv" 服务,可以使用:
            
            
              powershell
              
              
            
          
          Stop-Service -Name wuauserv
        要启动该服务,可以使用:
            
            
              powershell
              
              
            
          
          Start-Service -Name wuauserv
        6. Get-EventLog
Get-EventLog cmdlet 允许用户读取和查询事件日志。例如,要获取系统日志中的最近 10 条事件,可以使用:
            
            
              powershell
              
              
            
          
          Get-EventLog -LogName System -Newest 10
        3.3 实用工具命令
PowerShell 的实用工具命令提供了一系列功能,使用户能够进行文本和数据处理、文件操作、数据转换等任务。这些命令非常适合日常的数据处理和分析任务。以下是一些常用的实用工具命令及其功能介绍:
1. Sort-Object
Sort-Object cmdlet 用于对对象进行排序。例如,要按名称对进程进行排序,可以使用:
            
            
              powershell
              
              
            
          
          Get-Process | Sort-Object -Property ProcessName
        2. Measure-Object
Measure-Object cmdlet 用于计算对象的数量、平均值、总和等。例如,要计算文本文件中的行数,可以使用:
            
            
              powershell
              
              
            
          
          Get-Content -Path "C:\path\to\file.txt" | Measure-Object -Line
        3. ConvertTo-Json 和 ConvertFrom-Json
这两个 cmdlet 用于将对象转换为 JSON 格式,或从 JSON 格式转换为对象。例如,要将进程列表转换为 JSON 格式,可以使用:
            
            
              powershell
              
              
            
          
          Get-Process | ConvertTo-Json
        反之,要从 JSON 格式转换为 PowerShell 对象,可以使用:
            
            
              powershell
              
              
            
          
          $jsonData | ConvertFrom-Json
        4. Select-Object
Select-Object cmdlet 允许用户选择对象的特定属性。例如,要仅选择进程的名称和 ID,可以使用:
            
            
              powershell
              
              
            
          
          Get-Process | Select-Object -Property ProcessName, Id
        5. Where-Object
Where-Object cmdlet 用于过滤对象。例如,要选择使用内存超过 100 MB 的进程,可以使用:
            
            
              powershell
              
              
            
          
          Get-Process | Where-Object {$_.WorkingSet64 -gt 100MB}
        6. Out-File
Out-File cmdlet 用于将输出重定向到文件。例如,要将进程列表保存到文件中,可以使用:
            
            
              powershell
              
              
            
          
          Get-Process | Out-File -Path "C:\path\to\file.txt"
        7. Compare-Object
Compare-Object cmdlet 用于比较两个对象集合的差异。例如,要比较两个文本文件的内容,可以使用:
            
            
              powershell
              
              
            
          
          Compare-Object $(Get-Content -Path "C:\path\to\file1.txt") $(Get-Content -Path "C:\path\to\file2.txt")
        3.4 提供程序命令
PowerShell 提供程序是一种特殊的适配器,使得各种数据存储(如文件系统、注册表和证书存储)可以像文件系统一样被访问。这意味着你可以使用相同的命令(如 Get-Item、Set-Location 等)来操作这些不同的数据源。以下是与提供程序相关的一些常用命令及其功能介绍:
1. Get-PSProvider
Get-PSProvider cmdlet 显示所有已加载的 PowerShell 提供程序。执行此命令将列出如 FileSystem、Registry、Certificate 等提供程序。
            
            
              powershell
              
              
            
          
          Get-PSProvider
        2. Get-PSDrive
Get-PSDrive cmdlet 列出所有当前会话中的驱动器,包括由提供程序支持的驱动器。例如,除了常规的文件系统驱动器(如 C:、D:)之外,你还可能看到注册表驱动器(如 HKLM、HKCU)。
            
            
              powershell
              
              
            
          
          Get-PSDrive
        
3. New-PSDrive
New-PSDrive cmdlet 允许你创建一个新的 PowerShell 驱动器。例如,你可以创建一个指向特定注册表路径的新驱动器:
            
            
              powershell
              
              
            
          
          New-PSDrive -Name HKMyCustom -PSProvider Registry -Root HKEY_CURRENT_USER\Software\MyCustomPath
        4. Remove-PSDrive
Remove-PSDrive cmdlet 用于删除之前使用 New-PSDrive 创建的驱动器。
            
            
              powershell
              
              
            
          
          Remove-PSDrive -Name HKMyCustom
        5. Set-Location 和 Get-Location
这两个 cmdlet 用于导航到不同的 PowerShell 驱动器和路径。Set-Location(或其别名 cd)用于更改当前位置,而 Get-Location(或其别名 pwd)用于获取当前位置。
例如,要导航到之前创建的注册表驱动器,可以使用:
            
            
              powershell
              
              
            
          
          Set-Location -Path HKMyCustom:
        然后,使用 Get-Location 可以确认你当前的位置。
6. Get-Item 和 Set-Item
这两个 cmdlet 用于获取和设置提供程序位置的项。例如,要获取注册表键的值,可以使用:
            
            
              powershell
              
              
            
          
          Get-Item -Path HKMyCustom:\SomeKeyName
        要设置该键的值,可以使用:
            
            
              powershell
              
              
            
          
          Set-Item -Path HKMyCustom:\SomeKeyName -Value "NewValue"
        3.5 模块和包命令
在 PowerShell 中,模块是一个包含多个函数、cmdlet、提供程序和变量的包。这些模块可以被导入到 PowerShell 会话中,以增强其功能。包则是通过包管理器(如 PowerShellGet)分发的模块或脚本。以下是与模块和包相关的一些常用命令及其功能介绍:
1. Import-Module
Import-Module cmdlet 允许你导入一个模块,使其在当前会话中可用。例如,要导入名为 "MyModule" 的模块,可以使用:
            
            
              powershell
              
              
            
          
          Import-Module -Name MyModule
        2. Remove-Module
Remove-Module cmdlet 用于从当前会话中卸载模块。例如,要卸载 "MyModule",可以使用:
            
            
              powershell
              
              
            
          
          Remove-Module -Name MyModule
        3. Get-Module
Get-Module cmdlet 显示已导入或可用的模块。使用 -ListAvailable 参数可以列出所有可用的模块:
            
            
              powershell
              
              
            
          
          Get-Module -ListAvailable
        4. Install-Module
Install-Module cmdlet 允许你从 PowerShell 库或其他存储库安装模块。例如,要安装 "PSReadline" 模块,可以使用:
            
            
              powershell
              
              
            
          
          Install-Module -Name PSReadline
        5. Uninstall-Module
Uninstall-Module cmdlet 用于从系统中卸载模块。例如,要卸载 "PSReadline" 模块,可以使用:
            
            
              powershell
              
              
            
          
          Uninstall-Module -Name PSReadline
        6. Find-Module
Find-Module cmdlet 允许你在 PowerShell 库中搜索模块。例如,要查找与 "Azure" 相关的所有模块,可以使用:
            
            
              powershell
              
              
            
          
          Find-Module -Name *Azure*
        7. Save-Module
Save-Module cmdlet 允许你下载模块但不安装它。这对于在没有互联网连接的环境中手动安装模块很有用。例如,要下载 "PSReadline" 模块,可以使用:
            
            
              powershell
              
              
            
          
          Save-Module -Name PSReadline -Path C:\path\to\directory
        3.6 调试命令
在开发和运行 PowerShell 脚本时,可能会遇到错误或异常。为了帮助用户识别和解决这些问题,PowerShell 提供了一组调试命令。这些命令允许用户设置断点、检查变量的值、跟踪执行流程等,从而更容易地找到和修复问题。以下是与调试相关的一些常用命令及其功能介绍:
1. Set-PSBreakpoint (sbp)
Set-PSBreakpoint cmdlet 允许用户在脚本、行或命令上设置断点。当执行到断点时,脚本的执行将暂停,允许用户检查当前的状态。
例如,要在 "myscript.ps1" 脚本的第 10 行设置断点,可以使用:
            
            
              powershell
              
              
            
          
          Set-PSBreakpoint -Script myscript.ps1 -Line 10
        2. Get-PSBreakpoint
Get-PSBreakpoint cmdlet 显示当前设置的所有断点。
            
            
              powershell
              
              
            
          
          Get-PSBreakpoint
        3. Remove-PSBreakpoint (rbp)
Remove-PSBreakpoint cmdlet 允许用户删除一个或多个断点。例如,要删除 ID 为 1 的断点,可以使用:
            
            
              powershell
              
              
            
          
          Remove-PSBreakpoint -ID 1
        4. Get-PSCallStack
Get-PSCallStack cmdlet 显示当前的调用堆栈。这对于理解脚本的执行流程和确定当前执行的位置非常有用。
            
            
              powershell
              
              
            
          
          Get-PSCallStack
        5. Trace-Command
Trace-Command cmdlet 允许用户跟踪特定命令的执行,以获取详细的调试信息。例如,要跟踪 "Get-ChildItem" 命令的执行,可以使用:
            
            
              powershell
              
              
            
          
          Trace-Command -Name Metadata -Expression { Get-ChildItem } -PSHost
        6. Enter-PSHostProcess 和 Exit-PSHostProcess
这两个 cmdlet 允许用户进入和退出另一个 PowerShell 进程的会话,这对于调试运行中的脚本非常有用。
例如,要进入进程 ID 为 1234 的 PowerShell 进程,可以使用:
            
            
              powershell
              
              
            
          
          Enter-PSHostProcess -Id 1234
        退出该进程的会话后,可以使用:
            
            
              powershell
              
              
            
          
          Exit-PSHostProcess
        3.7 远程命令
PowerShell 的远程功能允许用户从一个计算机连接到另一个计算机,并在远程计算机上执行命令和脚本。这对于管理大型网络、自动化跨多台机器的任务或简单地从一个位置访问另一个位置的资源非常有用。以下是与远程相关的一些常用命令及其功能介绍:
1. Enter-PSSession
Enter-PSSession cmdlet 允许用户启动一个交互式会话到远程计算机。例如,要连接到名为 "RemoteServer" 的远程计算机,可以使用:
            
            
              powershell
              
              
            
          
          Enter-PSSession -ComputerName RemoteServer
        在此会话中,你可以执行命令就像在远程计算机的本地会话中一样。
2. Exit-PSSession
当你完成远程会话并想返回到本地计算机时,可以使用 Exit-PSSession cmdlet。
            
            
              powershell
              
              
            
          
          Exit-PSSession
        3. Invoke-Command
Invoke-Command cmdlet 允许用户在一个或多个远程计算机上执行命令或脚本。例如,要在 "RemoteServer" 上执行 "Get-Process" 命令,可以使用:
            
            
              powershell
              
              
            
          
          Invoke-Command -ComputerName RemoteServer -ScriptBlock { Get-Process }
        4. New-PSSession
New-PSSession cmdlet 允许用户创建一个或多个持久的远程会话。这些会话可以用于多次远程命令调用,而不必每次都重新建立连接。
            
            
              powershell
              
              
            
          
          $session = New-PSSession -ComputerName RemoteServer
        5. Remove-PSSession
Remove-PSSession cmdlet 用于关闭并删除之前使用 New-PSSession 创建的远程会话。
            
            
              powershell
              
              
            
          
          Remove-PSSession -Session $session
        6. Export-PSSession
Export-PSSession cmdlet 允许用户从远程会话导出命令、函数、别名和其他信息,并将它们保存为一个模块。这对于在本地计算机上使用远程计算机的功能非常有用。
            
            
              powershell
              
              
            
          
          Export-PSSession -Session $session -OutputModule RemoteFunctions
        7. Enable-PSRemoting 和 Disable-PSRemoting
这两个 cmdlet 用于启用或禁用计算机上的 PowerShell 远程功能。
要启用远程功能,可以使用:
            
            
              powershell
              
              
            
          
          Enable-PSRemoting
        要禁用远程功能,可以使用:
            
            
              powershell
              
              
            
          
          Disable-PSRemoting
        四、特殊操作符
PowerShell 提供了一系列特殊操作符,用于执行各种任务,如比较、模式匹配、数组操作等。以下是 PowerShell 中可用的特殊操作符及其详细介绍:
1. 算术操作符
-:减法+:加法*:乘法/:除法%:取余
2. 赋值操作符
=:赋值+=:增加并赋值-=:减少并赋值*=:乘以并赋值/=:除以并赋值
3. 比较操作符
-eq:等于-ne:不等于-gt:大于-lt:小于-ge:大于或等于-le:小于或等于-like:使用通配符进行字符串比较-notlike:不使用通配符进行字符串比较-match:使用正则表达式进行匹配-notmatch:不使用正则表达式进行匹配
4. 逻辑操作符
-and:逻辑与-or:逻辑或-not:逻辑非!:逻辑非的简写形式
5. 位操作符
-band:位与-bor:位或-bxor:位异或-bnot:位非-shl:左移-shr:右移
6. 重定向操作符
>:将输出重定向到文件,覆盖文件内容>>:将输出追加到文件2>:将错误输出重定向到文件2>&1:将错误输出和正常输出合并
7. 特殊操作符
-contains:检查数组是否包含特定值-notcontains:检查数组是否不包含特定值-in:检查值是否在数组中-notin:检查值是否不在数组中-replace:使用正则表达式替换字符串-split:使用正则表达式分割字符串-join:连接数组中的元素为一个字符串
8. 类型操作符
-is:检查对象是否为特定类型-isnot:检查对象是否不为特定类型-as:尝试将对象转换为特定类型
五、运行Windows命令行工具
5.1 常用命令行工具
PowerShell 不仅支持其自己的 cmdlets,还支持传统的 Windows 命令行工具 。这意味着你可以在 PowerShell 会话中直接运行这些工具,而无需切换到命令提示符(CMD)。以下是一些常用的 Windows 命令行工具及其简要描述:
1. ipconfig
ipconfig 是一个用于查看和管理 Windows IP 配置的工具。
- 基本用法 :
ipconfig显示计算机的所有网络接口的 IP 地址、子网掩码和默认网关。 - 参数 :
/all:显示所有网络接口的详细信息。/release:释放指定适配器的 IP 地址。/renew:更新指定适配器的 IP 地址。/flushdns:清除 DNS 解析器缓存。/displaydns:显示 DNS 解析器缓存的内容。/registerdns:刷新所有 DHCP 租约并重新注册 DNS 名称。
 
2. netstat
netstat 用于显示活动的网络连接、端口和网络统计信息。
- 基本用法 :
netstat显示活动的网络连接。 - 参数 :
-a:显示所有活动的网络连接和监听的端口。-b:显示在创建每个连接或监听端口时涉及的可执行文件。-n:以数字形式显示地址和端口号。-o:显示与每个连接关联的所有者的进程 ID。
 
3. ping
ping 工具用于测试网络连接的可达性。
- 基本用法 :
ping [目标主机名或 IP]向指定的主机或 IP 发送 ICMP 请求。 - 参数 :
-t:持续发送 ping 请求,直到用户中断。-a:解析主机名。-n [次数]:发送指定次数的 echo 请求。-l [大小]:发送指定大小的数据。
 
4. tracert
tracert 是一个用于诊断路由路径的工具。
- 基本用法 :
tracert [目标主机名或 IP]显示数据包从源计算机到指定目标的路由路径。 - 参数 :
-d:不解析地址。-h [最大跃点数]:指定最大跃点数。-j [主机列表]:沿途经过的主机列表。
 
5. nslookup
nslookup 是一个用于查询 DNS 名称和 IP 地址的工具。
- 基本用法 :
nslookup [主机名]查询指定主机名的 IP 地址。 - 参数 :
-type:查询的资源记录类型,如 A、MX、PTR 等。-debug:在响应中显示详细的开发信息。
 
6. sfc
sfc 是系统文件检查器,用于扫描和修复 Windows 系统文件。
- 基本用法 :
sfc /scannow扫描并尝试修复任何损坏的系统文件。 - 参数 :
/verifyonly:仅扫描并验证系统文件的完整性。/offbootdir:指定启动目录。/offwindir:指定 Windows 目录。
 
7. chkdsk
chkdsk 是一个用于检查和修复磁盘错误的工具。
- 基本用法 :
chkdsk [驱动器:]检查指定的驱动器。 - 参数 :
/f:修复磁盘上的错误。/r:查找坏扇区并恢复可读信息。/x:强制卷在检查前先卸载。
 
8. systeminfo
systeminfo 工具用于显示详细的系统信息。
- 基本用法 :
systeminfo显示操作系统、硬件和网络配置的详细信息。 - 参数 :
/s [计算机名]:指定要查看的远程系统。/u [用户名]:指定用户名进行身份验证。
 
9. gpupdate
gpupdate 是一个用于手动更新组策略的工具。
- 基本用法 :
gpupdate更新组策略。 - 参数 :
/force:强制重新应用所有设置。/logoff:如果需要,完成后注销用户。
 
10. tasklist
tasklist 工具用于显示当前运行的进程列表。
- 基本用法 :
tasklist显示所有当前运行的进程及其详细信息。 - 参数 :
/s [计算机名]:指定要查看的远程系统。/u [用户名]:指定用户名进行身份验证。/fi [过滤条件]:按照指定的条件显示任务列表。
 
这些命令行工具在日常的系统管理和故障排查中都非常有用。了解它们的参数和用法可以帮助你更有效地使用这些工具。
5.2 对应的cmdlet
| Windows Tool | PowerShell Cmdlet | 
|---|---|
| ipconfig | Get-NetIPAddress | 
| netstat | Get-NetTCPConnection | 
| ping | Test-Connection | 
| tracert | Test-NetConnection -TraceRoute | 
| nslookup | Resolve-DnsName | 
| sfc | Repair-WindowsImage | 
| chkdsk | Repair-Volume | 
| systeminfo | Get-ComputerInfo | 
| gpupdate | Invoke-GPUpdate | 
| tasklist | Get-Process | 
六、运行用户程序
除此,之外,我们自己安装的一些命令行应用也可以在Powershell中运行。
在Powershell中使用下列工具时,请先安装,并且添加到环境变量。
在Windows下,很多功能并不需要在终端中完成,使用图形化界面可能更省事,本文只是做个介绍。
6.1 vim(nvim)
nvim是vim的一个分支,更加友好,我之前的文章专门介绍过。
这里去github下载Windows版本的nvim使用。
和在Linux终端中的使用方式一样。
示例:


6.2 gcc / gdb
我是使用MinGW(Minimalist GNU for Windows)安装的gcc和gdb。
使用示例: 一个tcp服务端程序。
编译和运行

调试

把 永 远 爱 你 写 进 诗 的 结 尾 ~