Netwrix 1Secure delivers unified visibility across data and identity - free for 14 days with full access. Start a free trial

Resource centereBook
Windows PowerShell scripting tutorial

Windows PowerShell scripting tutorial

PowerShell scripting lets you automate repetitive Windows and Active Directory tasks, from pulling reports to bulk-updating accounts. This tutorial starts with the basics: variables, the pipeline, loops, and functions, then builds up to scripts you can run today. Download the full guide for complete walkthroughs of AD account and computer management, file system tasks, and script scheduling, so you have working scripts on hand when you need them.

Download the PowerShell commands scripting tutorial (PDF)

How do you write a PowerShell script?

A PowerShell script is a plain-text file saved with a .ps1 extension. You can write one in any text editor, though most people use Visual Studio Code with the PowerShell extension for syntax highlighting, autocomplete, and integrated debugging. Write your commands one per line, save the file, and run it.

Start small. A two-line script that gets a value and prints it teaches you the core loop every PowerShell user relies on: write, run, adjust. Add a comment with a # at the start of a line to note what a section does, since scripts you read months later rarely explain themselves. This tutorial builds each concept on the last, so a beginner can follow along from start to finish.

What are the basic PowerShell commands?

PowerShell commands, called cmdlets, follow a verb-noun pattern that makes them easy to read and guess: Get-, Set-, New-, and Remove-. A handful of cmdlets cover most day-one tasks:

  • Get-Help: look up syntax and examples for any cmdlet
  • Get-Command: list every cmdlet available in your session
  • Get-Member: see the properties and methods of an object
  • Get-Process: view running processes
  • Get-ChildItem: list files, folders, or registry keys

Type Get-Help <cmdlet name> -Examples any time you need a refresher, since PowerShell's built-in help covers nearly every cmdlet in the box. The cheat sheet groups these by task, so you're not searching mid-script.

How do you run a PowerShell script?

Open PowerShell, navigate to the script's folder, and run it with .\scriptname.ps1. If a message says the script can't be loaded because running scripts is disabled, PowerShell's execution policy is blocking it, not your script.

Execution policy has several levels, from locked down to fully open, and picking the wrong one can either leave you stuck or leave your systems exposed. The guide walks through each level, which one strikes the right balance for day-to-day scripting, and the exact command to set it safely.

How do you use variables, loops, and functions?

Variables store values with a $ prefix, like $name = "Server01". They can hold text, numbers, or entire objects, and PowerShell figures out the type for you.

Loops repeat an action over a collection. A foreach loop is the one you'll reach for most:

      foreach ($user in $users) {
    Write-Host $user.Name
}
      

Functions package reusable logic, so you write it once and call it often:

      function Get-Greeting($name) {
    "Hello, $name"
}
      

Each concept gets a short, runnable example you can paste and adapt.

What can you automate with PowerShell in Active Directory?

PowerShell's ActiveDirectory module, installed with RSAT (Remote Server Administration Tools), adds cmdlets built specifically for directory tasks. Common wins:

Worked examples in this tutorial show the pattern for each, so you can adapt them to your own environment without starting from a blank script.

For teams that would rather not maintain scripts long term, purpose-built tooling covers the same AD reporting and change tracking without custom code.

FAQs

Share on