#PSTip Rename a local or a mapped drive using Shell.Application

In the ‘Create mapped network drive using WScript.Network‘ post, it was shown how a mapped drive can be mounted. The mapped drive doesn’t initially have a friendly name–only the name of the shared folder and the server name will be shown. In order to rename the drive using PowerShell, the Shell.Application COM object can be used. This code will allow you to rename a mapped network drive or any local disk:

$ShellObject = New-Object –ComObject Shell.Application
$DriveMapping = $ShellObject.NameSpace('Z:')
$DriveMapping.Self.Name = 'NewName'

To make this shorter the following one-liner can be used to rename a local drive:

(New-Object -ComObject Shell.Application).NameSpace('Z:').Self.Name='NewName'

About the author: Jaap Brasser

Jaap is a Senior System Engineer in the Financial Services industry. He focuses on Microsoft and related technologies and has a passion for PowerShell. In his spare time he maintains his Blog, Twitter feed, @Jaap_Brasser, and contributes on the Official Scripting Guys Forum and Windows PowerShell TechNet forums

Related Posts