How to change drive label using Powershell?
06:51 22 Feb 2012

I'm trying to mount a couple of network drives and then rename them. However my Powershell script is failing after the first drive renames fine. For the second one I get an error:

Exception calling "Put" with "0" argument(s): ""
At C:\Users\ben\Documents\mapdrives.ps1:12 char:11
+ $disk.Put <<<< ()
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

My script looks like this:

cls
$Username = ""
$Password = ""
$net = New-Object -com WScript.Network; 

$Drive = "M:"
$UNC = "\\server01\bin$"
$net.mapnetworkdrive($Drive, $Unc, $false, $Username, $Password)
$disk = Get-WmiObject "Win32_LogicalDisk WHERE DeviceID='M:'"
if (-not $disk) { return }
$disk.VolumeName = "server1_bin"
$disk.Put()

$Drive = "N:"
$UNC = "\\server01\data$"
$net.mapnetworkdrive($Drive, $Unc, $false, $Username, $Password)
$disk = Get-WmiObject "Win32_LogicalDisk WHERE DeviceID='N:'"
if (-not $disk) { return }
$disk.VolumeName = "server1_data"
$disk.Put()

What am I doing wrong?

powershell