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-03-29T17:52:24.327+00:00

    This works indeed!

    Almost there. I have 2 shares with 2 different names but on the same server which must be copied and then deleted and unshared. I thought this can be done to ask twice about the sharename (it is maybe not efficient but could work for a beginner...) However when i execute them seperatly they work but when starting it in Powershell-ise it gives a copy-item syntax on the first $naam, syntax filename or volume name is wrong.

    But what it does is removing the share and folders but not copy-item!, so this is tricky...

    $naam = Read-Host -Prompt "Please enter user logon name"
    $Source = "\server\$naam\"
    $Target="\server\path\Users\$naam\"
    Get-ChildItem -Path $Source -Recurse | copy-Item -Destination $Target -Force
    Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
    Remove-SmbShare $Using:naam -Force
    Remove-Item -Path $p -recurse -force
    }
    $naam = Read-Host -Prompt "fill in sharename"
    $Source = "\server\$naam\"
    $Target="\server\otherpath\$naam\"
    Get-ChildItem -Path $Source -Recurse | Copy-Item -Destination $Target -Force
    Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
    Remove-SmbShare $Using:naam -Force
    Remove-Item -Path $p -recurse -force
    }

    Can i make something better out of this?


  2. Rich Matheisen 45,011 Reputation points
    2021-03-29T19:00:14.987+00:00

    See if this works:

    param (
        [String]$naam,
        [String]$firstpath,     # path
        [String]$secondpath     # otherpath
    )
    
    Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
        $t = (Get-SmbShare $Using:firstpath).Path
        $t += "\Users\$Using:naam\"
        Remove-SmbShare $Using:naam -Force
        Copy-Item $p -Destination $t -Force
        Remove-Item -Path $p -recurse -force
        }
    Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
        $t = (Get-SmbShare $Using:secondpath).Path
        $t += "\$Using:naam\"
        Remove-SmbShare $Using:naam -Force
        Copy-Item $p -Destination $t -Force
        Remove-Item -Path $p -recurse -force
    }
    
    0 comments No comments

  3. corne nietnodig 196 Reputation points
    2021-03-31T17:50:18.1+00:00

    Rich,

    I do not know what you mean with:
    $t = (Get-SmbShare $Using:firstpath).Path
    $t += "\Users\$Using:naam\"

    You are using $t as a destination of the copy item and this is not the sharename this is an harddisk on the network zo lets say: \disk1\pathtofolder\samenameas $naam

    Where do i give in that folder path of the destination which copy-item must use as the destination?

    0 comments No comments

  4. corne nietnodig 196 Reputation points
    2021-04-01T16:48:29.22+00:00

    Rich, can you help me with the last one, where do i put the destination and what is $t +="users\$using:naam
    do?

    0 comments No comments