PowerShell Remove-PSDrive

PowerShell's Get, Then RemoveRemove-PSDrive Technique

My main task for Remove-PSDrive is when testing scripts.  If I want to run code for a second time, it often makes sense to undo the previous instruction.

I also want to emphasise the simple, but useful Get-xyz, then Remove-xyz technique.

Topics for Windows PowerShell Remove-PSDrive

 ♣

New-PSDrive: Create a Mapped Drive

Before we can Remove-PSDrive the drive must exist!  What better than to use the sister command New-PSDrive to map a network drive.

# PowerShell script to map the Y: drive to a network share.
$Network = "\\Server\ShareName"
New-PSDrive -Name Y -PSProvider FileSystem -Root $Network -Persist
Invoke-Item Y:

Note 1: Please change the value for the $Network variable, otherwise it will not work.

Note 2: Thanks the -Persist parameter in PowerShell 3.0, we can see the drive in explorer.

The Main Event: Remove-PSDrive

Here is an illustration of the Get-xyz | Remove-xyz technique.  The benefit is that you can test with Get- before you actually zap with Remove-xyz.

This example only works if you have a mapped drive called Y.

# PowerShell script to remove the Y: drive
Clear-Host
Get-PSDrive -Name Y | Remove-PSDrive
Get-PSDrive [A-Z]

Note: Strangely, Get and Remove-PSDrive need the bare drive letter (Y), wheras Invoke-Item needs that colon (Y:).

Note: FileSystem drives are not the only provider that you can remove, I have used a similar tecnique to remove registry drives that I no longer neeed.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Mapping Network Drives with PowerShell

For me, mapping network drives started with VBScript, indeed, it was the desire to program drive letters connected to network shares that sparked my interest in scripting.  We can map drives in PowerShell v 1.0 by mimicing VBScript and creating a ComObject, but here in version 3, we can get the job done much easier thanks to New-PSDrive and its -Persist parameter.

Get-Help Remove-PSDrive

PowerShell's built-in help is marvellous for learning about a cmdlet's parameters; in this instance it reveals a -Force option.

# Research Remove-PSDrive Parameters
Get-Help Remove-PSDrive

Note 10: The list of parameters reminds us that -WhatIf is particulary useful with cmdlets like this which contain the Remove verb.

 

Other Members of the PSDrive Family

We have already met two other members of the PSDrive family:

New-PSDrive – The modern way of mapping a network drive.

Get-PSDrive – Reveals PowerShell's thinking by listing drives such as Variable, Function and Alias.

See more on Get-PSDrive »

Summary of PowerShell 3.0 Remove-PSDrive

Remove-PSDrive is ideal for testing scripts that are designed to map network drives.  Before I want to run code for a second time, it prevents error messages if I remove a previous mapping.

This page is a great example of combining Get-xyz with Remove-xyz.

If you like this page then please share it with your friends

 


See more Windows PowerShell  examples of variables

Syntax   • PowerShell Variables   • Get-PSProvider   • PowerShell Dollar Variable

PowerShell Functions   • Get-PSDrive   • PowerShell New-PSDrive   • Remove-PSDrive

PowerShell Home   • Foreach loops   • PowerShell Foreach   • Foreach-Object cmdlet

Please email me if you have a better example script.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to  correct the fault.