# Delete Folders from Multiple Servers

**URL:** <https://forums.powershell.org/t/delete-folders-from-multiple-servers/15580>\
**Category:** PowerShell Help\
**Created:** [February 4, 2021, 11:15am UTC](https://forums.powershell.org/t/delete-folders-from-multiple-servers/15580 "2021-02-04T11:15:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![powershell\_techie](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@powershell\_techie](https://forums.powershell.org/u/powershell_techie)\
**Post date:** [February 4, 2021, 11:15am UTC](https://forums.powershell.org/t/delete-folders-from-multiple-servers/15580/1 "2021-02-04T11:15:17Z")

</div>

Team,

I’m trying to delete folders from multiple Backup servers (SQL Servers). Folder names are Server Names itself .

When I use below script remotely to delete folders, I get access denied error. Could you please help me why this code is not working

```
$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
}
}
```

Error

```
Remove-Item : Access is denied
At line:20 char:5
+ Remove-Item -Verbose “\\$BackupServer\$Servers_Test” -Recurse -Er …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\\LVSQLBACKUP\Padma_test123:String) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot find path ‘\\LVSQLBACKUP\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
```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [February 7, 2021, 6:59pm UTC](https://forums.powershell.org/t/delete-folders-from-multiple-servers/15580/2 "2021-02-07T18:59:42Z")

</div>

Its just because of double hoping issue. When in PowerShell if we try to do any action which requires authentication within a remote session, then for security reasons the authentication is done by default using NT Authority\System account. You can understand more about it here. Here you are trying to access a network share which requires authentication.

> **[Making the second hop in PowerShell Remoting - PowerShell](https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-7.2&viewFallbackFrom=powershell-7.1)**
>
> This article explains the various methods for configuring second-hop authentication for PowerShell remoting, including the security implications and recommendations.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 7, 2021, 9:07pm UTC](https://forums.powershell.org/t/delete-folders-from-multiple-servers/15580/3 "2021-02-07T21:07:57Z")

</div>

Didn’t we answer this question already?

[Delete Folders from Multiple Servers](https://powershell.org/forums/topic/delete-folders-from-multiple-servers-2/#post-290752)

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [February 9, 2021, 12:19am UTC](https://forums.powershell.org/t/delete-folders-from-multiple-servers/15580/4 "2021-02-09T00:19:14Z")

</div>

Didn’t noticed the original one. @PowerShell_Techie, This thread will be closed, please continue to reply in the old thread if the problem is not solved.

[https://powershell.org/forums/topic/delete-folders-from-multiple-servers-2/#post-290752](https://powershell.org/forums/topic/delete-folders-from-multiple-servers-2/#post-290752)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/delete-folders-from-multiple-servers/15580/5 "2024-05-16T20:27:53Z")

</div>


