1. 程式人生 > >PowerShell 變數(Variable)

PowerShell 變數(Variable)

PowerShell  中變數的好處是可以將一些命令或結果儲存到變數中,從而更方便物件的操作和維護。PowerShell  中可以不指定型別宣告變數,系統會自動識別並定義。PowerShell  中變數的宣告是以美元符號("$")開頭的,且變數名不區分大小寫。

當前版本:


宣告和賦值:

$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 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