Rename-Drive : Renaming Drives with PowerShell & WMI

PowerShell Team

.CmdletName { font-size:large } .CmdletSynopsis { font-size:medium } .CmdletDescription { font-size:medium } .CmdletParameters { font-size:medium } th { font-size: medium; font-style: italic } table { border: 1 }

Shane Burton from Compellent just asked me to help him rename a drive with PowerShell. It’s possible to rename drives using a couple of different WMI classes, Win32_Volume, and Win32_LogicalDisk. You change the drive by setting a property name on the WMI Object (Label in Win32_Volume, and VolumeName in Win32_LogicalDisk), and then putting it back in the WMI repository with the Put() method. @”, @” WMI and PowerShell can do a lot of cool things together. If you want a quick way to search what you can do with WMI in PowerShell, check out an earlier blog post of mine: Get-WmiHelp / Search-WMIHelp.

Here’s my Rename-Drive function:

Rename-Drive

Synopsis:

Renames a drive with WMI

Syntax:

Rename-Drive [[-deviceID] [<Object>]] [[-name] [<Object>]] [<CommonParameters>]

Detailed Description:

Uses the Win32_LogicalDisk class to rename a drive using WMI. Must be running as administrator to use

Examples:

    -------------------------- EXAMPLE 1 --------------------------
# Sets the name of C: to FooBar
Rename-Drive C: FooBar
    
    -------------------------- EXAMPLE 2 --------------------------
# Resets the name of C:
Rename-Drive C:
    

Here’s Rename-Drive:

function Rename-Drive {
    #.Synopsis
    #    Renames a drive with WMI
    #.Description
    #    Uses the Win32_LogicalDisk class to rename a drive using WMI.  Must be running as administrator to use
    #.Example
    #    # Sets the name of C: to FooBar
    #    Rename-Drive C: FooBar
    #.Example
    #    # Resets the name of C:
    #    Rename-Drive C:
    param($deviceID = "C:", $name)
$drive = Get-WmiObject "Win32_LogicalDisk WHERE DeviceID='$deviceID'"
    if (-not $drive) { return }
    $drive.VolumeName = $name
    $null = $drive.Put()
}
    

Automatically generated with Write-CommandBlogPost

0 comments

Comments are closed. Login to edit/delete your existing comments