PowerShell for the beginner, for Hyper-V & Windows Server

“PowerShell, i’ve heard about it”

Without doubt you’ve heard about PowerShell. As the name implies; It’s a powerful framework created by Microsoft. Repetitive jobs can be automated making is less tedious and a lot less prone to human errors.
Unfortunately not everyone who could and perhaps should use PowerShell, actually works it. Usually I hear a comment such as “I know it’s unavoidable. I just don’t get it”, or “Yeah, I really should start working with it. But I never actually had time for it”. Because more and more companies start implementing Windows Server 2016 and PowerShell. This would be the right time to start with the basics and get acquainted. Trust me, you’ll learn to love it!

The first steps

When you take your first steps with PowerShell, I’d like to advise you to start out with Windows PowerShell Integrated Scripting Environment, or ISE. To start the Windows PowerShell ISE you can either click start, scroll down to Windows PowerShell and click PowerShell ISE or right-click Start, click Run and in the Run box, type, powershell_ise.exe.
This opens up a window where you can start scripting in the top pane, or execute commands directly in the blue pane at the bottom. The pane on the right hand side has an overview of all available commands, easily filtered by module name.

Windows PowerShell ISE

PowerShell and the PowerShell ISE both have Tab-completion of cmdlets. So don’t worry that you don’t know commands from the top of your head.
To get you started immediately start with your first PowerShell cmdlet Update-Help. This will download all new and updates help files to help you on your journey with PowerShell.
Most of the PowerShell cmdlets start with either Get- , Set-, New- or Remove- followed by that what we want to do or see.
For instance; if we want to see all the items in our current directory, just like the good old DIR command. We just type Get-ChildItem and a list of all files in the current directory will appear on your screen.

Things will get a bit more interesting at start if you can combine some hands on work with PowerShell and perhaps Hyper-V.

In the ISE or Powershell itself, run Get-VM to get an overview of all VM’s currently running on your Hyper-V Server. You can run this command from your own PC, but to make things easy (we are just beginning after all) make sure that you log onto your Hyper-V server with RDP or a tool of your preference.

Powershell Get-VM

And then?

Now things can become a bit more interesting, since we can do things with Virtual Machines like powering it on and off and perphaps create a checkpoint. All by using PowerShell.
To start a particular virtual machine, run the following command with name of the virtual machine:

Start-VM -Name <VM NAME>
This powers on a VM, if its name is is exactly like the name we provided. Powering on a lot of VMs like this is actually even more tedious than clicking on a VM and press Power ON and we are starting out with PowerShell to automate, are we not? Then let do so, but before we start doing so. Let me quickly explain 1 more important bit of PowerShell; the | or Pipe, as we call it. Using the pipe in PowerShell means we are going to use the result and do something else with it.

How does this work?
We’re going to call a Get-VM cmdlet, but instead of having to type a name, we’re going to pipe the results, check if a VM is powered off and start the VM if that’s the case:

Get-VM | where {$_.State -eq 'Off'} | Start-VM
On the other way around we can power off all running VMs

Get-VM | where {$_.State -eq 'Running'} | Stop-VM
Using these cmdlets you can easily start dozens of VMs within seconds without breaking a sweat!


Finally, to round things up, we can create a checkpoint using PowerShell. We select the virtual machine using the Get-VM command and pipe this to the Checkpoint-VM cmdlet and finally give the checkpoint a name using -SnapshotName:

Get-VM -Name <VM Name> | Checkpoint-VM -SnapshotName <name for snapshot>

There we go, you just started with PowerShell, no more excuse! Enjoy what PowerShell can do for you and until next time!


Just in case you now wonder how to get your hand on the Hyper-V module for Windows:
In your Config Panel, open Add- Remove Programs. Go to “Turn Windows Features on or off” and from there enable all Hyper-V Management Tools.

By | 2017-07-31T19:27:54+00:00 July 31st, 2017|Blog, Tutorial, Veeam|0 Comments