Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

powershell

是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境

是命令提示符cmd.exe的扩充和颠覆

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

查看powershell版本

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

管道

把上一条命令的输出作为下一条命令的输入

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

重定向

把命令的输出保存到文件中,‘ > ’为覆盖,‘ >> ’追加

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

进行数学运算

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

执行外部命令

默认键入一个字符串,PowerShell会将它原样输出,如果该字符串是一个命令或者启动 程序。在字符串前加‘&’可以执行命令,或者启动程序

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

ipconfig 查看自己的网络配置

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

启动CMD控制台键入cmd或者cmd.exe,退出cmd可以通过命令exit

可以通过cmd /c help查找可用的cmd控制台命令

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

命令集 cmdlets

cmdlets是PowerShel1的内部命令, 若要查找用于查看和更改Windows 服务的cmdlet 列表

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

找到完成任务的cmdlet 后,可通过使用Get -Help cmdlet 了解有关该cmdlet 的详细信息

例如,若要显示有关Get-Service cmdlet 的帮助

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

若要全面了解该cmdlet 的输出,请将其输出通过管道传递给Get -Member cmdlet。 例如,下面的命令显示了有关Get-Service cmdlet所输出对象的成员的信息。

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

别名

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

别名的作用:

继承:继承unix shell 和windows cmd

方便:方便用户使用

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

设置powershell运行脚本的环境

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

定义变量

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

自动化变量

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

查看进程

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

可以通过Get-Help about_Automatic_variables 查看Automatic_variables 的帮助

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

环境变量

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

驱动器变量

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

变量的作用域

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

变量类型和强类型

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

当不知道curdate后面跟什么时 可执行命令

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

查看当天是星期几以及当天是今年的第几天

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

PowerShell数组和哈希表

命令返回数组

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

创建数组 数组必须是多个(符号、数字都可以)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

把字符串转化为数组

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

查看数组信息

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

查看数组a的长度

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

访问数组

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

强类型数组

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Int[] 进行四舍五入 并且必须是数字类型 否则会报错

使用hash表  @{}

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

添加值

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

删除值   remove()

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

使用管道

cmd管道是基于文本的,但是powershell是基于对象

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

ls | 对象排序 按名字降序 | 按对象名字长度,最后写入时间 | 转换成html格式 | 输出名字为ls.html

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

排序和分组

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

分组

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

过滤管道的结果

 过滤对象可以使用where-object

 $_.status:代表get-service.status

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

查看进程

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

根据CPU 大小降序排列筛选前5个

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

分析和比较管道结果

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

导入管道结果

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

导出管道结果

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

作业一 比较cmd和Powershell

查看CMD下常用命令在PowerShell对应具体哪条命令,查看帮助信息,并运行其中的示例程序。

 

切换目录

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

显示当前目录中内容

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

创建目录

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

拷贝文件

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

移动文件

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

显示设置时间

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

 

作业二 升级PowerShell

安装Windows Server2008 R2 SP1虚拟机

参考微软官方技术支持文档,把虚拟机中的PowerShell升级到5.1版本

 

  1.  安装Microsoft .NET Framework 4.5,

            https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=30653

  1.  安装Windows Management Framework 5.0,Windows 2008 R2选择安装

          【Win7AndW2K8R2-KB3134760-x64.msu】

           https://www.microsoft.com/en-us/download/details.aspx?id=54616

 

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)

Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)