Delete Folders from Multiple Servers

I’m trying to delete folders from multiple Backup servers remotely. Folder name represents server name itself. Executing below script is providing Access denied error though I’m admin on the server. Folders also have needed permissions.

Could someone please help where I’m going wrong?

If Backup Server names are Backup1, Backup2 etc, Folder names are Server1, Server2 which exists either in Backup1 or Backup2 Servers


$BackupServers = Get-Content ‘C:\Scripts\BackupServers.txt’ # Backup Servers are the servers where folders exists
$Servers = Get-Content ‘C:\Scripts\Serverlist.txt’ # contains folder names which are nothing but server names

Write-host "Deleting Backup folders from Backup Servers… " -ForegroundColor cyan -BackgroundColor DarkMagenta

foreach ($BackupServer in $BackupServers)


foreach ($Server in $Servers)

Write-host "Removing Backups from the server " -f Yellow -NoNewline; write-host $Server -f Cyan

Remove-Item -Verbose “\$BackupServer$Servers” -Recurse -ErrorAction continue




Remove-Item : Access is denied
At line:20 char:5

  • Remove-Item -Verbose “\$BackupServer$Servers_Test” -Recurse -Er …
  • CategoryInfo : PermissionDenied: (\BackupServer\Padma_test123:String) [Remove-Item], UnauthorizedAccessException
  • FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot find path ‘\BackupServer\Padma_test123’ because it does not exist.
At line:20 char:5

  • Remove-Item -Verbose “\$BackupServer$Servers_Test” -Recurse -Er …
  • CategoryInfo : ObjectNotFound: (\LVSQLBACKUP\Padma_test123:String) [Remove-Item], ItemNotFoundException
  • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

It looks like your trying to delete a Share, not a folder, hence your Permission Denied:

PermissionDenied: (\LVSQLBACKUP\Padma_test123

The general syntax for a UNC is \server\share, then from there you have folders like \server\share\folder…

You cant delete a share with Remove-Item, you would need something like:


To remove the folder.

Hi Tony,

Thank you for clarification.

The folder name itself is Padma_Test123. In that case should I give \BackupServers\WindowsDrive$\Padma_test123 ?

When I try to navigate from Windows run with \BackupServer\Padma_test123</strong> I’m able to navigate to Padma_test123 folder.

Your help is much appreciated.

Would it hurt you when you tried? :wink:

No Olaf :stuck_out_tongue:

But errored out again with below error

Remove-Item : Access is denied
Remove-Item : Cannot find path ‘\Backupserver\D$\Padma_test123’ because it does not exist

Is there any way that I can delete the folder from server without giving share?


In theory, using ‘\Backupserver\D$\Padma_test123’

Should work. I just ran a simple test using this with no issues:

remove-item -Path '\server\c$\temp\folder'

Are you certain you have permissions?

Without knowing the contents of your CSV files, this is difficult to figure. For example, in your for loop, you are enumerating $servers and also using that in your remove-item instead of $server.

So you have to tell us. Where else should we know? :wink:

[quote quote=290629]
Remove-Item : Access is denied
Remove-Item : Cannot find path ‘\Backupserver\D$\Padma_test123’ because it does not exist[/quote]

As Tony already wrote - that should work if you really have permissions.

Actually there is. You could use Powershell remoting. Therefor you need to have administrative permissions to the remote server - not just to share. :wink:

Invoke-Command -ComputerName 'BackupServer' -ScriptBlock { Remove-Item -Path D:\Padma_test123 }


I’m admin on the servers as well as on the servers trying to delete folder.

The contents in .txt file are below servernames.




Contents in Backup text file is below Backup file server names.




I’m trying to find Server1 in any of Backupservers (Backup servers 1,2,3 because folder with servername will exists in any of these servers) and then when found, delete it.


A duplicate of this is here: [closed thread]