question

Mike-6205 avatar image
0 Votes"
Mike-6205 asked Mike-6205 edited

How to find hash key from an array, and update the array based on the key of the hash using powershell?

I really have been research and fell short on where to start on this.

My objective is to find the value in my hashtable from an existing array. and if it existis, wanted to updated the $array. really stuck and need some help to give me some example that I can learn upon.

populating my hash from csv.
$hash = @{ }
import-csv data.csv | foreach {hash.add($_tSamaccountname,gSamaccountname)
this imports a name mike and key michael

then i have an $array that contains mike,mark.
how do I loop thru this array looking if the value is in my $hash, then update the array from the key.

So my $array should now be michael,mark.


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

Replace line #1 in the code below to load the hash with your CSV.

 $x= [PSCustomObject]@{tSamaccountname='mike';gSamaccountname='michael'}, [PSCustomObject]@{tSamaccountname='mark';gSamaccountname='marcus'}
 [System.Collections.ArrayList]$a = @('mike','mark')
 $h = @{}
 $x |
     ForEach-Object{
         $h.($_.tSamaccountname) = $_.gSamaccountname
     }
 for ($i =0; $i -lt $a.count; $i++){
         if ($h.ContainsKey($a[$i])){
             $a.item($i) = $h.($a[$i])
         }
 }
 $a
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks. the code replaces it as it should. But when I try to import a CSV for $x and $a, it didn't seem to work the way it should

0 Votes 0 ·

Remove line #1.
Replace line #4 with "Import-CSV -Path c:\dir\file.csv | "

I don't know how you populate the array "$a" -- all you said was "then i have an $array that contains mike,mark." There's no mention of the data source.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered Mike-6205 edited

Hi,

If the tSamaccountname is mike and the gSamaccountname is michael, you can update the array like this

 $hash = @{ }
 import-csv d:\temp\data.csv | foreach {
 $hash.add($_.tSamaccountname,$_.gSamaccountname)
 }
 $array = 'mike','mark'
 for($i=0; $i -lt $array.count; $i++){
    if($hash.($array[$i])){
         $array[$i] = $hash.($array[$i])
     }
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

by bad Ian, i was not importing the the csv properly.
after doing this
$a = @()
Import-Csv G:\array.csv | ForEach-Object {$a += $_.samaccountname}

was able to execute the code as it is. many thanks.

0 Votes 0 ·

hi Ian,

what should be the code like if the i need to replace a value from another hashtable like this.

name:mike
age:30
SamaccountName:umike

Name:mark
age:31
SamaccountName:uMark

0 Votes 0 ·