Windows powershell基本命令(别名、变量、数组和哈希表、管道、升级power shell)
powershell
是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境
是命令提示符cmd.exe的扩充和颠覆
查看powershell版本
管道
把上一条命令的输出作为下一条命令的输入
重定向
把命令的输出保存到文件中,‘ > ’为覆盖,‘ >> ’追加
进行数学运算
执行外部命令
默认键入一个字符串,PowerShell会将它原样输出,如果该字符串是一个命令或者启动 程序。在字符串前加‘&’可以执行命令,或者启动程序
ipconfig 查看自己的网络配置
启动CMD控制台键入cmd或者cmd.exe,退出cmd可以通过命令exit
可以通过cmd /c help查找可用的cmd控制台命令
命令集 cmdlets
cmdlets是PowerShel1的内部命令, 若要查找用于查看和更改Windows 服务的cmdlet 列表
找到完成任务的cmdlet 后,可通过使用Get -Help cmdlet 了解有关该cmdlet 的详细信息
例如,若要显示有关Get-Service cmdlet 的帮助
若要全面了解该cmdlet 的输出,请将其输出通过管道传递给Get -Member cmdlet。 例如,下面的命令显示了有关Get-Service cmdlet所输出对象的成员的信息。
别名
别名的作用:
继承:继承unix shell 和windows cmd
方便:方便用户使用
设置powershell运行脚本的环境
定义变量
自动化变量
查看进程
可以通过Get-Help about_Automatic_variables 查看Automatic_variables 的帮助
环境变量
驱动器变量
变量的作用域
变量类型和强类型
当不知道curdate后面跟什么时 可执行命令
查看当天是星期几以及当天是今年的第几天
PowerShell数组和哈希表
命令返回数组
创建数组 数组必须是多个(符号、数字都可以)
把字符串转化为数组
查看数组信息
查看数组a的长度
访问数组
强类型数组
Int[] 进行四舍五入 并且必须是数字类型 否则会报错
使用hash表 @{}
添加值
删除值 remove()
使用管道
cmd管道是基于文本的,但是powershell是基于对象
ls | 对象排序 按名字降序 | 按对象名字长度,最后写入时间 | 转换成html格式 | 输出名字为ls.html
排序和分组
分组
过滤管道的结果
过滤对象可以使用where-object
$_.status:代表get-service.status
查看进程
根据CPU 大小降序排列筛选前5个
分析和比较管道结果
导入管道结果
导出管道结果
作业一 比较cmd和Powershell
查看CMD下常用命令在PowerShell对应具体哪条命令,查看帮助信息,并运行其中的示例程序。
切换目录
显示当前目录中内容
创建目录
拷贝文件
移动文件
显示设置时间
作业二 升级PowerShell
安装Windows Server2008 R2 SP1虚拟机
参考微软官方技术支持文档,把虚拟机中的PowerShell升级到5.1版本
- 安装Microsoft .NET Framework 4.5,
https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=30653
- 安装Windows Management Framework 5.0,Windows 2008 R2选择安装
【Win7AndW2K8R2-KB3134760-x64.msu】
https://www.microsoft.com/en-us/download/details.aspx?id=54616