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
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?
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
}
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?
Rich, can you help me with the last one, where do i put the destination and what is $t +="users\$using:naam
do?