Tuesday, December 30, 2014

#38 : Powershell script to Map Network drive

We often need to map network drives. Especially if you are writing a package which installs something from a shared drive. It becomes important to map drives and give a drive letter for the same.

Net share is the command which can be used to accomplish the same. Below is the Powershell version of the same -

Adding a Network Drive 
(New-Object -Com WScript.Network).MapNetworkDrive("x:" , "\\divine\d$")

That's it!
But this not the all about this. There might be further requirement to remove a network drive.


Listing the Network Drives
(New-Object -Com WScript.Network).EnumNetworkDrives()


Removing a Network Drive
(New-Object -Com WScript.Network).RemoveNetworkDrive("x:")

You can use EnumNetworkDrives() to find if particular share exists or not, then remove it. Anyways, there are several methods to do something, my job was just to give your tips not thesis.

Enjoy!!


1 comment:

  1. Very helpful, cheers! Other solutions use Powershell 3.0, but this works with 2.0 which is what I was looking for :-)

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...