开发者

What is correct syntax for arrays in powershell?

开发者 https://www.devze.com 2023-02-05 06:32 出处:网络
Example1: Note 2: The comma is also used so separate items in an array {0,-30} Example2: To create an array, we create a

Example1:

Note 2: The comma is also used so separate items in an array {0,-30}

Example2:

To create an array, we create a variable and assign the array. Arrays are noted by the “@” symbol. Let’s take the discussion above and use an array to connect to multiple remote computers: $strComputers = @(“Server1″, “Server2″, “Server3″)

So, which one is correct or what is the differe开发者_运维技巧nce ?


Example 2 uses the array cast syntax which allows a single element, for example, to be treated as an array:

$myList = @("Hello")

Essentially, it allows anything between the parenthesis to be treated as an array including the output from other commands:

$myArray = @(Get-Process Excel)

Alternatively you can just create an array by specifying a comma separated list:

$myArray = "hello", "world", "again"

(The curly brackets are not needed)


You can also attain a single element array by prepending the , operator to a single value:

[PS] C:\>$a = ,"Hello World"

[PS] C:\>$a.gettype()


IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array


[PS] C:\>$a.count

1
0

精彩评论

暂无评论...
验证码 换一张
取 消