Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting Object to String Array Powershell

Tags:

powershell

I want to create an array of strings instead of a variable object so that I can use the "contains" keyword on each index of the array.

$myArray = Get-ADDomain

The above creates an object, which is not what I want. I also tried

[string[]] $myArray = Get-ADDomain

But after that, $myArray only contains one string and it is the first non-empty property of Get-ADDomain, in my case "ComputersContainer". What should I do to receive an array of strings where each string is a different property, such as

$myArray[0] = "AllowedDNSSuffixes = {}"
like image 711
Colebacha2 Avatar asked Oct 27 '25 16:10

Colebacha2


2 Answers

You cannot cast a PSObject directly to a string array like that. However, this can be accomplished rather easily.

To get an array of string from the object

$myArray = Get-ADDomain
# You can use a standard array @() but these tends to be slower for bigger amount of data
$outArray = New-Object -TypeName System.Collections.Generic.List[String]

#To add just the value
$myArray.psobject.properties | Foreach { $outArray.Add($_.Value) }

# To add Name = {Value} instead
$myArray.psobject.properties | Foreach { $outArray.Add("$($_.Name) = {$($_.Value)}") }

Using an hasthable instead:

$myArray = Get-ADDomain
$hashtable = @{}
$myArray.psobject.properties | Foreach { $hashtable[$_.Name] = $_.Value }
# If you need to do something with the key
Foreach ($key in $hashtable.Keys) {
    $Value = $hashtable[$key]
    if ($value -like '*prod*') {
        Write-Host $key
    }
}
like image 110
Sage Pourpre Avatar answered Oct 30 '25 07:10

Sage Pourpre


PowerShell will always return objects by design of course, and specifying that [string[]], does not really change that.

For what you are trying to use, you have to force the array creation. The below is just one way, but I am sure others will have more elegant ways of doing this as well. Though I am curious why one would want to do this, this way. But, hey, that's just me.

# Create an empty array
$DomainData = @()

# Get all the data points for the utilized cmdlet, split on a common delimiter for the array
[string[]]$DomainData = (Get-ADDomain | Select *) -split ';'

# Display the array count 
$DomainData.Count
34

# validate getting a value from the array by using an index number
$Item = $DomainData[17]
NetBIOSName=CONTOSO

[array]::IndexOf($DomainData, $Item)
17

# Use that element number to validate the use of the contains comparison operator
0..($DomainData.Count - 1) | %{ If($DomainData[$_] -contains $item){"Index key is $_ contains a value of $Item"} }
Index key is 17 contains a value of  NetBIOSName=CONTOSO

# Use the previous with a partial string for a comparison, -contains cannot be used, like or match has to be used
# From the documentation:
# -Contains
# Description: Containment operator. Tells whether a collection of reference values includes a single test value.


$Item = '*domain*'
0..($DomainData.Count - 1) | %{ If($DomainData[$_] -like $item){"Index key is $_ like a value of $Item"} }
Index key is 1 like a value of *domain*
Index key is 6 like a value of *domain*
Index key is 7 like a value of *domain*
Index key is 8 like a value of *domain*
Index key is 18 like a value of *domain*
Index key is 20 like a value of *domain*
like image 27
postanote Avatar answered Oct 30 '25 07:10

postanote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!