PowerShell Tutorial 1-8
PowerShell Tutorial 1-8
PowerShell Tutorial
for Beginners
Table of Contents
Introduction 4
1.4 Comments 11
1.5 Pipes 11
2
3. Top 10 File System Management Tasks Using PowerShell 28
Final Word 45
About Netwrix 47
3
Introduction
Automate it. Now, where’s that script…
Warning: PowerShell is addictive.
Windows PowerShell is an object-oriented automation engine and scripting language with an interactive
command-line shell designed to help IT professionals configure systems and automate administrative tasks.
You can find it in every modern Windows OS starting with Windows 2008R2.
Learning Windows PowerShell is like learning to use a universal multi-tool. In this eBook, we’ll walk you
through PowerShell scripting basics, show you PowerShell commands and scripts for performing the most
common administrative tasks, and explain how you can schedule your PowerShell scripts and commands.
4
1. PowerShell Scripting Basics
In this part, we’ll cover PowerShell scripting basics so you can more easily perform virtually any
administration task in your Windows IT environment.
To launch the PowerShell command line, type powershell.exe in the Windows Start menu. You’ll see a
screen like the following:
To launch the PowerShell ISE, type powershell_ise.exe in the Start menu. Using the PowerShell ISE is the
preferred way to work with the scripting language because it provides syntax highlighting, auto-filling of
commands and other automation features that simplify script development and testing.
5
1.2 Preparing to Run PowerShell Scripts
PowerShell scripts are stored in.ps1 files. You cannot run a script by simply double-clicking a file; this design
helps avoid accidental harm to your systems. Instead, to execute a script, right-click it and choose Run with
PowerShell:
In addition, there is a policy that restricts script execution. You can check this policy by running the
Get-ExecutionPolicy command in PowerShell:
Restricted — No scripts are allowed. This is the default setting, so you will see it the first time you run the
command.
AllSigned — You can run scripts signed by a trusted developer. Before executing, a script will ask you to
confirm that you want to run it.
RemoteSigned — You can run your own scripts or scripts signed by a trusted developer.
To start working with PowerShell, you’ll need to change the policy setting from Restricted to RemoteSigned
using the Set-ExecutionPolicy RemoteSigned command:
6
1.3 PowerShell Cmdlets
A cmdlet is a PowerShell command with a predefined function, similar to an operator in a programming
language. Here are some key things to know about cmdlets:
Cmdlets can get data for analysis or transfer data to another cmdlet using pipes (I’ll discuss pipes more in
a moment).
Cmdlets are case-insensitive. For example, it doesn’t matter whether you type Get-ADUser, get-aduser
or gEt-AdUsEr.
If you want to use several cmdlets in one string, you must separate them with a semicolon (;).
A cmdlet always consists of a verb (or a word that functions as a verb) and a noun, separated with a hyphen
(the “verb-noun” rule). For example, some of the verbs include:
New — To create something (“new” is not a verb, of course, but it functions as one)
7
Get-Service — Shows the list of services with their status
Get-Content — Shows the content of the file you specify (for example, Get-Content
C:\Windows\System32\drivers\etc\hosts)
Good news — you don’t need to memorize all cmdlets. You can list all cmdlets by executing the Get Help
-Category cmdlet, which will return the following:
Each cmdlet has several parameters that customize what it does. The PowerShell ISE will automatically
suggest all valid parameters and their types after you type a cmdlet and a hyphen (-):