如何使用Powershell的Get-help

  初学Powershell必不可少的是学会使用其的帮助文档,这样可以更高效的让我们学会Powershell。
  下面就说说新手学习Powershell如何从Powershell中的帮助文档中获取更多信息吧。我已最常用的获取进程(Get-service)为例,给大家讲讲。
1.可更新的帮助
  当第一次使用帮助文档时,需要更新帮助文档(确保电脑网络是通的)。需要注意的要点是需要在管理员权限下打开Powershell,否则就会出现无法更新的情况。
  那么怎么更新帮助文档呢  管理员模式下打开Powershell,输入update-help(小技巧:输入过程中可以使用tab快捷键进行自动补全哦)。
PS C:\Windows\system32> Update-Help
  输入完成之后,敲击回车enter就可以自动更新了。
2.使用帮助文档找命令
  比如我要查看当前的的服务Service。但是我不知道命令是什么。于是就可以这样做。其中*是通配符,通配符是啥呢,就是可以代替一个不知道输入内容。
PS C:\Windows\system32> help *service*

Name                              Category  Module                    Synop
----                              --------  ------                    -----
Get-Service                       Cmdlet    Microsoft.PowerShell.M... Gets
New-Service                       Cmdlet    Microsoft.PowerShell.M... Creat
New-WebServiceProxy               Cmdlet    Microsoft.PowerShell.M... Creat
Restart-Service                   Cmdlet    Microsoft.PowerShell.M... Stops
Resume-Service                    Cmdlet    Microsoft.PowerShell.M... Resum
Set-Service                       Cmdlet    Microsoft.PowerShell.M... Start
Start-Service                     Cmdlet    Microsoft.PowerShell.M... Start
Stop-Service                      Cmdlet    Microsoft.PowerShell.M... Stops
Suspend-Service                   Cmdlet    Microsoft.PowerShell.M... Suspe
Get-NetFirewallServiceFilter      Function  NetSecurity               ...
Set-NetFirewallServiceFilter      Function  NetSecurity               ...
  然后就可以看到需要命令与Service有关。只有英文差不多的就可以看出来Get-service就是我们要中命令。但是现在还不确定的是这个命令是不是直接用就可以了。下面我们来说说怎么回去详细的使用方法。
3.获取详细的帮助
  上面我们已经知道get-service可以获取当前host上的服务。那么怎么确定就是这样呢,可以这样做。查看帮助文档里的例子。然后就可以看到详细的说明帮助啦。
PS C:\Windows\system32> help Get-Service -Examples

名称
    Get-Service

摘要
    Gets the services on a local or remote computer.

    Example 1: Get all services on the computer

    PS C:\>Get-Service


    This command gets all of the services on the computer. It behaves as though you typed `Get-Service *`. The default
    display shows the status, service name, and display name of each service.
    Example 2: Get services that begin with a search string

    PS C:\>Get-Service "wmi*"


    This command retrieves services with service names that begin with WMI (the acronym for Windows Management Instrume
    ntation).
    Example 3: Display services that include a search string

    PS C:\>Get-Service -Displayname "*network*"


    This command displays services with a display name that includes the word network. Searching the display name finds
     network-related services even when the service name does not include "Net", such as xmlprov, the Network Provision
    ing Service.
    Example 4: Get services that begin with a search string and an exclusion

    PS C:\>Get-Service -Name "win*" -Exclude "WinRM"


    These commands get only the services with service names that begin with win, except for the WinRM service.
    Example 5: Display services that are currently active

    PS C:\>Get-Service | Where-Object {$_.Status -eq "Running"}
  好啦,就到这吧。

                      愿每个人都是上帝眷顾的幸运儿
<think>好的,用户之前问过如何将ADB路径加入系统环境变量,现在他们又想知道如何使用PowerShell。我需要考虑他们的真实需求是什么。可能他们刚刚开始接触PowerShell,或者想更高效地管理系统配置,比如之前的环境变量设置。 首先,用户可能是开发人员或IT管理员,需要自动化任务或系统管理。之前他们用图形界面和PowerShell命令添加环境变量,现在想深入学习PowerShell使用方法。他们可能对命令行不太熟悉,所以需要基础到进阶的内容,包括基本命令、脚本编写、执行策略等。 接下来,我要确保覆盖PowerShell的基础知识,比如启动方式、基本命令(Get-Command、Get-Help)、文件操作、系统管理任务(如进程管理、服务控制、环境变量设置)。同时,用户可能对如何运行脚本和模块管理感兴趣,特别是他们之前用过环境变量设置命令,这部分需要详细说明。 还要注意常见问题,比如执行策略的问题,因为用户可能在尝试运行脚本时遇到权限错误。需要解释如何安全地更改执行策略,并推荐最佳实践。此外,远程管理和模块安装也是进阶用户可能需要的功能,应该包括进去。 最后,要确保结构清晰,分步骤讲解,从基础到高级,帮助用户逐步掌握PowerShell。可能需要提醒用户注意安全性,比如不要随意运行不明脚本,以及如何获取帮助信息。同时,结合他们之前的环境变量问题,可以举例说明如何在PowerShell中操作环境变量,巩固他们的应用场景。</think>以下是Windows PowerShell的详细使用指南,从基础操作到高级应用分层讲解: --- ### 一、PowerShell基础认知 1. **启动方式**: - Win+R → 输入`powershell` → Enter(启动普通会话) - 右键开始菜单 → Windows PowerShell (管理员) 2. **命令格式**: ```powershell 动词-名词 [-参数] [值] # 示例: Get-ChildItem -Path "C:\" -Filter *.txt ``` 3. **别名系统**: | 完整命令 | 别名 | 功能等价于CMD命令 | |-------------------|-------|-----------------| | `Get-ChildItem` | `dir` | `dir` | | `Copy-Item` | `cp` | `copy` | | `Remove-Item` | `rm` | `del` | --- ### 二、必备核心命令 #### 1. 帮助系统 ```powershell Get-Help <命令名> -Examples # 查看命令示例 Update-Help # 更新帮助文档(需管理员权限) ``` #### 2. 文件操作 ```powershell Get-ChildItem -Recurse -Filter *.log # 递归搜索日志文件 Copy-Item "D:\test.txt" -Destination "E:\Backup\" -Force # 强制覆盖复制 Compress-Archive -Path "C:\Reports\" -DestinationPath "D:\Reports.zip" # 创建压缩包 ``` #### 3. 系统管理 ```powershell Get-Process -Name "chrome" # 查看Chrome进程 Stop-Process -Name "notepad" -Force # 强制终止记事本进程 Get-Service -Name "WinDefend" | Restart-Service # 重启Windows Defender服务 ``` --- ### 三、脚本开发入门 #### 1. 创建与执行 ```powershell # 示例脚本 test.ps1 $name = Read-Host "输入你的名字" Write-Host "你好, $name!当前时间: $(Get-Date)" ``` #### 2. 执行策略设置 ```powershell Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # 允许本地脚本运行 ``` #### 3. 条件判断 ```powershell $age = 25 if ($age -gt 18) { Write-Host "成年人" } elseif ($age -ge 13) { Write-Host "青少年" } else { Write-Host "儿童" } ``` --- ### 四、高级功能应用 #### 1. 管道操作 ```powershell Get-Process | Where-Object { $_.CPU -gt 100 } | Sort-Object -Property CPU -Descending ``` #### 2. 远程管理 ```powershell Enter-PSSession -ComputerName Server01 -Credential AdminUser # 建立远程会话 Invoke-Command -ComputerName Server01,Server02 -ScriptBlock { Get-Service } # 批量执行 ``` #### 3. 模块管理 ```powershell Find-Module -Name PSWindowsUpdate # 搜索模块 Install-Module -Name PSWindowsUpdate -Force # 安装模块 Import-Module PSWindowsUpdate # 加载模块 Get-WindowsUpdate -Install -AcceptAll # 执行系统更新 ``` --- ### 五、实用技巧 1. **历史记录**: ```powershell # 查看历史命令 Get-History # 执行历史命令ID为5的命令 Invoke-History -Id 5 ``` 2. **快捷键**: - Tab:命令补全 - Ctrl+C:强制终止当前操作 - ↑/↓:浏览历史命令 3. **配置文件**: ```powershell notepad $PROFILE # 编辑启动配置文件 ``` --- ### 六、安全注意事项 1. 谨慎执行来自不可信来源的`.ps1`文件 2. 敏感操作前使用`-WhatIf`参数预演:`Remove-Item *.tmp -WhatIf` 3. 定期检查执行策略:`Get-ExecutionPolicy -List` > 💡 **提示**:可通过`Get-Command -Verb Get | More`查看所有Get类命令,逐步探索更多功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M_pengpeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值