Powershell – Check and Install roles and features

If you are building Windows Servers on a recurring basis you will want to start scripting parts of or all of the process. Installing Roles and Features is one of those parts.

The below example covers installing the Hyper-V role via Powershell.

Notes

  1. Extra details on the Install-WindowsFeature  commandlet:
    1. -IncludeManagementTools = Includes the management tools that come with the role
    2. -Restart = Restarts the machine once the role is installed

Code description

  1. Checks the status of the Hyper-V role on the machine
  2. If the role is not installed it will install it and reboot the machine
  3. If the role is installed it will write out to the console

Code


#Install Hyper-V
#Check if Hyper-V is already installed
$Check = Get-WindowsFeature -Name Hyper-V
If ($check.Installed -ne "True") {
write-host "Hyper-V Installing and rebooting afterwards..."
Install-WindowsFeature –Name Hyper-V -IncludeManagementTools -Restart
}

Else {
write-host "The Hyper-V role is installed already."
}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s