PowerShell 变量(Variable)
2015-09-30 13:13
627 查看
PowerShell 中变量的好处是可以将一些命令或结果存储到变量中,从而更方便对象的操作和维护。PowerShell 中可以不指定类型声明变量,系统会自动识别并定义。PowerShell 中变量的声明是以美元符号("$")开头的,且变量名不区分大小写。
当前版本:
声明和赋值:
查看变量:
交换变量值:
重设变量:
声明只读变量:(参考
New-Variable)
查看当前已声明的变量:
查看变量更多属性:
删除变量值(不删除变量):
删除变量:(一般不需删除,关闭当前会话自动清除)
系统变量、自动变量:
查看环境变量:
添加、更改、删除 当前环境变量(当前会话有效):
查看、添加用户或系统环境变量:
变量作用域(全局、当前、私有、脚本):
$global
全局变量,在所有的作用域中有效,如果你在脚本或者函数中设置了全局变量,即使脚本和函数都运行结束,这个变量也任然有效。
$script
脚本变量,只会在脚本内部有效,包括脚本中的函数,一旦脚本运行结束,这个变量就会被回收。
$private
私有变量,只会在当前作用域有效,不能贯穿到其他作用域。
$local
默认变量,可以省略修饰符,在当前作用域有效,其它作用域只对它有只读权限。
全局变量和函数内部的变量:
查看变量类型:
变量类型转换:
变量类型参考:
参考:powershell 在线教程 Chapter
3. Variables
当前版本:
声明和赋值:
$a = 1 $b = 2 $c = $d = 3 ${a&a} = 10 $mydir = ls
查看变量:
$a Get-Variable a $a,$b,$c,$d,${a&a},$mydir
交换变量值:
$a,$b = $b,$a
重设变量:
$a = 100 Set-Variable -name b -value 99
声明只读变量:(参考
New-Variable)
New-Variable pi -Value 3.14 -Force -Option readonly New-Variable zero -Value 0 -Force -Option constant
Option | Description |
"None" | NOoption (default) |
"ReadOnly" | Variablecontents may only be modified by means of the -force parameter |
"Constant" | Variablecontents can't be modified at all. This option must already bespecified when the variable is created. Once specified thisoption cannot be changed. |
"Private" | Thevariable is visible only in a particular context (localvariable). |
"AllScope" | Thevariable is automatically copied in a new variable scope. |
ls variable:
查看变量更多属性:
ls Variable:pi | Format-List *
删除变量值(不删除变量):
Clear-Variable a
删除变量:(一般不需删除,关闭当前会话自动清除)
del variable:a del Variable:pi -Force del Variable:zero -Force #constant 不可删除 Remove-Variable c
系统变量、自动变量:
$HOME Get-Help about_Automatic_variables
查看环境变量:
$env:USERNAME ls env:USER* ls env:
添加、更改、删除 当前环境变量(当前会话有效):
#添加环境变量(当前会话有效) $env:TestVar="Test variable" #添加路径 $env:TestVar = $env:TestVar + ";F:\KK\" #删除环境变量 del env:TestVar
查看、添加用户或系统环境变量:
[Environment]::GetEnvironmentvariable("Path", "User") [Environment]::SetEnvironmentVariable("Path", "F:\KK\", "User") #查看/添加系统环境变量 [Environment]::GetEnvironmentvariable("Path", "Machine") [Environment]::SetEnvironmentVariable( "Path", $env:Path + ";F:\KK\", [System.EnvironmentVariableTarget]::Machine )
变量作用域(全局、当前、私有、脚本):
$global
全局变量,在所有的作用域中有效,如果你在脚本或者函数中设置了全局变量,即使脚本和函数都运行结束,这个变量也任然有效。
$script
脚本变量,只会在脚本内部有效,包括脚本中的函数,一旦脚本运行结束,这个变量就会被回收。
$private
私有变量,只会在当前作用域有效,不能贯穿到其他作用域。
$local
默认变量,可以省略修饰符,在当前作用域有效,其它作用域只对它有只读权限。
全局变量和函数内部的变量:
function f(){ "var=$var";$var="function inner";$var } $var = "12345" $var f $var
查看变量类型:
$var = "12345" $var.GetType() $var.GetType().Name $var.GetType().FullName (10).gettype().name (9999999999999999).gettype().name (3.14).gettype().name (3.14d).gettype().name ("WWW.MOSSFLY.COM").gettype().name (Get-Date).gettype().name
变量类型转换:
[String]$var = '2014-2-14' [Boolean]$var = 1 [datetime]'2014-2-14'
变量类型参考:
Variabletype | Description | Example |
[array] | Anarray | |
[bool] | Yes-novalue | [boolean]$flag= $true |
[byte] | Unsigned8-bit integer, 0...255 | [byte]$value= 12 |
[char] | Individualunicode character | [char]$a= "t" |
[datetime] | Dateand time indications | [datetime]$date= "12.Nov 2004 12:30" |
[decimal] | Decimalnumber | [decimal]$a= 12 $a = 12d |
[double] | Double-precisionfloating point decimal | $amount= 12.45 |
[guid] | Globallyunambiguous 32-byte identification number | [guid]$id= [System.Guid]::NewGuid() $id.toString() |
[hashtable] | Hashtable | |
[int16] | 16-bitinteger with characters | [int16]$value= 1000 |
[int32],[int] | 32-bitintegers with characters | [int32]$value= 5000 |
[int64],[long] | 64-bitintegers with characters | [int64]$value= 4GB |
[nullable] | Widensanother data type to include the ability to contain null values. Itcan be used, among others, to implement optional parameters | [Nullable``1[[System.DateTime]]]$test= Get-Date $test = $null |
[psobject] | PowerShellobject | |
[regex] | Regularexpression | $text= "Hello World" [regex]::split($text, "lo") |
[sbyte] | 8-bitintegers with characters | [sbyte]$value= -12 |
[scriptblock] | PowerShellscriptblock | |
[single],[float] | Single-precisionfloating point number | [single]$amount= 44.67 |
[string] | String | [string]$text= "Hello" |
[switch] | PowerShellswitch parameter | |
[timespan] | Timeinterval | [timespan]$t= New-TimeSpan $(Get-Date) "1.Sep 07" |
[type] | Type | |
[uint16] | Unsigned16-bit integer | [uint16]$value= 1000 |
[uint32] | Unsigned32-bit integer | [uint32]$value= 5000 |
[uint64] | Unsigned64-bit integer | [uint64]$value= 4GB |
[xml] | XMLdocument |
3. Variables
相关文章推荐
- linux shell 自定义函数
- shell——wget命令
- linux shell “(())” 双括号运算符使用
- linux下创建shell脚本
- XShell连接不了虚拟机linux Could not connect to '127.0.0.1' (port 22): Connection failed.
- shell编程中的$@和$*的区别
- Shell脚本编程与sed,awk工具的使用--9月15日课程作业
- shell 玩转文件描述符 和 重定向
- shell 之进行数学运算 let expr bc 中括号
- shell脚本编程-例子_批量添加用户脚本
- PowerShell 对象(Object)
- bash background tasks
- shell——变量自增
- Linux Shell脚本编程--netstat命令
- shell ——grep
- shell基础特性3
- 通过C++和shell脚本去解析URL && c++调用shell脚本后将脚本的内容返回给程序
- 递归的grep
- PowerShell工具脚本---按行数切割大文本文件
- ksh 命令退出状态