Managing network drives in Powershell

It used to be, in Windows, that the way to map network drives was with the net use command, but with Powershell this has become a bit easier. I haven’t, however, been able to find a simple overview of the commands and their usage so hopefully this will be of use to someone other than me.

There are three commands (cmdlets in Powershell speak) that you need to know about.

Get-PSDrive

This lists the currently mapped drives and a bit of information about them, most usefully the network path that the drive is mapped to. You can also enter the drive letter as a parameter so that

Get-PSDrive C

Tells you that drive C is mapped to the Windows C: drive. Obvious, really, but more useful if you want to know about other mapped drives.

New-PSDrive

This is the cmdlet that maps drives to to shared folders. If I want, for example, to map my Y drive to folder sharedfiles on server Server the command would look like this:

New-PSDrive –Name “Y” –PSProvider FileSystem –Root “\Serversharedfiles” –Persist

The PSProvider switch tells Powershell what sort of data this drive is accessing. Filesystem, obviously, is the filesystem but you can also map to environment variables and other oddities.

The Root switch is needed so that Powershell understands the path name.

The Persist switch tells Powershell that this mapping is persistent so the mapped drive is available every time you reboot. This switch also causes Powershell to create a mapped network drive in Windows which can be accessed via all the standard Windows tools such as Windows Explorer.

Remove-PSDrive

When you no longer need the mapped drive, you can remove the mapping with this command.

Remove-PSDrive Y

Does exactly what you would expect.