Learning and utilizing basic commands in PowerShell

Windows Server 2016 introduced full PowerShell support with over 2,300 commandlets, including the Hyper-V module, which comes with 232 commands. It is true that the Graphical User Interface (GUI) is easier than almost all the other options. But PowerShell Version 5 proved that it can be handy in most common scenarios.

This recipe will demonstrate how easy it is to use PowerShell and provide you with some helpful information, with examples, to guide you in using the commands with no advanced knowledge or a lot of effort.

Getting ready

To get ready, you just need to open a PowerShell window as administrator, from the taskbar, and run the command Update-Help to update all the existing help content. To update the Help command, you will need an Internet connection.

How to do it...

The following steps will walk you identifying the existing Hyper-V commands and how to get more information about how to use them:

  1. PowerShell divides the commands by components, such as AppLocker, Server Manager, and Hyper-V. These divisions are called modules. To see the current commands that exist in the Hyper-V Module, you need to use the Get-Command commandlet, specifying the module that you want to see, as shown in the following example. A list of all the 232 commandlets for Hyper-V will show up, as shown in the following screenshot:
    Get-Command –Module Hyper-V 
    
    How to do it...
  2. To find a command using a particular word, such as New, Start, Add, VHD, VM, Switch, and so on, you can use Get-Command with the Name switch. Using the following example as a reference, you can change the term between the asterisks at the end of the command to find the word that you are looking for. In the example, PowerShell shows every command from the Hyper-V module that contains the word vhd:
    Get-Command –Module Hyper-V –Name *vhd*
    
    How to do it...
  3. You can also use the switches Noun and Verb to find a command with a particular noun or verb, as shown in the following examples, to find commands with the noun VM and the verb Start:
    Get-Command -noun VM
    Get-Command –Verb Start
    
  4. Now that you know the existing Hyper-V commands, you can move on to one of the most important commands in Hyper-V: the Help command. By using the Get-Help command, you can understand what you can do with the commandlet, and how to do it. For example, if you want to see what you can do with the New-VM command, you can type the following Help commands, the last one being handy for showing examples of the usages of New-VM:
    Get-Help New-VM
    Get-Help New-VM –Detailed
    Get-Help New-VM –Full
    Get-Help New-VM –Examples
    
  5. By referring to the previous example of New-VM, you can use the following command to create a virtual machine named NewVM, with 512MB, of memory at the path C:Hyper-VVMs:
    New-VM –Name NewVM –MemoryStartupBytes 512MB –path C:Hyper-VVMs
    
  6. One of the problems with these commands is trying to remember the switches that can be used. That's why, in this new version of PowerShell, you can type the command and, when executed, it can continue processing your command even if you forget a switch. To make it simple, the next example shows a VM being created in three different ways, but with almost the same results—a new VM called NewVM with 512MB of memory:
    New-VM –Name NewVM –MemoryStartupBytes 512MB –path C:Hyper-VVMs
    New-VM –Name NewVM
    New-VM
    
  7. If you don't think this is easy, you can use a GUI interface to guide you to type a command. To use the GUI window, type Show-Command.
  8. In the Commands window, select the Hyper-V module in the first drop-down list followed by the command that you want to use. The following screenshot shows an example of using the New-VM command:
    How to do it...
  9. Use the other tabs, such as Existing VHD and New VHD, as shown in the previous screenshot, to insert more options for your new VM. These tabs will be different based on the command you choose. To see advanced options, click on Common Parameters in the Commands window.
  10. Now you can use the previous task from this recipe to see how to use the other commands on PowerShell.

How it works...

PowerShell offers different commands, each one with lots of switches and options that are difficult to remember. The purpose of this recipe is to show how simple it can be knowing more about the commands in PowerShell by using some commands. We used Get-Command to find other commands, the helpful Get-Help command using the Examples switch to show some nice examples, and the GUI provided by Show-Command to help IT admins who don't like the command-line interface.

After working through these steps, you can identify and learn how to use the Hyper-V commands on PowerShell.

There's more...

Keeping the previous example in mind, you can create some new virtual machines with the New-VM command, and then you can explore some other advanced options using loops and variables to create lots of VMs in just one command line. In the following command, there is a loop from one to ten, which will create ten virtual machines, with the name starting with NewVM followed by the loop number:

1..10 | % { New-VM -Name "NewVM$_" }

The following screenshot shows the 10 virtual machines that were created after running the command line:

There's more...

See also

  • The Using PowerShell commands for daily tasks recipe in this chapter
  • The Enabling and working with remote connection and administration through PowerShell recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.6.75