remove foldershare remotely

corne nietnodig 196 Reputation points
2021-03-19T12:24:23.78+00:00

We have shared folders which must be copied and deleted regurally. So i made a powershell script and try to do copy and delete the share but i can only copy the complete folderstructure but not delete it and not move it remotely. When all subfolders and files are copied to another server then the rootfolder share is empty but cannot be deleted remotely not in a batchfile with cmd not with powershell and not with robocopy. Process is in use. But when i make a new share end trie it is also not deleted, just the subfolders. When i delete the complete patch remotely then it can be deleted but not the share. \servername\share what can be deleted is: \servername\patch to share\share or \servername\e$\patch Is there somewhere a setting that prevents deleting rootfolder shared folders?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,011 Reputation points
    2021-03-24T19:04:40.52+00:00

    You don't have to pipe anything. Put this in the script block (change the share name, of course!):

    $ShareName = "Junk"
    $p = (Get-SmbShare $ShareName).Path
    Remove-SmbShare $ShareName -Force
    Remove-Item -Path $p
    
    0 comments No comments

14 additional answers

Sort by: Most helpful
  1. corne nietnodig 196 Reputation points
    2021-04-03T08:18:54.997+00:00

    I have tried above script but this is not asking for the sharename so it gives an error.
    Filled in the destination in
    $t = (Get-SmbShare $Using:firstpath).Path
    $t += "\server\pathtodestination\Users\$Using:naam\"

    and in the second:
    $t = (Get-SmbShare $Using:secondpath).Path
    $t += "\server\pathtootherdestination\$Using:naam\"

    Gives the following:

    No MSFT_SMBShare objects found with property 'Name' equal to ''. Verify the value of the property and retry
    etc etc

    0 comments No comments

  2. corne nietnodig 196 Reputation points
    2021-04-03T08:48:33.43+00:00

    I have now tried this agin but then in 2 steps: 1. the first share and then again the second share. Strange for me is that the first time with the first share copy-item works and the share is deleted, but when i want to start the second script, again asking for the name copy-item does not start, but the removing the share and folder is working so then i have no copy...

    This is to risky to execute because it is deleting but not copying the second share.

    Is it not possible to ask 2 times for the name and give the second input a different name or something?

    0 comments No comments