PowerCLI One-Liner: Rename Default “datastore1” Datastores in Bulk

1 minute read

The ESXi installer, by default, creates a datastore on the local disk called “datastore1” on all new hosts. When you add a bunch of new hosts to vCenter, it will resolve the duplicate names by appending a sequence number in parenthesis, like so:

datastore1
datastore1 (1)
datastore1 (2)
datastore1 (3)

That’s pretty ugly. In my environments, we rename these and use the hostname of the owning host to make the names unique. Our format is a prefix consisting of z-, the unqualified hostname, and then a suffix _boot. So, if a host is named ny-esxi-001.mycompany.com, the datastore name would be:

z-ny-esxi-001_boot

The prefix ensures that these datastores sort to the bottom of the list, which is nice.

Problem is, if you are adding more than just one or two hosts, it can be time consuming to go rename each datastore individually. Enter PowerCLI!

Get-Datastore -Name datastore1* | %{ $n = 'z-' + (Get-VMHost -Id $_.ExtensionData.Host[0].Key[0]).Name.Split('.')[0] + '_boot';Set-Datastore -Datastore $_ -Name $n }

This could definitely be broken into multiple lines for clarity, but I’m a fan of one-liners. Here’s what’s going on there:

  1. Get an array of datastores with names starting with “datastore1”
  2. For each datastore in that array:
    • Set a variable $n that takes the part of the owning-host’s FQDN before the first dot and attaches the prefix and suffix to it, and
    • Rename the datastore with the Set-Datastore cmdlet