How do I create an ArrayList from array in PowerShell?

I have a list of files in the array I want to enumerate these files and delete specific files from them Obviously, I can't delete items from the array, so I want to use an ArrayList

$temp = Get-ResourceFiles
$resourceFiles = New-Object System.Collections.ArrayList($temp)

Where $temp is an array

How can I achieve it?

Solution

I can't get the constructor either However, this seems to be effective:

# $temp = Get-ResourceFiles
$resourceFiles = New-Object System.Collections.ArrayList($null)
$resourceFiles.AddRange($temp)

You can also pass an integer in the constructor to set the initial capacity

What do you mean when you want to list these documents? Why can't you filter the desired values into a new array?

Edit:

It looks like you can use this array constructor:

$resourceFiles = New-Object System.Collections.ArrayList(,$someArray)

Note the comma I believe what is happening is when you call Net method, you always pass parameters as an array PowerShell unpacks the array and passes it to the method as a separate parameter In this case, we don't want PowerShell to decompress the array; We want to pass the array as a unit Now, the comma operator creates an array Therefore, PowerShell unpacks the array, and then creates the array with comma operator I think this is what happened

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>