Why "net use * /delete" does not work but waits for confirmation in my PowerShell script?
Asked Answered
P

2

55

I have a script where I want to disconnect from the mapped drives before I create a new PSDrive.

Otherwise I get this error:

New-PSDrive : Multiple connections to a server or shared resource by the same user , using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again

So I have this line:

net use * /delete

(Unfortunately I could not find a way to disconnect from a specific mapped drive just by providing the server name so far :( )

When PS comes to this line

You have these remote connections:

\\ServerName\SharedFolder Continuing will cancel the connections.

And then it stops executing.

Is there a way to auto confirm disconnecting from the mapped drives automatically without confirmation (it does not have to be the net use /delete solution)?

Note that: I run my script from the Powershell ISE PS promt

Pickled answered 8/11, 2012 at 7:3 Comment(0)
F
140

Try this:

net use * /delete /y

The /y key makes it select Yes in prompt silently

Finfoot answered 8/11, 2012 at 7:28 Comment(3)
And /no selected No in prompt silently.Pember
Does the /Y param only work on the wild card delete?Immerse
This is so, so useful :) Saves typing in the names of sharesCroupier
S
2

With PowerShell 5.1 in Windows 10 you can use:

Get-SmbMapping | Remove-SmbMapping -Confirm:$false
Squaw answered 24/3, 2020 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.