1

as the title says how can I convert a string collection into a string array in powershell don't know where to start with this as I am new to powershell. Would it be anything like String[])$viewFields.toArray(new String[0])

2 Answers 2

2

You should try type conversion ([object[]]). Here is an example:

$x = New-Object System.Collections.ArrayList # Definitely not an object array
([object[]]$x).GetType() # Object[]
Sign up to request clarification or add additional context in comments.

Comments

0

To create an array out of anything surround with @(...). To create an array of a specific type, System.Array has some factory methods and use [type] to get a Type object. Thus

$theArray = @($viewFields.toArray([array]::CreateInstance([string], 0)))

2 Comments

new String[0]? What is it? I cannot recognize this as a valid PowerShell syntax.
@ForNeVeR Ooops, too much copy and paste :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.