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,379 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-03-24T11:15:36.543+00:00

    Invoke-Command -ComputerName "REMOTE-MACHINE" -ScriptBlock { Remove-SmbShare -Name "SHARE-NAME" -Force}

    This does indeed remove the share but then i cannot remove the folder remotely anymore because the share does not excists after above command. So when i first move the subfolders and files then remove the smbshare with above command, the how do i delete the headfolder without giving in the whole path?

    Let's assume the share is \server\test and the path is \server\d$\department\test en has several subfolders and files. The goal is to copy or move all subolders and files to the destination \server1\share.
    I do not know the "department in advance so cannot give the whole path with department in it.

    I can empty the share: \server\test but the testfolder remains, then remove the share test, that leaves the folder test to remove: \server\d$\department\test


  2. Rich Matheisen 45,011 Reputation points
    2021-03-24T14:59:07.683+00:00

    Do all the work in the script block of the Invoke-Command!

    Before you remove the share, use Get-SMBShare and get the path value for the share. Then remove the share and, using the path you retrieved before removing the share, use Remove-Item to remove the directory.

    0 comments No comments

  3. corne nietnodig 196 Reputation points
    2021-03-28T12:47:14.447+00:00

    Hi, almost there i think. Your answer was the solution for removing the folder inclusive the subfolders however i would like to make the sharename variable so let Powershell ask me for the name and use that input as the $sharename.
    What i have now is not finding the sharename, it copies the folder and subfolders with copy-item but the part the invoke command does not work, Powershell cannot find property 'Name'equal to ''.It leaves it empty.
    How can we use the input from:
    param ($naam)
    if ($naam -eq $null){
    $naam = Read-Host -Prompt "Please enter user logon name"
    }

    Invoke-Command -ComputerName "server" -ScriptBlock { $ShareName = "$naam"
    $p = (Get-SmbShare $ShareName).Path
    Remove-SmbShare $ShareName -Force
    Remove-Item -Path $p -recurse -force
    }

    and use this input for the invoke-command scriptblock?

    0 comments No comments

  4. Rich Matheisen 45,011 Reputation points
    2021-03-28T14:44:02.437+00:00

    This should allow you to use "$naam" in the script block:

    $naam = Read-Host -Prompt "Please enter user logon name"
    
    Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
            Remove-SmbShare $Using:naam -Force
            Remove-Item -Path $p -recurse -force
        }
    
    0 comments No comments