简体   繁体   中英

PowerShell Remove-Item cannot delete populated folder

I am trying to delete some files in:

C:\\inetpub\\wwwroot\\my_site\\

Under my_site , there are many files and folders, including bin folders which has some .dll files. As soon as I run the script, I get an error message that some of the .dll files cannot be accessed because they are being used by another process, and on some dlls, I get a permission denied error.

I don't know what the problem is. I am using the following script, which works perfect for any other folders, but not for the one I am trying to delete.

get-childitem "c:\inetpub\wwwroot\my_site\" -recurse | % {

remove-item $_.FullName -recurse -force

}

Run PowerShell as Administrator

The C:\\inetpub directory has special permissions that require you to run it as an administrator in order to make any changes inside the folder.

This is the script I am using

   $content = get-childitem 'C:\Backups\my_site'      
   $sortedContent = $content |  Sort-Object LastWriteTime -Descending  


   write-host "This is the list of all the backups for my_site :"

   $count = 1
   foreach ($item in $sortedContent)
   {

    Write-Host ("{0}: {1}" -f $count, $item.Name)
    $count++ 
    }



    # 2.Take input from user
    $itemNumber = Read-Host "Please select which backup you want to restore"
    $confirmation = Read-Host "Are you Sure You Want To Proceed:"
    # 2.Take input from user

    if ($confirmation -eq 'y') {

    # 3. BACKUP script
   ./bacup_mysite.ps1
    # 3. BACKUP 

    # 4. DELETE CONTENTS OF my_site


    get-childitem "C:\inetpub\wwwroot\my_site\" -recurse | % {

    remove-item $_.FullName -recurse -force

    }

    }


    # 4. DELETE CONTENTS OF APP

    # 5. COPY CONTENTS OF ZIP INTO APP DIRECTORY
    $itemNumber = $itemNumber -1
    if($sortedContent[$itemNumber].PSIsContainer -eq $true)
    { 
    $src = $sortedContent[$itemNumber].FullName + "\"


    $WebzipPath = $src + "my_site.zip"


    $Date = Get-Date
    $folder_date = $Date.ToString("yyyy-MM-dd_HHmm")
    $tempPath = 'C:\_Temp\Restore\my_site_restore_' + $folder_date
    if (!(Test-Path -path $tempPath)) 
    {
        New-Item $tempPath -type directory
    }   


    ./Library/unzip.ps1 $WebzipPath $tempPath



    $tempPathWeb = $tempPath + "/my_site/*" 

    Copy-Item  -Path $tempPat -Destination 'C:\inetpub\wwwroot\my_site\' -Recurse -
    force

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM