Automating the world one-liner at a time…
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() }
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() }
Hope This Helps,
James Brundage [MSFT]
Automatically generated with Write-CommandBlogPost
Am I confused, or this do the same as the label command from cmd?
So with this you can rename the system drive?
That will change all the system variables and registry entries to make the system able to boot and work from a different drive name?
I have create a powershell script that basically copies files from a server share to the desktop. When I run the script as the logged on user I get access denied messages. I would like to run this script on the desktop as an admin equivelent thru a login script. If the user is an admin on the machine it works fine, but I dont want normal users to be admins. My email is [email protected]. Any help would be greatly appreciated.
What would be the command to make this function available to execute like Get-Process?