## 创建和初始化数组

``````\$A = 22,5,10,8,12,9,80
``````

``````\$B = ,7
``````

``````\$C = 5..8
``````

``````\$A.GetType()
``````

``````[int32[]]\$ia = 1500,2230,3350,4000
``````

``````[Diagnostics.Process[]]\$zz = Get-Process
``````

## 数组子表达式运算符

``````@( ... )
``````

``````\$a = @("Hello World")
\$a.Count
``````
``````1
``````
``````\$b = @()
\$b.Count
``````
``````0
``````

``````\$p = @(Get-Process Notepad)
``````

## 访问和使用数组元素

### 读取数组

``````\$a
``````
``````0
1
2
3
4
5
6
7
8
9
``````

``````\$a[0]
``````
``````0
``````

``````\$a[2]
``````
``````2
``````

``````\$a[1..4]
``````
``````1
2
3
4
``````

``````\$a = 0 .. 9
\$a[-3..-1]
``````
``````7
8
9
``````

``````\$a = 0 .. 9
\$a[-1..-3]
``````
``````9
8
7
``````

``````\$a = 0 .. 9
\$a[2..-2]
``````
``````2
1
0
9
8
``````

``````\$a = 0 .. 9
\$a[0,2+4..6]
``````
``````0
2
4
5
6
``````

``````\$a = 0..9
\$a[+0..2+4..6+8]
``````
``````0
1
2
4
5
6
8
``````

### 对数组元素的迭代

``````\$a = 0..9
foreach (\$element in \$a) {
\$element
}
``````
``````0
1
2
3
4
5
6
7
8
9
``````

`For` 检查数组中的元素时递增计数器时，循环很有用。 例如，若要使用 `For` 循环返回数组中的所有其他值，请键入：

``````\$a = 0..9
for (\$i = 0; \$i -le (\$a.length - 1); \$i += 2) {
\$a[\$i]
}
``````
``````0
2
4
6
8
``````

``````\$a = 0..9
\$i=0
while(\$i -lt 4) {
\$a[\$i];
\$i++
}
``````
``````0
1
2
3
``````

## 数组的属性

### Count 或 Length 或 LongLength

``````\$a = 0..9
\$a.Count
\$a.Length
``````
``````10
10
``````

### Rank

``````\$a = @(
@(0,1),
@("b", "c"),
@(Get-Process)
)

"`\$a rank: \$(\$a.Rank)"
"`\$a length: \$(\$a.Length)"
"`\$a length: \$(\$a.Length)"
"Process `\$a[2][1]: \$(\$a[2][1].ProcessName)"
``````

``````\$a rank: 1
\$a length: 3
\$a[2] length: 348
Process \$a[2][1]: AcroRd32
``````

``````[string[,]]\$rank2 = [string[,]]::New(3,2)
\$rank2.rank
\$rank2.Length
\$rank2[0,0] = 'a'
\$rank2[0,1] = 'b'
\$rank2[1,0] = 'c'
\$rank2[1,1] = 'd'
\$rank2[2,0] = 'e'
\$rank2[2,1] = 'f'
\$rank2[1,1]
``````
``````2
6
d
``````

``````\$a = "red",\$true
\$b = (New-Object 'int[,]' 2,2)
\$b[0,0] = 10
\$b[0,1] = 20
\$b[1,0] = 30
\$b[1,1] = 40
\$c = \$a + \$b
\$a.GetType().Name
\$b.GetType().Name
\$c.GetType().Name
\$c
``````

``````Object[]
Int32[,]
Object[]
red
True
10
20
30
40
``````

## 数组的方法

``````\$a = 1, 2, 3
\$a.Clear()
\$a | % { \$null -eq \$_ }
``````
``````True
True
True
``````

``````[int[]] \$intA = 1, 2, 3
\$intA.Clear()
\$intA
``````
``````0
0
0
``````

### ForEach

`ForEach`方法具有执行不同操作的几个重载。

``````ForEach(scriptblock expression)
ForEach(scriptblock expression, object[] arguments)
ForEach(type convertToType)
ForEach(string propertyName)
ForEach(string propertyName, object[] newValue)
ForEach(string methodName)
ForEach(string methodName, object[] arguments)
``````

#### ForEach (scriptblock expression， object[] arguments)

``````\$a = @(0 .. 3)
\$a.ForEach({ \$_ * \$_})
``````
``````0
1
4
9
``````

#### ForEach (类型 convertToType)

``````@("1/1/2017", "2/1/2017", "3/1/2017").ForEach([datetime])
``````
``````
Sunday, January 1, 2017 12:00:00 AM
Wednesday, February 1, 2017 12:00:00 AM
Wednesday, March 1, 2017 12:00:00 AM
``````

#### ForEach (propertyName， object[] newValue)

`ForEach`方法还可用于快速检索或设置集合中每个项的属性值。

``````# Set all LastAccessTime properties of files to the current date.
(dir 'C:\Temp').ForEach('LastAccessTime', (Get-Date))
# View the newly set LastAccessTime of all items, and find Unique entries.
(dir 'C:\Temp').ForEach('LastAccessTime') | Get-Unique
``````
``````Wednesday, June 20, 2018 9:21:57 AM
``````

#### ForEach (字符串 methodName，object[] 参数)

Last`ForEach`ly， 方法可用于对集合中每个项执行方法。

``````("one", "two", "three").ForEach("ToUpper")
``````
``````ONE
TWO
THREE
``````

### 其中

``````Where(scriptblock expression[, WhereOperatorSelectionMode mode
[, int numberToReturn]])
``````

• Default (0) - 返回所有项
• First (1) - 返回第一项
• Last (2) - 返回最后一项
• SkipUntil (3) - 跳过项直到条件为 true，则 返回剩余的项
• Until (4) - 返回所有项，直到条件为 true
• Split (5) - 返回两个元素的数组
• 第一个元素包含匹配的项
• 第二个元素包含剩余的项

``````(0..9).Where{ \$_ % 2 }
``````
``````1
3
5
7
9
``````

``````('hi', '', 'there').Where({\$_.Length})
``````
``````hi
there
``````

#### Default

``````# Get the zip files in the current users profile, sorted by LastAccessTime.
\$Zips = dir \$env:userprofile -Recurse '*.zip' | Sort-Object LastAccessTime
# Get the least accessed file over 100MB
\$Zips.Where({\$_.Length -gt 100MB}, 'Default', 1)
``````

#### Last

``````\$h = (Get-Date).AddHours(-1)
\$logs = dir 'C:\' -Recurse '*.log' | Sort-Object CreationTime
# Find the last 5 log files created in the past hour.
\$logs.Where({\$_.CreationTime -gt \$h}, 'Last', 5)
``````

#### SkipUntil

``````\$computers = "Server01", "Server02", "Server03", "localhost", "Server04"
# Find the first available online server.
\$computers.Where({ Test-Connection \$_ }, 'SkipUntil', 1)
``````
``````localhost
``````

#### Until

`Until`模式反转 `SkipUntil` 模式。 它返回 集合 中的 ALL 项，直到项通过脚本块表达式。 一旦 项传递 scriptblock 表达式， `Where` 该方法将停止处理项。

``````# Retrieve the first set of numbers less than or equal to 10.
(1..50).Where({\$_ -gt 10}, 'Until')
# This would perform the same operation.
(1..50).Where({\$_ -le 10})
``````
``````1
2
3
4
5
6
7
8
9
10
``````

`Until` `SkipUntil` 均在不测试一批项的前提是运行。

`Until`返回第一次 传递之前的项目

`SkipUntil`返回第一次 传递后的所有项， 包括第一个传递的项。

#### Split

``````\$running, \$stopped = (Get-Service).Where({\$_.Status -eq 'Running'}, 'Split')
\$running
``````
``````Status   Name               DisplayName
------   ----               -----------
Running  Appinfo            Application Information
Running  AudioEndpointBu... Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
...
``````
``````\$stopped
``````
``````Status   Name               DisplayName
------   ----               -----------
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
...
``````

`foreach` `where` 方法都是内部成员。 有关内部成员的信息 ，请参阅about_Instrinsic_Members

## 获取数组的成员

``````Get-Member -InputObject \$a
``````

``````,\$a | Get-Member

,(1,2,3) | Get-Member
``````

## 操作数组

``````\$a[1] = 10
``````

``````\$a.SetValue(500,1)
``````

``````\$a = @(0..4)
\$a += 5
``````

``````\$t = \$a[0,1 + 3..(\$a.length - 1)]
``````

``````\$x = 1,3
\$y = 5,9
\$z = \$x + \$y
``````

`\$a = \$null`

## 零或一的数组

### 零个对象

``````\$a = \$null
\$a.Count
\$a.Length
``````
``````0
0
``````

### 一个对象

``````\$a = 4
\$a.Count
\$a.Length
\$a[0]
\$a[-1]
``````
``````1
1
4
4
``````

## 对系统的索引编制支持。Tuple 对象

PowerShell 6.1 添加了对对象的索引访问 Tuple 的支持，类似于数组。 例如：

``````PS> \$tuple = [Tuple]::Create(1, 'test')
PS> \$tuple[0]
1
PS> \$tuple[1]
test
PS> \$tuple[0..1]
1
test
PS> \$tuple[-1]
test
``````