PowerShell for Windows Admins

May 6 2019   1:02PM GMT

Modifying hashtables

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

Saw a question about modifying hashtables. The suggested code seemed like overkill.

The starting point is this hashtable:

$record = @{Name=’Joe’; Date=’2019-02-01′; Status=’Pending’}

The updated data is:
$update = @{Name=’Joe’; Date=’2019-04-01′; Status=’Hired’; City=’York’; Country=’England’}

You need to keep the Name and Date from $record. Modify the Status to be that of $update and add the City and County information to record.

Changing status is a simple assignment:

$record.Status = $update.Status

Adding the City and County information is done by adding them to the original hashtable.

$record += @{City=$update.City; Country=$update.Country}

You can check the results by displaying the hashtable.

Technically, you’re creating a new hashtable, adding the information from $record and then the City and Country information but as it stays in the original variable its always referred to as modifying.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: