Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove-PSDrive doesn't remove Mapped Drives

I am having trouble deleting a specific drive for a user in my script. I cannot use NET USE as it is removed by security guys in my place. Remove-PSDrive -Name X only removes it for the PS shell not the user's explorer.

any thoughts?

like image 891
FalconRider Avatar asked Mar 06 '26 01:03

FalconRider


1 Answers

What you are after is the WScript.Network Com Object.

# Create new object
$NetDrive = new-object -ComObject WScript.Network
# Remove the network drive (Drive Letter, Force Removal, Remove Mapping)
$NetDrive.RemoveNetworkDrive('X:',$True,$True)

Without doing $True twice, it will only disconnect the mapped drive.

You can map it by doing the below.

# Add the network drive (Drive Letter, Path, Persistent, Username, Password)
$NetDrive.MapNetworkDrive($drive, $path, "true", $user, $pwd)
like image 93
Drew Avatar answered Mar 08 '26 22:03

Drew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!