Forum Discussion

_MoZZa's avatar
_MoZZa
Copper Contributor
Nov 05, 2023

Powershell - replace an array / hash table with a file

Hi All, This should be straigtforward (I think), but i cannot get it to work. I have a working script which reads a csv file from an HR app and then creates the user accounts. Dependant on the use...
  • LainRobertson's avatar
    Nov 06, 2023

    _MoZZa 

     

    Hi, MoZZa.

     

    Logically-speaking, you can refer to $Site2OU as a dictionary, but strictly-speaking, PowerShell uses the HashTable class for the @{} statement.

     

     

    Given you're working with only two columns in the input file, you can easily achieve what you're seeking.

     

    Input file

    "name","ou"
    "Miami","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
    "Miami Beach","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
    "Miami Beach Holiday Homes","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
    "Boston","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
    "Boston Bay","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
    "Bston Bay Holiday Homes","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
    "Mississippi","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
    "Mississippi Water","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
    "Mississippi Water Holiday Homes","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"

     

    Constructing the HashTable

    $h = [ordered] @{};
    Import-Csv -Path D:\Data\Temp\Forum\forum.csv | ForEach-Object { $h.Add($_.name, $_.ou); }

     

    Example output

     

    Cheers,

    Lain

Resources