1

does anybody know the equivalent in PowerShell of the following windows command:

net use \\server_name\share_name /delete

I'd like to disconnect from the shares from powershell, included the ones that are not mapped as shared drives.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
mic.sca
  • 1,688
  • 2
  • 18
  • 34
  • 2
    Maybe answer to this question: http://stackoverflow.com/questions/4055704/how-can-i-convince-powershell-run-through-task-scheduler-to-find-my-network-dr/4097398#4097398 could somehow guide you. – rsc Nov 17 '11 at 12:33
  • 3
    why not just use "net use \\servername\share\ /delete" ? powershell is a shell, not a replacement operating system. – x0n Nov 17 '11 at 15:50
  • @x0n because of a bug in ps that raises an exception when you call poweshell redirecting the standard error in standard output with 2&>1 : have a look here: http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/ . To avoid the problem I call from powershell a bat that calls the net use redirecting the standard error in the standard output, otherwise I would get the error. My ps script is called by a scheduler that calls it with 2&>1 over which I don't have control, and the ps does some reasoning to find the shares it has to disconnect. – mic.sca Nov 20 '11 at 09:35
  • @mic.sca ok, I've answered with the equivalent WMI. – x0n Nov 20 '11 at 14:59

1 Answers1

0

WMI should do the trick:

ps> get-wmiobject -computer server -class win32_share -credential $creds | `
    where-object { $_.name -eq "sharedfolder" } | foreach-object  { $_.delete() }
x0n
  • 51,312
  • 7
  • 89
  • 111
  • 1
    this deletes the share itself, while net use /delete is called by the client to disconnect from a share – mic.sca Nov 21 '11 at 14:23