is there a way to delete a directory in powershell?

If you go this way, it’s a beginning.

Get-ChildItem -Path C:\ -Recurse | Where-Object {$_.Name -eq "hi.exe" -or $_.Name -eq "newScript.bat" } | foreach { $_.Directory }

Instead of sending the directory name to console, you can delete the directory where they resides.

EDIT:

Since I missed the “contains both files”, I suggest this. Feel free to write it to a one-liner, but I did it step-by-step so there is “checkpoints” on the way.

$potensial_files = Get-ChildItem -Path C:\ -File -Recurse | Where-Object { @("calc.exe", "cmd.exe").Contains($_.Name) }

$potensial_files.Directory.FullName | select –unique | foreach { 

    $dir = $_
    $files = $potensial_files | Where-Object { $_.Directory.FullName -eq $dir }
    if(($files | Measure-Object).Count -eq 2)
    {
        Write-Host "$dir contains $files"
    }
}

And I am sure that this can be written a lot shorter, but what is the point for a “run once” script?

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top