Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, March 4, 2014 7:37 PM
Hello!
My issue is that a former admin somehow managed to get a mapped drive onto several hundred machines. The user under which this mapped drive was created is causing us a huge headache. What I'm looking to do is to write a script that basically looks at the path of the mapped drives, and if a drive is mapped to a certain location, disconnect it.
For instance:
Z is mapped to \server01\share01
If Z is indeed mapped to \server01\share01 then disconnect it. If drive Z exists, but is not mapped to \server01\share01 then leave it alone.
I came across the Test-Path and Get-PSDrive commands, but I couldn't figure out how to set the Root from Get-PSDrive as a variable, or the Providername from the WMI equivalent.
All replies (12)
Tuesday, March 4, 2014 8:00 PM âś…Answered
Hi,
Give this a try:
Get-PSDrive | ForEach {
If ( $_.DisplayRoot -eq '\\server01\share01' ) {
Remove-PSDrive -Name $_.Name -WhatIf
}
}
This won't actually remove anything for the time being. If you're happy with the results, remove the -WhatIf switch and rerun the script.
Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)
Tuesday, March 4, 2014 8:12 PM
Hmmm.
I mapped a drive to the location in question, changed the location in the script and ran it. Getting a blank output.
Tuesday, March 4, 2014 8:15 PM
If you type Get-PSDrive by itself, do you see \server01\share01 appear in the Root column? If not, the code above won't detect anything.
Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)
Tuesday, March 4, 2014 8:22 PM
Hi,
Give this a try:
Get-PSDrive | ForEach { If ( $_.DisplayRoot -eq '\\server01\share01' ) { Remove-PSDrive -Name $_.Name -WhatIf } }This won't actually remove anything for the time being. If you're happy with the results, remove the -WhatIf switch and rerun the script.
Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)
DisplayRoot? Did you mean Root? Although DisplayRoot is a property, it's blank for me as well.
Tuesday, March 4, 2014 8:24 PM
Looks like I needed to relaunch ISE for whatever reason. It does indeed give me an output warning for deleting the drive.
Tuesday, March 4, 2014 8:26 PM
I used a simple If statement.
If ((Get-PSDrive).Name -eq 'Z' -and (Get-PSDrive).Root -eq '\\server01\share01') {
Remove-PSDrive -Name 'Z'
}
Tuesday, March 4, 2014 8:35 PM
DisplayRoot? Did you mean Root? Although DisplayRoot is a property, it's blank for me as well
Nope, DisplayRoot is what I was after. Type Get-PSDrive and find something that shows up with a UNC path. Then do Get-PSDrive x | Select *. You'll see DisplayRoot shown.
Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)
Tuesday, March 4, 2014 8:37 PM
Little off topic, but where do I find resources on items such as this? I knew there was a way to isolate the "Root" aspect of what Get-PSDrive returns, but I had no idea where to find out how to do that. Any preferred training materials?
Tuesday, March 4, 2014 8:40 PM
Not off topic at all. There are some good resources available online, but I've always found that a physical book is the most helpful. There's plenty of them on the market now.
Here's a few links that you might find helpful:
http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx (Microsoft learning resources, great place to start)
http://gallery.technet.microsoft.com/scriptcenter (Script repository - lots of good examples to look at and build from)
http://ss64.com/ps/ (My favorite command reference)
http://powershell.com/cs/blogs/ebookv2/default.aspx (Free ebook)
Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)
Tuesday, March 4, 2014 8:43 PM
Thanks for the information, Mike. I'm still not seeing it, but I'll continue working with it until I do (or until I move onto something different).
Tuesday, March 4, 2014 8:47 PM
Cheers, here's an example from my session:
PS C:\> Get-PSDrive v | select *
Used : 216021852160
Free : 3730027581440
CurrentLocation :
Name : V
Provider : Microsoft.PowerShell.Core\FileSystem
Root : V:\
Description :
Credential : System.Management.Automation.PSCredential
DisplayRoot : \\serverName\shareName
Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)
Tuesday, March 4, 2014 9:02 PM
I was running it on my own machine expecting some form of output, but I totally forgot to map the drive on my own machine first. I then closed/opened ISE again and was able to get output.