Modifying hashtables

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.

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s