---
title: "PowerShell Tutorial: Mastering Scriptblocks, Arrays and Hashtables"
description: "Learn how to use PowerShell scriptblocks for reusable code, arrays for data collections, and hashtables for key-value pair management in this practical hands-on tutorial with real-world examples."
canonical: "https://adamtheautomator.com/powershell-scriptblocks-tutorial/"
---

# PowerShell Tutorial: Mastering Scriptblocks, Arrays and Hashtables

> Learn how to use PowerShell scriptblocks for reusable code, arrays for data collections, and hashtables for key-value pair management in this practical hands-on tutorial with real-world examples.

Source: https://adamtheautomator.com/powershell-scriptblocks-tutorial/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![PowerShell Tutorial: Mastering Scriptblocks, Arrays and Hashtables](https://adamtheautomator.com/wp-content/uploads/2024/11/image-71.png)

# PowerShell Tutorial: Mastering Scriptblocks, Arrays and Hashtables

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)27 November 20243 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Leveraging Scriptblocks for Reusable Code](#leveraging-scriptblocks-for-reusable-code)
*   [Harnessing the Power of Arrays and Generic Lists](#harnessing-the-power-of-arrays-and-generic-lists)
*   [Working with Arrays](#working-with-arrays)
*   [Managing Elements in Generic Lists](#managing-elements-in-generic-lists)
*   [Streamlining Data Management with Hashtables](#streamlining-data-management-with-hashtables)
*   [Conclusion](#conclusion)

As you dive deeper into PowerShell, you’ll encounter three core components essential for any PowerShell user: scriptblocks, arrays, and hashtables.

Scriptblocks allow you to encapsulate and execute reusable pieces of code; arrays let you organize and manipulate data collections. At the same time, hashtables provide a powerful way to store and access key/value pairs.

Whether you’re a beginner or looking to refine your skills, understanding these concepts will significantly enhance your ability to write effective PowerShell scripts.

If scriptblocks, arrays, and hashtables are part of a larger move into automation development, compare [Educative developer learning paths for interactive software engineering practice](https://educative.pxf.io/c/1454808/1657818/19245) before choosing a paid platform. The best fit is readers who want structured practice with reusable code patterns after this PowerShell walkthrough.

## Leveraging Scriptblocks for Reusable Code

When you run any command or code, that code is called an expression. It’s a finite bit of code that PowerShell executes.

For example, to check if a file exists, you can use the `Test-Path` command:

```
Test-Path -Path C:\file.txt
```

![Checking if a file exists](https://adamtheautomator.com/wp-content/uploads/2024/11/image-72.png)

This command is an expression. If you need to run this command in multiple places in your script, you can wrap it in curly braces to create a scriptblock.

```
$myScriptBlock = { Test-Path -Path C:\file.txt }
```

The scriptblock looks like a regular string.

```
$myScriptBlock
```

![](https://adamtheautomator.com/wp-content/uploads/2024/11/image-74.png)

But when you append an ampersand (`&`), PowerShell runs the code inside the scriptblock.

```
& $myScriptBlock
```

Scriptblocks allow you to store and execute code in a variable as needed, making them versatile and reusable. Scriptblocks are used in various areas in PowerShell and are an essential concept to understand.

![Executing the code inside a scriptblock](https://adamtheautomator.com/wp-content/uploads/2024/11/image-75.png)

## Harnessing the Power of Arrays and Generic Lists

Previously, we worked with single values like strings, numbers, or Boolean values. Let’s now explore collections of objects, starting with arrays.

Arrays serve as a basic data structure in PowerShell, facilitating the management of collections of related items. By using arrays, you can consolidate multiple values into a single variable, enhancing the organization and efficiency of data management.

Consider a color picker script that contains four colors: `blue`, `white`, `yellow`, and `black`.

```
$colorPicker = @('blue','white','yellow','black')
```

#### Working with Arrays

Arrays in PowerShell are indicated by the `@` symbol, with elements separated by commas inside parentheses. You can read and manipulate these elements as a single set.

For instance, read the entire array.

```
$colorPicker
```

![Reading an entire array](https://adamtheautomator.com/wp-content/uploads/2024/11/image-76.png)

To read a specific element, reference its index starting from zero (`0`).

```
$colorPicker[0]
$colorPicker[2]
$colorPicker[3]
```

![Reading specific array elements by index](https://adamtheautomator.com/wp-content/uploads/2024/11/image-77.png)

You can also use the range operator to read a sequence of elements.

```
$colorPicker[1..3]
```

![Reading a sequence of array elements](https://adamtheautomator.com/wp-content/uploads/2024/11/image-78.png)

Elements in an array can be added, removed, or modified, just like a scalar value.

To change the first element (`0`):

```
$colorPicker[0] = 'pink'
$colorPicker
```

![Changing an array element](https://adamtheautomator.com/wp-content/uploads/2024/11/image-79.png)

For adding (`+`) a new element:

```
$colorPicker = $colorPicker + 'orange'
$colorPicker
```

![Adding an array element](https://adamtheautomator.com/wp-content/uploads/2024/11/image-80.png)

Or use a shortcut to add the element (`+=`):

```
$colorPicker += 'brown'
$colorPicker
```

![Adding an array element via the shortcut method](https://adamtheautomator.com/wp-content/uploads/2024/11/image-81.png)

When adding multiple elements at once:

```
$colorPicker += @('pink','cyan')
$colorPicker
```

![Adding multiple array elements at once](https://adamtheautomator.com/wp-content/uploads/2024/11/image-82.png)

## Managing Elements in Generic Lists

Besides arrays, you can use a different type of collection called a list or, more specifically, a generic list.

```
$colorPicker = [System.Collections.Generic.List[string]]@('blue','white','yellow','black')
$colorPicker
```

This code converts the array into a `System.Collections.Generic.List` of strings.

![Converting an array into a generic list of strings](https://adamtheautomator.com/wp-content/uploads/2024/11/image-83.png)

With this list, you can add elements using the `Add()` method.

```
$colorPicker.Add('gray')
$colorPicker
```

![Adding an element from a list via the Add() method](https://adamtheautomator.com/wp-content/uploads/2024/11/image-84.png)

Or remove elements using the `Remove()` method.

```
$colorPicker.Remove('gray')
$colorPicker
```

![Adding an element from a list via the Remove() method](https://adamtheautomator.com/wp-content/uploads/2024/11/image-85.png)

## Streamlining Data Management with Hashtables

Another robust data structure in PowerShell is hashtables. While arrays are excellent for handling lists of items, hashtables offer a more advanced way to store and retrieve data using key/value pairs.

This capability makes hashtables ideal for tasks where you must quickly look up values based on unique identifiers.

Now, let’s explore hashtables as follows:

```
$users = @{
 abertram = 'Adam Bertram';
 raquelcer = 'Raquel Cerillo';
 zheng21 = 'Justin Zheng'
}
$users
```

Unlike arrays, hashtables store a label or key that describes the value.

For example, this hashtable maps usernames to their full names.

![Creating a sample hashtable](https://adamtheautomator.com/wp-content/uploads/2024/11/image-86.png)

You can read elements using dot notation or brackets.

```
$users['abertram']
$users.abertram
```

![Reading elements via dot notation and brackets](https://adamtheautomator.com/wp-content/uploads/2024/11/image-87.png)

Next, to get a list of keys or values:

```
$users.Keys
$users.Values
```

![Reading element keys and values](https://adamtheautomator.com/wp-content/uploads/2024/11/image-88.png)

If you prefer to see items in a nicely formatted style:

```
Select-Object -InputObject $users -Property *
```

![Reading and displaying elements in a nice format](https://adamtheautomator.com/wp-content/uploads/2024/11/image-89.png)

When adding elements to a hashtable, reference the key in brackets and assign a value.

```
$users['phrigo'] = 'Phil Rigo'
$users
```

![Adding elements to a hashtable](https://adamtheautomator.com/wp-content/uploads/2024/11/image-90.png)

To check if a hashtable contains a particular key, use the `ContainsKey()` method.

This method returns `$true` if the key exists or `$false` if not.

```
$users.ContainsKey('johnnyq')
$users.ContainsKey('phrigo')
$users
```

![Verifying a specific key exists in a hashtable](https://adamtheautomator.com/wp-content/uploads/2024/11/image-91.png)

And finally, to remove an element, use the `Remove()` method with the key.

```
$users.Remove('raquelcer')
$users
```

![Removing a specific key from a hashtable](https://adamtheautomator.com/wp-content/uploads/2024/11/image-92.png)

## Conclusion

With an understanding of scriptblocks, arrays, and hashtables, you now have a versatile toolkit for managing data and reusable code in PowerShell. These core concepts make your scripts cleaner and more efficient and lay the foundation for tackling more advanced PowerShell tasks.

Continue to build on these skills, and you’ll find new ways to automate and streamline your workflows in PowerShell!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-scriptblocks-tutorial%2F&text=PowerShell%20Tutorial%3A%20Mastering%20Scriptblocks%2C%20Arrays%20and%20Hashtables)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-scriptblocks-tutorial%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-scriptblocks-tutorial%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
