How to use PowerShell and PowerShell cmdlets

by

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 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.

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 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.

Windows Server 2012 R2, add roles and features wizard
Figure 1. Windows Server 2012 R2, “add roles and features” wizard

There are several reasons to consider using the PowerShell command line interface, but here are my top three

  1. 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 command so it’s done automatically?
  2. 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.
  3. 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 cmdlet would you use?” and so on.

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.

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_ise or ise in the command line or by launching the Windows PowerShell ISE tool in the Server Manager.

Windows PowerShell ISE interface
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!

Get 30 days trial of Veeam Backup & Replication now

About the author
VeeamVeeam Logo
Andrew Zhelezko is a Veeam Technical Product Analyst who gained a strong understanding of Veeam products by working initially in Veeam technical support. This practical experience has helped him speak the same language as Veeam community members. His goal is to help others realize the beauty and power of virtualization. Follow Andrew on Spiceworks.