I'd like to remove network drives from the system using a PowerShell script.
I need to find the drive by name, as the command $net.RemoveNetworkDrive('P:',1)
needs the driveletter.
Is there a command to find the network drive letter in PowerShell?
My script:
$Drive = "\\192.168.2.117\Blabla"
echo $Drive
cls
if (((New-Object -Com WScript.Network).EnumNetworkDrives() | Where-Object {$_ -eq $Drive}))
{
echo 'found Drive'
#$net = $(New-Object -comobject WScript.Network)
#$net.RemoveNetworkDrive('P:',1)
}
else
{
echo 'Drive not there'
}
You could try the following method for getting the drive information:
$Drive = Get-WmiObject -Class Win32_mappedLogicalDisk `
-filter "ProviderName='\\\\192.168.2.117\\Blabla'"
$Drive.Name
$Drive.Name
would become the drive letter which should allow you to the do the following:
$net = $(New-Object -comobject WScript.Network)
$net.RemoveNetworkDrive($Drive.Name,$true)