A Windows PowerShell Tutorial
My mission is to two fold. Firstly, to encourage a new generation of script writers to learn PowerShell; secondly I want to provide experienced scripters with a bank of example scripts so that they can convert to PowerShell.
To newbies I want to say: ‘It really will be easy to get started. You will be surprised by how many tasks on your Windows computer benefit from a PowerShell script’. To the experienced scripters I say, ‘I want to build a reference library for that moment when you have forgotten the syntax for a particular PowerShell command’.
Windows PowerShell Tutorial Sections
- PowerShell Examples
- Section Dedicated to PowerShell Techniques
- Section Dedicated to Syntax
- Section for PowerShell Real-life Tasks
- New features of PowerShell v 2.0
- New features of PowerShell v 3.0
- PowerShell 4.0
- PowerShell 5.0 New Features
To help you discover the benefits of PowerShell, I will not only provide real life tasks, but also I will add learning points. One of my greatest joys is when you modify the code in my PowerShell tutorials to fit your situation.
In 2013 Microsoft released PowerShell 4.0. To check which version you have, launch PowerShell and then type these five characters:
$Host
Alternatively, try $PSVersionTable.
Individual PowerShell Tutorials
- PowerShell Introduction
- PowerShell on Windows 8
- PowerShell Dreams
- .NET Framework
- 3 Key Commands
- Add-Content
- Alias
- Active Directory
- Brackets () {} and []
- Cmdlets
- Cmdlets Built-In
- Com Objects
- -Com Shell.Application
- Compare-Object
- Comparison Operators: -eq, -gt
- Compare-Object Registry
- Conditional Operators: -Like -Match – contains
- Drives
- Eventlog
- Export-CSV
- Files – Listing
- Format-Table (FT)
- Format-List (FL)
- Functions Intro
- -f Format Operator
- Get-Acl Permissions
- Get-ChildItem (gci)
- Get-Command
- Get-Credential
- Get-History
- Get-Member
- Get-Date
- Group-Object
- If Statement (also ElseIf)
- Ipconfig – The PowerShell way
- Import-CSV
- Import-Module
- Invoke-Command
- Invoke-Expression
- -Match and -NotMatch
®
- ISE (Integrated Scripting Engine) or GUI
- List of PowerShell Cmdlets in Exchange 2010 Server
- Loops
- New-Item
- Out-File
- Out-GridView
- Pipeline (|)or (¦)s
- Printers
- Process
- Profile.ps1
- Quotes ‘Single’ or “Double”?
- Recurse
- Registry PSDrive
- Remote PowerShell v 2.0
- Replace
- Select-String
- Service
- Set-Acl – Change Permissions
- Set-Content
- Set-ItemProperty in Registry
- Syntax
- Variables
- Where Statement
- WMI Disk
- WMI Objects
- WMI Query
- WMI Select
- WhatIf & confirm
- PowerShell Tips
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Getting Started with Windows PowerShell
One trigger for dabbling with Windows PowerShell is so that you can configure Exchange Server. However, I believe that the pressing reason for learning PowerShell is for issuing WMI commands. When you want to interrogate your Microsoft operating system, three lines of PowerShell will replace twenty lines of VBScript. PowerShell is free, therefore all you need is a spare half hour to experiment with the potent new Verb-Noun pairs at the PowerShell command line.
- Reasons to Learn PowerShell
- PowerShell Installation
- Getting Started – PowerShell Structure
- Abilities of PowerShell
- Summary of Windows PowerShell
Four Reasons to Learn Windows PowerShell
- Think of learning PowerShell as a more convenient method of configuring the server than using the corresponding GUI. I can foresee running PowerShell scripts that will diagnose server problems. With PowerShell you can run queries that are not possible from the GUI. On a good day such a script would even cure the problem – magic!To come back down to earth, let us start with a trivial example to illustrate the capability of PowerShell. As you may know, the Task Manager will display all the processes, yet with PowerShell you can not only list all processes, but also group them by company (Microsoft, HP and Roxio). All you need is this one-liner:
Get-Process | group-Object company - PowerShell will achieve the same results as VBScript, but with fewer commands. For instance, PowerShell makes scripting WMI much easier, for example, try: Get-WmiObject win32_BIOS. More good news, you can even use PowerShell to replace VBScript for Active Directory scripts.
- I have a sense that there is a changing of the guard; out with old CMD commands, and in with new PowerShell language. More importantly, the new PowerShell language will give you command line control of the Windows operating system; this is similar to the power that Bash commands exert over the UNIX operating system. In a nutshell: PowerShell really is the way of the future.
- The main reason for learning PowerShell is so that you can administer Exchange Server, and also the Windows Servers that have succeeded Windows Server 2003. Microsoft’s thinking is that the point has arrived where complex products like Exchange and Windows Server have so many menus and sub-menus to configure via a GUI, that we need an alternative. Hence the easy-to-use scripting language called PowerShell.
Note: Existing Windows executables, such as Ipconfig and ping, perform in the PS Shell just as they do in the old CMD ‘DOS box’.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
PowerShell Tutorial Samples
Preamble, Start, Run PowerShell.
At the command line type (In that dark blue shell):
Get-Service
Here is another example, type:
Clear-Host
Get-WmiObject win32_computersystem
PowerShell Needs Less Code
I first realised the ability of PowerShell when I saw three lines of PowerShell replace about twenty lines of VBScript. The task is to interrogate Win32_classes, we can either use the traditional VBScript and WMI, or the new PowerShell with its Verb-Noun commands.
VBScript | Windows PowerShell |
‘ Memory.vbs WScript.Quit ‘ End of Sample Memory WMI / VBScript | # Windows PowerShell Tutorial script “System Name = ” + $CompSys.name
|
Microsoft refer to this new PowerShell command style as ‘task-based’. As with other scripting language, PowerShell lends itself to automating repetitive tasks, especially as you can store the lines of code in cmdlets scripts to reuse latter. For programmers and developers, PowerShell offers the ability to manipulate the operating system’s objects, it is to gain this ability that you need to first install .Net Framework 2.0 (or 3.0).
PowerShell Installation
Modern operating system have PowerShell built-in, just go to the Control Panel, Programs, ‘Turn Windows feature on’. However, for older OS you need to download PowerShell from Microsoft’s site.
Here are the three stages to get up and running with Windows PowerShell:
1) Download, then install the latest .Net Framework.
2) Get a copy of PowerShell (See here for detailed instructions)
3a) Once you have installed the downloaded executables, press the Windows key +r, this launches the Run command (even in Vista), now type in the dialog box: PowerShell. Visit Microsoft’s Windows PowerShell download center
3b) Better still, if you have PowerShell 2.0 or later, seek out the ISE (Integrated Scripting Engine) version.
Guy Recommends: SolarWinds’ Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the users with their fields in the top row, and save as .csv file. Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.
Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.
If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)
Getting Started – PowerShell Structure
The situation is that you are at the PowerShell prompt, and you want to get started. The basic PowerShell command consists of two-word sentences. Start with a verb, follow with a hyphen and end with a noun. Naturally, you press carriage return when you have finished the command. Try these PowerShell examples:
Get-Process # (Correct)
Get – Process
# (Wrong no spaces required next to the hyphen).
With time, and plenty of practice, you will soon increase your vocabulary. A good place to start is with nouns such as eventlog, process and service. For example:
Get-Eventlog -List.
From the above example, we learn that the hyphen has a second, or complimentary role, namely to act as a modifier, switch or as PowerShell calls it a -parameter. If we study this command structure, the role of a space, or lack of a space, becomes obvious, even intuitive, for example -List tells the command to display all the event logs. However, after Get-Eventlog the code needs a breather, hence a space BEFORE the -List modifier.
# Windows PowerShell Tutorial
Get-Eventlog – list # (Wrong)
Get-Eventlog-List # (Wrong)
Get-Eventlog -List # (Correct)
Perhaps the most useful command for beginners is: Get-Help. Alternatively, you could try plain: help. Incidentally, from this we deduce that the default verb is ‘get’. Test this theory by typing just a noun that PowerShell understands for example:
Process
help
eventlog
In each case ‘Get-‘ is assumed. (Get-Process, Get-Help or Get-Eventlog)
Other common verbs are: set, start, stop, new, add, copy and out (out as in output file).
PowerShell is a potent language, in this section I am just scratching the surface. Once you get going, then you can create more complex scripts by employing the usual scripting structures such as functions, loops and ‘where’ clauses.
Abilities of Microsoft PowerShell
If PowerShell were a car, so far we have only driven in first gear and I have barely touched the accelerator. For now, you will either have to trust me when I say PowerShell has power, depth and flexibility, or else you can research for your self. To give you one example, with PowerShell you can create new objects for example, DirectoryServices.DirectoryEntry or the simpler, system.DateTime. Keep in mind that it is thanks to .Net Framework that PowerShell allows you to build such objects.
Summary of Windows PowerShell
PowerShell is a friendly new scripting language for administering Windows and Exchange servers. In particular, it makes tasks such as interrogating WMI objects easier. Moreover, one day PowerShell will be Microsoft’s standard scripting shell, meanwhile my PowerShell tutorials will make it easier for you to configure Exchange, and also Active Directory in Windows Server. I have prepared about 300 pages of Windows PowerShell tutorials for you to enjoy.
If you like this page then please share it with your friends
See more Windows PowerShell tutorials
• PShell Home • Introduction • Dreams • 3 Key Commands • PowerShell Help About • Get-Help
• PowerShell v 3.0 • Set-ExecutionPolicy • Get-Command • Cmdlet scripts • Import-Module
• PowerShell Version Check • Backtick • PowerShell examples • PowerShell ISE • Get-Member
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.