Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Can't delete folder recursively in script, no errors are printed.

$
0
0

I'm trying to remove a directory with a powershell script, the script seems to be working fine except for the deletion at the end. It's not printing out any errors so I'm not sure how to determine why it's failing.

 

    $location = $args[0];

    $copySuccessful;

 

    $extensions = @(".mkv", ".mp4", ".avi", ".wmv");

    $pruneSize = 41240000;

 

    Set-Location $location;

 

    #loop through all child objects

    Get-ChildItem -Recurse | ForEach-Object {

        #check the extension of each file

        If($extensions -contains $_.Extension) {

            #copy the file to the parent folder

            Copy-Item $_ "..\";

 

            #test if the file was successfully copied

            If(Test-Path "..\$_") {

                Write-Host copy successful;

 

                #delete old file

                Remove-Item $_ -Force;

            }

            else {

                Write-Host copy failed;

            }

        }

    }

 

    #go back out to parent folder

    Set-Location "..\";

 

    #if the copy was successful, delete the folder

    If($copySuccessful) {

        Remove-Item -Recurse -Force $location;

    }


Viewing all articles
Browse latest Browse all 6937

Latest Images

Trending Articles



Latest Images