您想知道有關於陣列的一切

基本使用方式

建立陣列

``````PS> \$data = @()
PS> \$data.count
0
``````

``````PS> \$data = @('Zero','One','Two','Three')
PS> \$data.count
4

PS> \$data
Zero
One
Two
Three
``````

``````\$data = @(
'Zero'
'One'
'Two'
'Three'
)
``````

其他語法

``````\$data = 'Zero','One','Two','Three'
``````

Write-Output 建立陣列

``````\$data = Write-Output Zero One Two Three
``````

存取項目

Offset

``````PS> \$data = 'Zero','One','Two','Three'
PS> \$data[0]
Zero
``````

``````PS> \$data[1]
One
``````

``````PS> \$data[3]
Three
``````

特殊索引訣竅

``````PS> \$data[0,2,3]
Zero
Two
Three
``````

``````PS> \$data[3,0,3]
Three
Zero
Three
``````

``````PS> \$data[1..3]
One
Two
Three
``````

``````PS> \$data[3..1]
Three
Two
One
``````

``````PS> \$data[-1]
Three
``````

``````PS> \$a = 1,2,3,4,5,6,7,8
PS> \$a[2..-1]
3
2
1
8
``````

``````PS> \$a[2,1,0,-1]
3
2
1
8
``````

超出範圍

``````PS> \$null -eq \$data[9000]
True
``````

無法為 null 陣列編製索引

``````PS> \$empty = \$null
PS> \$empty[0]
Error: Cannot index into a null array.
``````

Count

``````PS> \$data.count
4
``````

PowerShell 3.0 已將 count 屬性新增至大部分的物件。 您可以擁有單一物件，而且物件應該會提供您 `1` 的計數。

``````PS> \$date = Get-Date
PS> \$date.count
1
``````

``````PS> \$null.count
0
``````

差一錯誤

``````\$data[ \$data.count ]
``````

PowerShell 很樂意讓您這麼做，並為您提供索引 4的確切項目： `\$null`。 您應該使用 `\$data.count - 1` 或我們所學習的 `-1`

``````PS> \$data[ \$data.count - 1 ]
Three
``````

``````PS> \$data[ -1 ]
Three
``````

Lee Dailey 也會向我指出，我們可以使用 `\$data.GetUpperBound(0)` 來取得最大的索引編號。

``````PS> \$data.GetUpperBound(0)
3
PS> \$data[ \$data.GetUpperBound(0) ]
Three
``````

正在更新項目

``````\$data[2] = 'dos'
\$data[3] = 'tres'
``````

``````PS> \$data[4] = 'four'
Index was outside the bounds of the array.
At line:1 char:1
+ \$data[4] = 'four'
+ ~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException
``````

反覆運算

管線

``````PS> \$data = 'Zero','One','Two','Three'
PS> \$data | ForEach-Object {"Item: [\$PSItem]"}
Item: [Zero]
Item: [One]
Item: [Two]
Item: [Three]
``````

ForEach 迴圈

`ForEach` 迴圈適用於集合。 使用語法： `foreach ( <variable> in <collection> )`

``````foreach ( \$node in \$data )
{
"Item: [\$node]"
}
``````

ForEach 方法

``````PS> \$data.foreach({"Item [\$PSItem]"})
Item [Zero]
Item [One]
Item [Two]
Item [Three]
``````

`.foreach()` 接受屬於指令碼區塊的參數。 您可以置放括號，並僅提供指令碼區塊。

``````\$data.foreach{"Item [\$PSItem]"}
``````

For 迴圈

``````for ( \$index = 0; \$index -lt \$data.count; \$index++)
{
"Item: [{0}]" -f \$data[\$index]
}
``````

Switch 迴圈

``````\$data = 'Zero','One','Two','Three'
switch( \$data )
{
'One'
{
'Tock'
}
'Three'
{
'Tock'
}
Default
{
'Tick'
}
}
``````
``````Tick
Tock
Tick
Tock
``````

更新值

``````for ( \$index = 0; \$index -lt \$data.count; \$index++ )
{
\$data[\$index] = "Item: [{0}]" -f \$data[\$index]
}
``````

物件陣列

``````\$data = @(
[pscustomobject]@{FirstName='Kevin';LastName='Marquette'}
[pscustomobject]@{FirstName='John'; LastName='Doe'}
)
``````

``````\$processList = Get-Process
``````

存取屬性

``````PS> \$data[0]

FirstName LastName
-----     ----
Kevin     Marquette
``````

``````PS> \$data[0].FirstName

Kevin

PS> \$data[0].FirstName = 'Jay'
PS> \$data[0]

FirstName LastName
-----     ----
Jay       Marquette
``````

陣列屬性

``````PS> \$data | ForEach-Object {\$_.LastName}

Marquette
Doe
``````

``````PS> \$data | Select-Object -ExpandProperty LastName

Marquette
Doe
``````

``````PS> \$data.LastName

Marquette
Doe
``````

Where-Object 篩選

``````PS> \$data | Where-Object {\$_.FirstName -eq 'Kevin'}

FirstName LastName
-----     ----
Kevin     Marquette
``````

``````\$data | Where FirstName -eq Kevin
``````

Where()

``````\$data.Where({\$_.FirstName -eq 'Kevin'})
``````

更新迴圈中的物件

``````foreach(\$person in \$data)
{
\$person.FirstName = 'Kevin'
}
``````

``````foreach(\$person in \$data)
{
\$person = [pscustomobject]@{
FirstName='Kevin'
LastName='Marquette'
}
}
``````

操作員

PowerShell 中的運算子也適用陣列。 其中一些工作的執行方式稍有不同。

-join

`-join` 運算子最明顯，因此讓我們先瞭解一下。 我喜歡 `-join` 運算子並經常使用。 它會將陣列中的所有元素與您指定的字元或字串聯結在一起。

``````PS> \$data = @(1,2,3,4)
PS> \$data -join '-'
1-2-3-4
PS> \$data -join ','
1,2,3,4
``````

``````PS> 1 -join '-'
1
``````

``````PS> \$data = @(1,2,3,4)
PS> "Data is \$(\$data -join ',')."
Data is 1,2,3,4.
``````

-join \$array

Lee Dailey 在這裡向我傳授一個聰明的技巧。 如果您想要加入任何不含分隔符號的內容，您可以改用以下方式：

``````PS> \$data = @(1,2,3,4)
PS> \$data -join \$null
1234
``````

``````PS> \$data = @(1,2,3,4)
PS> -join \$data
1234
``````

-replace 與 -split

`-replace``-split` 等其他運算子會在陣列中的每個項目上執行。 我不能說我曾用過這種方式，但這裡有一個範例。

``````PS> \$data = @('ATX-SQL-01','ATX-SQL-02','ATX-SQL-03')
PS> \$data -replace 'ATX','LAX'
LAX-SQL-01
LAX-SQL-02
LAX-SQL-03
``````

-contains

`-contains` 運算子可讓您檢查值的陣列，以查看其是否包含指定的值。

``````PS> \$data = @('red','green','blue')
PS> \$data -contains 'green'
True
``````

-in

``````PS> \$data = @('red','green','blue')
PS> 'green' -in \$data
True
``````

``````PS> \$data = @('red','green','blue')
PS> \$pattern = "^({0})\$" -f (\$data -join '|')
PS> \$pattern
^(red|green|blue)\$

PS> 'green' -match \$pattern
True
``````

-eq 和-ne

``````PS> \$data = @('red','green','blue')
PS> \$data -eq 'green'
green
``````

``````PS> \$data = @('red','green','blue')
PS> \$data -ne 'green'
red
blue
``````

``````\$data = @('red','green','blue')
if ( \$data -eq 'green' )
{
'Green was found'
}
if ( \$data -ne 'green' )
{
}
``````

-match

`-match` 運算子會嘗試比對集合中的每個項目。

``````PS> \$servers = @(
'LAX-SQL-01'
'LAX-API-01'
'ATX-SQL-01'
'ATX-API-01'
)
PS> \$servers -match 'SQL'
LAX-SQL-01
ATX-SQL-01
``````

``````\$servers | Select-String SQL
``````

\$null 或 empty

``````if ( \$array -eq \$null)
{
'Array is \$null'
}
``````

``````\$array = @('one',\$null,'three')
if ( \$array -eq \$null)
{
'I think Array is \$null, but I would be wrong'
}
``````

``````if ( \$null -eq \$array )
{
'Array actually is \$null'
}
``````

`\$null` 陣列與空陣列屬於不同的陣列。 若您知道自己有陣列，請檢查其中的物件計數。 如果陣列為 `\$null`，則計數為 `0`

``````if ( \$array.count -gt 0 )
{
"Array isn't empty"
}
``````

``````PS> \$object = [PSCustomObject]@{Name='TestObject'}
PS> \$object.count
\$null
``````

``````if ( @(\$array).count -gt 0 )
{
"Array isn't empty"
}
``````

``````if ( \$null -ne \$array -and @(\$array).count -gt 0 )
{
"Array isn't empty"
}
``````

全部 -eq

``````\$results = Test-Something
if ( -not ( \$results -ne 'Passed') )
{
'All results a Passed'
}
``````

新增至陣列

PowerShell 不會實作減法運算。 如果您想要有彈性的陣列替代方案，則必須使用泛型`List`物件。

新增陣列

``````\$first = @(
'Zero'
'One'
)
\$second = @(
'Two'
'Three'
)
``````

``````PS> \$first + \$second

Zero
One
Two
Three
``````

Plus equals +=

``````\$data = @(
'Zero'
'One'
'Two'
'Three'
)
\$data += 'four'
``````

管道指派

``````\$array = 1..5 | ForEach-Object {
"ATX-SQL-\$PSItem"
}
``````

``````\$array = foreach ( \$node in (1..5))
{
"ATX-SQL-\$node"
}
``````

陣列類型

強類型陣列

``````PS> [int[]] \$numbers = 1,2,3
PS> [int[]] \$numbers2 = 'one','two','three'
ERROR: Cannot convert value "one" to type "System.Int32". Input string was not in a correct format."

PS> [string[]] \$strings = 'one','two','three'
``````

ArrayList

``````\$myarray = [System.Collections.ArrayList]::new()
``````

泛型清單

``````\$mylist = [System.Collections.Generic.List[string]]::new()
``````

``````\$mylist = [System.Collections.Generic.List[int]]::new()
``````

``````\$mylist = [System.Collections.Generic.List[int]]@(1,2,3)
``````

``````using namespace System.Collections.Generic
\$myList = [List[int]]@(1,2,3)
``````

``````\$myList.Add(10)
``````

``````PS> \$myList[-1]
10
``````

List[PSObject]

``````\$list = [List[PSObject]]::new()
``````

Remove()

`ArrayList` 和泛型 `List[]` 都支援從集合中移除項目。

``````using namespace System.Collections.Generic
\$myList = [List[string]]@('Zero','One','Two','Three')
[void]\$myList.Remove("Two")
Zero
One
Three
``````

``````[list[System.Management.Automation.PSDriveInfo]]\$drives = Get-PSDrive
\$drives.remove(\$drives[2])
``````
``````\$delete = \$drives[2]
\$drives.remove(\$delete)
``````

其他細微差異

預先調整大小的陣列

``````\$data = [Object[]]::new(4)
\$data.count
4
``````

相乘陣列

``````PS> \$data = @('red','green','blue')
PS> \$data * 3
red
green
blue
red
green
blue
red
green
blue
``````

以 0 初始化

``````PS> [int[]]::new(4)
0
0
0
0
``````

``````PS> \$data = @(0) * 4
PS> \$data
0
0
0
0
``````

「相乘」技巧的好處是您可以使用任何值。 因此，如果您想要以 `255` 做為預設值，這會是很好的方式。

``````PS> \$data = @(255) * 4
PS> \$data
255
255
255
255
``````

嵌套陣列

``````\$data = @(@(1,2,3),@(4,5,6),@(7,8,9))

\$data2 = @(
@(1,2,3),
@(4,5,6),
@(7,8,9)
)
``````

``````PS> \$outside = 0
PS> \$inside = 2
PS> \$data[\$outside][\$inside]
3
``````

Write-Output -NoEnumerate

PowerShell 習慣取消換行或列舉陣列。 這是 PowerShell 使用管道的核心層面，但有時您不希望出現這種情況。

``````PS> \$data = @('red','green','blue')
PS> \$data | Get-Member
TypeName: System.String
...
``````

``````PS> Write-Output -NoEnumerate \$data | Get-Member
TypeName: System.Object[]
...
``````

``````PS> ,\$data | Get-Member
TypeName: System.Object[]
...
``````

傳回陣列

Catch 是您有新的陣列。 如果這樣造成困擾，您可以使用 `Write-Output -NoEnumerate \$array``return ,\$array` 來解決問題。