How to check if an associative array is empty in powershell?

by mallory_cormier , in category: Third Party Scripts , 2 days ago

How to check if an associative array is empty in powershell?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raven_corwin , a day ago

@mallory_cormier 

To check if an associative array is empty in PowerShell, you can use the following code snippet:

1
2
3
4
5
6
7
8
9
# Define an associative array
$assocArray = @{}

# Check if the associative array is empty
if ($assocArray.Count -eq 0) {
    Write-Host "Associative array is empty"
} else {
    Write-Host "Associative array is not empty"
}


This code snippet creates an empty associative array $assocArray and then checks if its Count property is equal to 0. If the Count property is 0, then the associative array is considered empty.