I have a powershell script that generates an ArrayList based on the names of the folders in a given directory. So if the directory looked like the following:
C:\Directory\One
C:\Directory\Two
C:\Directory\Three
$list is an array that contains [One, Two, Three]. The line is as follows:
[System.Collections.ArrayList]$list = (Get-ChildItem C:\Directory -dir).Name
I can then iterate through the array of strings and do various things with them. However this stops working when there is only one folder in the directory. It seems it is no longer an ArrayList and becomes just a string. I get the following error:
Cannot convert the "Directory" value of type "System.String" to type "System.Collections.ArrayList"
What can be done here? Should I be using a different kind of array type?
You need to force the output into an array.
@((Get-ChildItem C:\Directory -dir).Name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With