当前版本:
声明和赋值:
$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 |
[double] | Double-precisionfloating point decimal | $amount= 12.45 |
[guid] | Globallyunambiguous 32-byte identification number | [guid]$id= [System.Guid]::NewGuid() |
[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 |
[psobject] | PowerShellobject |
|
[regex] | Regularexpression | $text= "Hello World" |
[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 |
|
参考:powershell 在线教程 Chapter 3. Variables