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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
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
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
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.
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?
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
}