Deploy VMs using Templates, customisation specs and POWERCLI

Who doesn’t love a bit of automation these days, I certainly like to use code snippets and shortcuts whenever I can.  Recently I’ve been deploying some VMs in my homelab and thought I would share some of the POWERCLI I use.

I have a Windows 2016 template stored in vCenter that I have recently patched and tweaked to my satisfaction and deploy that with a temporary customization spec to deploy and do the initial config (IP settings and domain join).

The key commands to use here to get the IP settings are



Only started playing with these recently and they are very useful.


## Deploy New VM
## CT April 2019

## Define Variables

$VCCred = Get-Credential # vCenterCred
$DomCred = Get-Credential # Cred for adding VM to domain

$VC = "" # vCenter
$VCTemplate = "Windows Server 2016 Template" # name of existing VM Template
$VMname = "NewVM" # New VM name
$Domain = "" # Domain
$LocalAdmin = "Administrator" # Local Admin name of VM
$LocalAdminPW = "D3pl0yMyVMBaby!" # Local Admin pw
$OrgName = "OrgName" # Organisation
$VMIP = "" #Desired IP of server
$VMSubnetMask = "" #VM subnetmask
$VMDefaultGW = "" #VM Default GW
$DNSServer1 = "" #DNS Server1
$DNSServer2 = "" #DNS Server2
$VMHost = "" #Target Host
$Datastore = "vsandatastore" #Target Datastore

## Connect to vCenter

Connect-VIServer $VC -Credential $VCCred

## Create Customisation

$CustSpec = New-OSCustomizationSpec -OSType "Windows"`
-Name "TempSpec" -Type NonPersistent `
-NamingScheme vm `
-Domain $Domain -DomainCredentials $DomCred `
-FullName $LocalAdmin -AdminPassword $LocalAdminPW `
-AutoLogonCount 1 `
-OrgName $OrgName -Description "Temp Custom Spec for VM deployment" `
-TimeZone 085 -ChangeSid `
-ErrorAction Stop

## Set Network Properties

$IP =  @{OScustomizationNicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec $CustSpec}
$IP.IPMode = "UseStaticIP"
$IP.IPAddress = $VMIP
$IP.SubnetMask = $VMSubnetMask
$IP.DefaultGateway = $VMDefaultGW
$IP.dns = $DNSServer1,$DNSServer2

Set-OSCustomizationNicMapping @IP

## Deploy VM

$NewVM = New-VM -Name $VMname -Template $VCTemplate `
            -VMHost $VMHost `
            -Datastore $Datastore `
            -OSCustomizationSpec $CustSpec `
            -ErrorAction Stop 

## Power UP VM to complete customisation

Start-VM $VMname


You may also like...

1 Response

  1. December 18, 2020

    […] discussed previously how I like to handle deploying appliances and VMs using PowerCLI and an existing VM template but I want to take that a stage further and create a template using […]

Leave a Reply

Your email address will not be published. Required fields are marked *