Search code examples
powershellpowershell-5.0

Export hashtable to csv from keys to value


Ok, I'm struggling with this one. I need to export this hash table to a csv.

$Sku = @{
        "AAD_BASIC"                              = "Azure Active Directory Basic"
        "RMS_S_ENTERPRISE"                       = "Azure Active Directory Rights Management"
        "AAD_PREMIUM"                            = "Azure Active Directory Premium P1"
        "AAD_PREMIUM_P2"                         = "Azure Active Directory Premium P2"
        "MFA_PREMIUM"                            = "Azure Multi-Factor Authentication"
        "RIGHTSMANAGEMENT"                       = "Azure Information Protcetion Plan 1"
        "O365_BUSINESS_ESSENTIALS"               = "Office 365 Business Essentials"
        "O365_BUSINESS_PREMIUM"                  = "Office 365 Business Premium"
        "ADALLOM_O365"                           = "Office 365 Cloud App Security"
        "ADALLOM_S_DISCOVERY"                    = "Unknown"

The above is a sample of the hash table You can find the site here: https://github.com/directorcia/Office365/blob/master/o365-skus.ps1

When I export it turns into a mess. I tried to convert it to a pscustom object. Because of how the table is setup, I have to call the basic . item. Is there a way to reference the .item instead of calling out each item. I've tried .name and .value, neither of them has worked.


Solution

  • A hash table can easily be converted into a PSCustomObject directly. PSCustomObject can be directly exported/converted to CSV.

    $Sku = @{
            "AAD_BASIC"                              = "Azure Active Directory Basic"
            "RMS_S_ENTERPRISE"                       = "Azure Active Directory Rights Management"
            "AAD_PREMIUM"                            = "Azure Active Directory Premium P1"
            "AAD_PREMIUM_P2"                         = "Azure Active Directory Premium P2"
            "MFA_PREMIUM"                            = "Azure Multi-Factor Authentication"
            "RIGHTSMANAGEMENT"                       = "Azure Information Protcetion Plan 1"
            "O365_BUSINESS_ESSENTIALS"               = "Office 365 Business Essentials"
            "O365_BUSINESS_PREMIUM"                  = "Office 365 Business Premium"
            "ADALLOM_O365"                           = "Office 365 Cloud App Security"
            "ADALLOM_S_DISCOVERY"                    = "Unknown"
    }
    
    [PSCustomObject]$sku | ConvertTo-Csv -NoTypeInformation
    

    EDIT

    As of powershell v7.2.0-preview.9, you can pass the hash table directly to the CSV cmdlets

    $Sku = @{
            "AAD_BASIC"                              = "Azure Active Directory Basic"
            "RMS_S_ENTERPRISE"                       = "Azure Active Directory Rights Management"
            "AAD_PREMIUM"                            = "Azure Active Directory Premium P1"
            "AAD_PREMIUM_P2"                         = "Azure Active Directory Premium P2"
            "MFA_PREMIUM"                            = "Azure Multi-Factor Authentication"
            "RIGHTSMANAGEMENT"                       = "Azure Information Protcetion Plan 1"
            "O365_BUSINESS_ESSENTIALS"               = "Office 365 Business Essentials"
            "O365_BUSINESS_PREMIUM"                  = "Office 365 Business Premium"
            "ADALLOM_O365"                           = "Office 365 Cloud App Security"
            "ADALLOM_S_DISCOVERY"                    = "Unknown"
    }
    
    $sku | ConvertTo-Csv
    

    Also note, -NoTypeInformation is now the default, it's only needed in Windows Powershell.