© Rossella Apostoli, 123RF.com

© Rossella Apostoli, 123RF.com

The best cmdlets for PowerShell

The Horse's Mouth

Article from ADMIN 14/2013
By
Windows is no longer the system for mouse pushers. In the latest server version, the default installation installs without a GUI, and management via PowerShell is a part of everyday life for Windows administrators.

In PowerShell 3.0, Microsoft lets admins manage, install, or adapt virtually any service on a Windows server via the shell. In this article, I introduce some of the new and interesting commandlets (cmdlets) that can make an admin's life easier. For example, the new Show-Command directive explains the use of other parameters in more detail, and with an Internet connection, you can update the help files in PowerShell using Update-Help.

Core Server with a GUI

For Windows Server 2012, installing as a core server is the recommended Microsoft approach and is selected by default. An important innovation in Windows Server 2012 is the ability to install the graphical interface in the PowerShell later on, which means you can convert a core server into a full-fledged server with a graphical interface, and the installed services will not be affected by the change. To do this, type powershell at the command prompt, and then type

Install-WindowsFeature Server-Gui-Shell

in the PowerShell session. After a few minutes, the server reboots, and you have Windows Server 2012.

If you install a Core Server, the server lacks the binaries for installing the graphical interface. You must either configure an Internet connection for the server so that it can download the required data from Windows Update or specify the folder containing the Windows Server 2012 installation files. To install on a Core Server, you need to run:

Install-WindowsFeature Server-Gui-Mgmt-Infra

You can also connect from a computer on the network using Server Manager. Alternatively, you can use the PowerShell commands:

Import-Module Dism
Enable-WindowsOptionalFeature -online -Featurename ServerCore-FullServer,Server-Gui-Shell,Server-Gui-Mgmt

The following command also installs the graphical interface:

Dism /online /enable-feature /featurename:ServerCore-FullServer /featurename:Server-Gui-Shell /featurename:Server-Gui-Mgmt

Until Windows Server 2008 R2, the binaries for features and server roles were also stored on the server, even if they were not installed. This approach had the advantage of quick feature installation, but the binaries unnecessarily used up storage space. Windows Server 2012 now lets you remove unneeded binaries with the Uninstall-WindowsFeature cmdlet. The process can be reversed using the installation media for Windows Server 2012 at any time. The Install-WindowsFeature cmdlet handles this procedure.

One benefit of this feature is the ability to roll out servers with the use of images. If you remove any unnecessary binaries before creating an image, you can save up to a gigabyte of storage space. If you want to remove a role or a feature completely, use the PowerShell Uninstall-WindowsFeature cmdlet with the -Remove option:

Uninstall-WindowsFeature Server-Gui-Shell -Remove

To install the appropriate role or feature, you will then need to access the installation media for Windows Server 2012.

You can use Server Manager or the PowerShell Install-WindowsFeature for the installation. The -Source option for this cmdlet lets you specify a path to a WIM image. If the server fails to find a WIM image, the installation wizard downloads the required files off the Internet from the Windows Update service.

Customizing Core Server and GUI Server

When configuring the IP settings, you will want to avoid using the netsh command-line tool from Windows Server 2008 R2 and instead use the cmdlets New-NetIPAddress and Get-NetIPConfiguration, as in:

New-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.178.2 -PrefixLength 24 -DefaultGateway 192.168.178.1

You can then enter the DNS servers like this:

Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses 192.168.178.4

Multiple DNS servers are separated by commas. The cmdlet

Set-DnsClientServerAddress -InterfaceIndex 12 -ResetServer

changes the setting to DHCP. Be sure to use the correct index number for each network adapter. This can be obtained using Get-NetIPConfiguration. To join a Windows domain, you need Add-Computer; using Rename-Computer lets you do just that.

Installing Server Roles and Features

Features and roles also can be installed in PowerShell. The Get-WindowsFeature Hyper-V* command, for example, tells you whether the role and management tools are already installed. With Windows Server 2012, you can use -computername to check the installation of remote servers on the network. To install Hyper-V or the Management Tools, you need the Install-WindowsFeature cmdlet (on Windows Server 2008 R2, this was called Add-WindowsFeature).

Using Install-WindowsFeature Hyper-V lets you install the Hyper-V server role, and the option -IncludeManagementTools does pretty much that. If you want the server to reboot automatically after this action, you can add the -Restart option. To install just the Management Tools, type:

Install-WindowsFeature Hyper-V-Tools

Then, you can install the feature using the Add-WindowsFeature <Features> command; for example, you could use

Add-WindowsFeature RSAT-AD-PowerShell, RSAT-AD-AdminCenter

to install the Active Directory Management Tools. These commands work in PowerShell 2.0 on Windows Server 2008 R2 and in the new PowerShell 3.0 for Windows Server 2012.

Besides specifying the role and feature names, you can use an XML control file in PowerShell; this file is created in the last of the Add Roles and Features Wizard windows (Figure 1). To install the same roles and features on a different server, just use PowerShell and specify the XML file (Figure 2).

Install-WindowsFeature -ConfigurationFilePath C:\Data\iis.xml
Figure 1: Saving an XML file in Server Manager.
Figure 2: Installing server roles in PowerShell via an XML file.

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Top PowerShell Cmdlets

    Windows is no longer the system for mouse pushers. In the latest server version, the default installation installs without a GUI, and management via PowerShell is a part of everyday life for Windows administrators.

  • Remotely managing web access servers
    The Windows PowerShell web-based console lets you run PowerShell commands and scripts in your browser.
  • Use PowerShell to manage Exchange Online
    Exchange Online in Office 365 can be managed just like its local counterpart with similar, sometimes identical, PowerShell cmdlets.
  • PowerShell Part 1: Retrieving System Information

    Windows administrators, like Unix or Linux administrators, look for ways to perform their duties with ease and elegance through automation. PowerShell allows administrators to gather information, change Registry parameters, work with Active Directory, manipulate services, look at events, work with Windows Management Instrumentation (WMI), and much more. PowerShell is a Windows administrator’s best friend.

  • Security with PowerShell 5
    Despite the growing exploitation of PowerShell's scripting language by hackers, the tool's security features also offer a line of defense.
comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=