R
R
Ruslan2017-05-26 11:10:56
PowerShell
Ruslan, 2017-05-26 11:10:56

How to delete all files and subfolders within a folder, preferably using PowerShell?

Good afternoon community.
There is a structure, something like this.
files
-01. Folder name
--One folder
--Two folder to be deleted
---folder
---mother
---files
-02. Folder name
--One folder
--Two folder to be deleted
---folder
---mother
---files
How to write a script that would delete everything inside folders "Two folder to be deleted" - files, folders, hidden files, absolutely everything.
I managed to write this:

$path = "d:\files\01. Имя папки\Два папка, которую надо удалить\" 
$path2 = "d:\files\02. Имя папки\Два папка, которую надо удалить\" 
 
if (Test-Path $path)
{ 
   $path + " Exists" 
   Remove-Item -path $path -Recurse -Force
   [System.Threading.Thread]::Sleep(1500)   
}
else
{ 
   Write-host -foregroundcolor Red  $path  " Does not exist" 
} 
if (Test-Path $path2) { 
    
   $path2 + " Exists" 
   Remove-Item -path $path2 -Recurse -Force
   [System.Threading.Thread]::Sleep(1500) 
}
else
{ 
   Write-host -foregroundcolor Red  $path2  " Does not exist" 
}

But the root folder is also deleted.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
azarij, 2017-05-26
@azarij

if the previous answer did not help, do those folders that need to be deleted have any distinguishing feature? you need something that you can catch on with the logic of the script.
for example, if you make a dir, the desired folder will always be the second (first, eighth, etc.) in the list if sorted by name, for example, or not?

S
strangerror, 2017-06-01
@strangerror

Remove-Item "$path\*" -Force -Recurse -Confirm:$false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question