Share via


Deleting hidden folder structure

Question

Saturday, August 13, 2011 2:59 PM

Hi People, I am unable to delete a hidden folder structure. It is asking me to confirm the deletion though I am using -confirm:$true. Any help is appreciated.

Folder Structure:

C:\Hiddenfolder1

      | -- Hiddenfolder2

      | - -Text file 1

The command I am using is ..

Get-Item c:\Hiddenfolder1 -force | Remove-Item -force -recurse -confirm:$true

I also tried

Get-ChildItem c:\hiddenfolder1 -recurse -force | remove-item -force -recurse -confirm:$true

Thanks,

Sitaram Pamarthi

Blog : http://techibee.com

Follow on Twitter

This posting is provided AS IS with no warranties or gurentees,and confers no rights

All replies (16)

Saturday, August 13, 2011 3:40 PM ✅Answered

Get-ChildItem c:\hiddenfolder1 -recurse -force |







select -expand fullname sort length -desc |







foreach-object {remove-item $_ -force -confirm:$false}

That will do a recursive gci, ouput the full names, sort them in descending order by the length of the fullname, and then delete them in that order.  No parrent container should get deleted before all of the child items in that container have been deleted.

 

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Saturday, August 13, 2011 3:46 PM ✅Answered

Hi All,

Thanks for all your help.

I managed to get it work by using below code.

Get-ChildItem c:\hiddenfolder1 -recurse -force | ? { $_.PSIsContainer -ne $true } | % {Remove-Item -Path $_.FullName -Force }
Remove-Item C:\hiddenfolder1 -Recurse -Force

Remove-Item cmdlet is not capable of deleting hidden folders with files inside it. So the work around is to delete files first and then empty directories.

Thanks,

Sitaram Pamarthi

Blog : http://techibee.com

Follow on Twitter

This posting is provided AS IS with no warranties or gurentees,and confers no rights


Saturday, August 13, 2011 3:11 PM | 1 vote

We had another thread in the Scripting Guy's Forum earlier this week:

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/74360eaa-ac04-4128-a92b-db46f744f9fd

Basically, the -recurse parameter of remove-item just doesn't work, so this won't work:

Get-Item c:\Hiddenfolder1 -force | Remove-Item -force -recurse -confirm:$true

The default value passed down the pipeline by get-childitem is the file name (no path information), so doing the -recurse in gci and piping that directly to remove-item doesn't work, because remove-item will belooking for all the files in the current path.

See if this works better:

Get-ChildItem c:\hiddenfolder1 -recurse -force |
foreach-object {remove-item $_.fullname -force -confirm:$true}

 

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Saturday, August 13, 2011 3:16 PM

We had another thread in the Scripting Guy's Forum earlier this week:

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/74360eaa-ac04-4128-a92b-db46f744f9fd

Basically, the -recurse parameter of remove-item just doesn't work, so this won't work:

Get-Item c:\Hiddenfolder1 -force | Remove-Item -force -recurse -confirm:$true

The default value passed down the pipeline by get-childitem is the file name (no path information), so doing the -recurse in gci and piping that directly to remove-item doesn't work, because remove-item will belooking for all the files in the current path.

See if this works better:

Get-ChildItem c:\hiddenfolder1 -recurse -force |
foreach-object {remove-item $_.fullname -force -confirm:$true}

 

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

Thanks for very quick reply. I tried the code snippet you provided but it is still prompting for confirmation.

 

Thanks,

Sitaram Pamarthi

Blog : http://techibee.com

Follow on Twitter

This posting is provided AS IS with no warranties or gurentees,and confers no rights


Saturday, August 13, 2011 3:17 PM

del C:\Folder -Force -Recurse


Saturday, August 13, 2011 3:20 PM

Thanks for very quick reply. I tried the code snippet you provided but it is still prompting for confirmation.

 

If you don't want the confirmation prompt, set -confirm to $false. [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Saturday, August 13, 2011 3:23 PM

del is just an alias for remove-item.

I don't understand how aliasing the cmdlet is going to fix the problem with -recurse.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Saturday, August 13, 2011 3:30 PM

del is just an alias for remove-item.

I don't understand how aliasing the cmdlet is going to fix the problem with -recurse.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

del C:\Folder -Force -Recurse - delete folder with all object,but if need to save root C:\Folder, del C:\Folder\ -Fo -Re -it's works too.


Saturday, August 13, 2011 3:30 PM

In fact I tried with $false only but It didn't help me.

PS C:\ Get-ChildItem c:\local\hiddenfolder1 -recurse -force |
>> foreach-object {remove-item $_.fullname -force -confirm:$false}
>>

Confirm
The item at C:\local\hiddenfolder1\hiddenfolder2 has children and the Recurse
parameter was not specified. If you continue, all children will be removed with
 the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):

Thanks,

Sitaram Pamarthi

Blog : http://techibee.com

Follow on Twitter

This posting is provided AS IS with no warranties or gurentees,and confers no rights


Saturday, August 13, 2011 3:32 PM

The documentation on the cmdlet (get-help remove-item -full) says it doesn't work.[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Saturday, August 13, 2011 3:35 PM

The documentation on the cmdlet (get-help remove-item -full) says it doesn't work. [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

As example:

PS 28 > dir 1


  Directory: C:\Users\Александр\1


Mode        LastWriteTime   Length Name
----        -------------   ------ ----
d----    13.08.2011   19:33      2
-a---    13.08.2011   19:33     8 1.txt


PS 29 > del 1 -force

Confirm
The item at C:\Users\Александр\1 has children and the Recurse parameter was not specified. If you continue, all
children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
PS 30 > del 1 -force -rec

Saturday, August 13, 2011 3:52 PM

The documentation says it doesn't work properly, but doesn't provide any details. It may break down when applied to nested directory structures. I haven't really tested it.

The objective seems to be to avoid that prompt about a folder having child items, and the confirm parameter doesn't seem to affect that.

Sorting the fullnames in descending order of length and then deleting them in that order avoids ever getting that prompt.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Saturday, August 13, 2011 3:58 PM

For example:

PS 25 > dir 1 -fo -rec | ft fullname,attributes -a

FullName  Attributes
--------  ----------
C:\Users\Александр\1\2 Hidden, Directory
C:\Users\Александр\1\4 Hidden, Directory
C:\Users\Александр\1\1.txt Archive
C:\Users\Александр\1\2\3 Directory
C:\Users\Александр\1\2\1.txt Archive
C:\Users\Александр\1\2\3\1.txt Archive
C:\Users\Александр\1\4\1.txt Archive
PS 26 > [io.directory]::Delete("C:\Users\Александр\1",$true)
PS 27 > gi 1 -fo
Get-Item : Cannot find path 'C:\Users\Александр\1' because it does not exist.
At line:1 char:3
+ gi <<<< 1 -fo
 + CategoryInfo : ObjectNotFound: (C:\Users\Александр\1:String) [Get-Item], ItemNotFoundException
 + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand


PS 37 > dir 1 -fo -rec | ft fullname,attributes -a

FullName  Attributes
--------  ----------
C:\Users\Александр\1\2 Hidden, Directory
C:\Users\Александр\1\4 Hidden, Directory
C:\Users\Александр\1\1.txt Archive
C:\Users\Александр\1\2\3 Directory
C:\Users\Александр\1\2\1.txt Archive
C:\Users\Александр\1\2\3\1.txt Archive
C:\Users\Александр\1\4\1.txt Archive

PS 38 > del 1 -fo

Confirm
The item at C:\Users\Александр\1 has children and the Recurse parameter was not specified. If you continue, all
children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1".

PS 39 > del 1 -fo -rec -Verbose
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1".
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1\2".
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1\2\3".
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\2\3\1.txt".
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\2\1.txt".
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1\4".
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\4\1.txt".
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\1.txt".


PS 40 > gi 1 -fo
Get-Item : Cannot find path 'C:\Users\Александр\1' because it does not exist.
At line:1 char:3
+ gi <<<< 1 -fo
 + CategoryInfo : ObjectNotFound: (C:\Users\Александр\1:String) [Get-Item], ItemNotFoundException
 + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand


PS 49 > del 1\* -fo -rec -Verbose
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1\2".
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1\2\3"
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\2\3\1.txt
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\2\1.txt".
VERBOSE: Performing operation "Remove Directory" on Target "C:\Users\Александр\1\4".
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\4\1.txt".
VERBOSE: Performing operation "Remove File" on Target "C:\Users\Александр\1\1.txt".

PS 50 > dir 1 -fo -rec | ft fullname,attributes -a
PS 51 >

 

Remove-Item cmdlet is not capable of deleting hidden folders with files inside it. - Incorrect


Saturday, August 13, 2011 4:03 PM

I do not understand the point. 

It doesn't avoid the prompt issue, and knowing the documentation says the parameter doesn't work properly why recommend using it without knowing what it is that doesn't work?

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Saturday, August 13, 2011 4:08 PM

I do not understand the point. 

It doesn't avoid the prompt issue, and knowing the documentation says the parameter doesn't work properly why recommend using it without knowing what it is that doesn't work?

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

Because in this case it works as it should, no problems there.


Saturday, August 13, 2011 4:19 PM

It did, and if the scenario it's being applied in by the OP confoms to your test scenario, it will work there, too.  But that may not be a good assumption.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "