Windows PowerShell Scripting Tutorial for Beginners


windows powershell scripting tutorial

Learn how to master Windows PowerShell scripting to automate repetitive tasks, manage systems efficiently, and streamline your workflow on Windows 11. This beginner-friendly tutorial shows the fundamentals, from writing your first script to using advanced commands safely.

Windows PowerShell Scripting Tutorial

What Is PowerShell?

PowerShell is a powerful command-line shell and scripting language developed by Microsoft. It helps administrators and power users automate tasks such as user management, software installation, and file operations. It combines the flexibility of modern scripting with rich system access.

For related commands and syntax, see our guide on essential shell commands in Windows.

Why Learn PowerShell Scripting?

  • Automate repetitive Windows tasks
  • Manage local and remote systems
  • Control Windows services, processes, and files
  • Create scripts that simplify IT administration

Once you understand the basics, you can use PowerShell to manage both local and networked environments.

How to Create Your First PowerShell Script

1. Open PowerShell

  1. Press Windows + S and type PowerShell.
  2. Right-click Windows PowerShell and select Run as Administrator.

If you need a detailed walkthrough, follow this step-by-step tutorial on how to run Windows PowerShell as an admin.

2. Create a New Script File

  1. Open Notepad.
  2. Type a simple command:
Write-Host "Hello, PowerShell!"
  1. Save the file with a .ps1 extension, for example: first_script.ps1.

3. Change the Script Execution Policy

By default, PowerShell blocks scripts from running. To enable them:

  1. In PowerShell, type:
Set-ExecutionPolicy RemoteSigned
  1. Press Y and hit Enter to confirm.

4. Run Your Script

  1. Navigate to the folder containing your script:
cd "C:\Users\YourName\Desktop"
  1. Execute it:
.\first_script.ps1

You should now see the message Hello, PowerShell! appear in the terminal.

How to Use Variables and Loops in PowerShell

  1. Declare and print a variable:
  1. Repeat actions with a loop:
$name = "John"
Write-Host "Hello, $name!"
for ($i = 1; $i -le 5; $i++) {
    Write-Host "Count: $i"
}

How to Manage Files and Folders with PowerShell

  1. Create a new folder:
New-Item -ItemType Directory -Path "C:\TestFolder"
  1. Create a new file:
New-Item -ItemType File -Path "C:\TestFolder\info.txt"
  1. Copy or move files:
Copy-Item "C:\TestFolder\info.txt" "C:\Backup\"
Move-Item "C:\TestFolder\info.txt" "C:\Archive\"
  1. Delete files or folders:
Remove-Item "C:\TestFolder" -Recurse

Chain these cmdlets in scripts to automate multi-step file operations.

How to Automate Tasks with PowerShell Scripts

1. Schedule Scripts with Task Scheduler

  1. Open Task Scheduler from the Start menu.
    task scheduler in windows 11
  2. Select Create Basic Task.
  3. Enter a name and choose a trigger time.
  4. Under Action, choose Start a Program.
  5. In Program/script, enter: powershell.exe and add -File "C:\Scripts\backup.ps1" in Arguments.
  6. Save and finish.

2. Use Parameters for Flexibility

  1. Add parameters at the top of your script:
param([string]$FolderPath)
Write-Host "You selected: $FolderPath"
  1. Run the script with a value:
.\myscript.ps1 -FolderPath "C:\Data"

How to Debug PowerShell Scripts

  1. Use Write-Host to print variable values during execution.
  2. Add breakpoints in PowerShell ISE or VS Code PowerShell extension.
  3. Step through code with F10 and F11 to inspect behavior.

FAQs

What is the difference between PowerShell and Command Prompt?

PowerShell supports rich automation through cmdlets and scripting, while Command Prompt focuses on simpler commands.

Can I use PowerShell on Windows 11?

Yes. PowerShell is included by default on Windows 11. You can also install PowerShell 7 for cross-platform features.

Is PowerShell difficult to learn?

No. With basic scripting logic and regular practice, you can progress quickly.

Where can I find examples of PowerShell scripts?

Browse the official PowerShell Gallery and community repositories on GitHub for ready-made scripts you can adapt.

Final Thoughts

PowerShell scripting is a valuable skill for managing Windows systems. Start with simple scripts, explore built-in cmdlets, and gradually automate complex administrative tasks. If you want to track system changes or updates using PowerShell, see how to view Windows Update history with PowerShell. Once you are comfortable with the basics, explore advanced features like remoting, error handling, and module creation.

More about the topics: Microsoft PowerShell

Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more

User forum

0 messages