PowerShell has come a long way from a command line interpreter with support of a few commands, to a fully integrated part of every modern Windows OS. Right now, not only is it a cool thing to, but it is also essential in order to achieve better results in Windows tasks’ management, including virtual machine (VM) backup.
This is a two-part article. In the first part, I’m going to talk about PowerShell in general, when you should consider using it for Hyper-V backup and how to get acquainted with PowerShell basics, PowerShell commands (cmdlets) and syntax. The second part contains practical examples of using PowerShell scripts with Veeam snap-in for Veeam Backup Free Edition to perform simple and more advanced operations of Microsoft Hyper-V VM backup and restore. Now, let’s get down to business.
What is PowerShell?
PowerShell, the object-oriented programming language and interactive command line shell for Microsoft Windows, is a great tool for automating Microsoft Windows system tasks. Besides that, it can be used for management of third-party applications, if that is envisioned by their developers. Examples of those applications are Microsoft SharePoint, VMware vSphere, IBM MQ and others, including Veeam Backup & Replication and Veeam Backup Free Edition.
PowerShell lets administrators perform administrative tasks on both local and remote Windows systems. Those tasks are performed by using PowerShell cmdlets (command-lets), which are special simple commands, consist of two words — a verb and a noun — executing a simple function. For example, “Get-Help” cmdlet displays help about Windows PowerShell cmdlets and concepts. Cmdlets can be grouped and used for writing advanced scripts and even executable applications.
The first version of this framework became available in 2006 as an optional Windows Server 2003 (or Windows XP SP2) component and became completely integrated into all later OS releases. Currently, Windows Server 2012 R2 and Windows 8.1 come with PowerShell 4.0, and Windows Server 2016 Technical Preview and Windows 10 come with PowerShell 5.0.
Figure 1. Windows Server 2012 R2, “add roles and features” wizard
Reasons to consider PowerShell
There are several reasons to consider using the PowerShell command line interface, but here are my top three
- Make routine tasks less routine (task automating) — Nobody likes to perform routine tasks manually. When there is a pattern to such tasks, isn’t it a lot less monotonous and more efficient to save that pattern somewhere so the next time you can just run a simple PowerShell command so it’s done automatically?
- Make huge tasks smaller — In addition to the point above, facing huge tasks can sometimes feel downright painful! Huge tasks can be much simpler, however, when they are divided into small achievable steps. Creating a few scripts will help you spend less energy on huge tasks.
- Customize the experience — Yes, sometimes software becomes less flexible for tuning from GUI (default settings, orientation for majority, etc.) and unleashing a product with scripts results in better experience for administrators with specific tastes.
Besides that, you won’t be able to obtain any Microsoft certification without some PowerShell knowledge because all exams contain practical questions like “which PowerShell commands would you use?” and so on.
PowerShell interface and key features
Run an old-fashioned command line (cmd.exe), type powershell and execute. Or, you can hit the PowerShell icon on the taskbar. Either way, you’ll get a ready-to-use Windows PowerShell console.
Figure 2. Windows PowerShell console
Use “Get-Help” cmdlet from before as a starting point for your journey. Combine it with any desired term, you’re up to and you’ll get more information about this area. For example, “Get-Help update” will get you all cmdlets containing “update” action. “Get-Help about_update” will get you all Help pages about update operations. And “Get-Help Update-Disk” will show an extended synopsis, syntax and description of “Update-Disk” cmdlet.
Another important cmdlet is Get-Command. It displays a list of all available PowerShell cmdlets and modules they belong. For example, “Get-Command –Module Hyper-V” lists all Hyper-V cmdlets.
NOTE: It’s more convenient to use scripting editors like Windows PowerShell ISE. You can run it by typing powershell_iseor ise in the command line or by launching the Windows PowerShell ISE tool in the Server Manager.
Figure 3. Windows PowerShell ISE interface
A couple things you should know about PowerShell itself:
- Commands — Adding PowerShell scripting commands is simple. Choose an action or verb, such as get, copy, move, find, select, or remove, and complement it with an object or noun, such as Credentials or Host
- Pipeline, or |— This is a special attribute to use when the output of one cmdlet is to be taken as the input to another cmdlet. The idea is similar to Unix-like systems.
- *.ps1 — This is an extension of PowerShell script.
- Scripting — PowerShell supports variables, functions and branching (if-then-else). It can operate with loops (while, do, for, and foreach), structured error and exception handling, closures and lambda expressions, and integration with .NET variables
- Variables — PowerShell provides special variables that have names, starting with $. They can be assigned with any value, including the output of previously executed cmdlets
If that was a little bit fast, you might want to check our webinar series, where we spend more time about getting started with PowerShell. If you’re okay with the pace, let’s go to the second part of the article to find out how PowerShell can help us with Hyper-V VM backup and recovery tasks. Stay tuned!