认识PowerShell
$psversiontable查看版本
PowerShell强大之处
PowerShell执行外部命令
&”notepad”
PowerShell命令集
以动名词来命名命令
- cmd 命令在powershell中可以直接使用 - get-command # 获取所有命令列表 
- 帮助命令走天下 - get-help - PowerShell别名使用- get-alias -name ls # 查询ls的原始命令 - PowerShell自定义别名- set-alias -name pad -value notepad # 设置临时别名,将notepad赋给pad别名 
 del alias:pad # 删除别名
 export-alias 1.psl # 导出别名
 import-alias -force 1.psl # 强制导入别名- PowerShell变量基础
- 等于号赋值$name=”xx” 
- 特殊变量名称用花括号包围${“asdsad asdsd” var ()} PowerShell变量操作
- 支持多变量赋值
 $name,$name2=1,2
- 查看正在使用的变量Get-Variable num* # 查找num特定变量值 
- 确定变量是否存在test-path variable:num1 # 返回的是布尔值 
- 删除变量名Remove-Variable num1 
PowerShell自动化变量
常用的变量
- $pid
- $home
PowerShell环境变量
ls env: # 查看当前环境变量
$env:os # 输出某个键的值
$env:0s=”Linux” # 临时赋值变量
- 设置永久环境变量(.net方式)[environment]\::setenvironmentvariable(“PATH”,”D:\”,”User”) PowerShell脚本执行策略get-executionpolicy # 查看当前运行策略 
| 1 | 策略分类 | 
set-executionpolicy RemoteSigned # 设置可以运行的策略
PowerShell与其他脚本程序的互相调用
- -eq #等于
- -lt #小于
- -gt #大于
- -contains #不包含1,3,5 -contains 3 
- -notcontains
- -not
- -and
- -or
- -ne #不等于1,3,5 -ne 3 PowerShell条件判断【if语句】if($num -gt 100){“1”} elseif($num -eq 100){“0”} else {“-1”} PowerShell条件判断【switch语句】1 
 2
 3
 4
 5
 6
 7
 8$number = 49 
 switch($number)
 {
 {$_ -le 50} {"此数值小于50"}
 {$_ -eq 50} {"此数值等于50"}
 {$_ -gt 50} {"此数值大于50"}
 }
 \\ $_代表变量
PowerShell循环结构【foreach语句】
| 1 | $arr = 1,2,3,4,5 或者 $arr=1..10 | 
PowerShell循环结构【while语句】
| 1 | $num = 15 | 
- dowhile 至少运行一次1 
 2
 3
 4
 5
 6do 
 {
 $num
 $num=$num-1
 }
 while($num -gt 15)
break和continue关键字使用
break跳出1
2
3
4
5
6
7
8
9
10
11
12
13$num=1
while($num -lt 6)
{
    if($num -gt 4)
    {
        break
    }
    else
    {
        $num
        $num++
    }
}
continue跳过1
2
3
4
5
6
7
8
9
10
11
12
13$num=1
while($num -lt 6)
{
    if($num -gt 4)
    {
        break
    }
    else
    {
        $num
        $num++
    }
}
PowerShell循环结构【for语句】
| 1 | $sum=0 | 
PowerShell循环结构【switch循环】
| 1 | $num=1..10 | 
PowerShell数组的创建
| 1 | $arr = ipconfig | 
$arr=ipconfig #cmd命令也可以执行
$arr=@() #创建空数组
$arr=1..10,”string”,(get-date)#创建混合数组
PowerShell访问数组
$arr[0..2]
PowerShell自定义函数及调用
| 1 | function myping($url) | 
PowerShell函数返回值
return
PowerShell定义文本
`转义
PowerShell实现用户交互
| 1 | $input=read-host "请输入具体的路径" | 
PowerShell格式化字符串
| 1 | "my name is {0} ,iam {1} years old" -f $name,$age | 
