Tip: There are several ways to work with network drives:
- WScript.Network COM object
The following commands create a persistent drive (the drive will still here even after logout or reboot), list and remove the drive.
$shareName = '\\server\folder'
$network = New-Object -ComObject WScript.Network
# Credentials not required
net.exe use M: $shareName /PERSISTENT:YES
$network.MapNetworkDrive('M:', $shareName, $true)
# Credentials required
net.exe use M: $shareName /PERSISTENT:YES /USER:DOMAIN\username *
$network.MapNetworkDrive('M:', $shareName, $true, 'DOMAIN\username', 'password')
net.exe use M:
net.exe use M: /DELETE
$network.RemoveNetworkDrive('M:', $true, $true)
Note: When providing credentials, in this example I use the IP Address as it is a workaround here.