Trying to figure out the syntax of a powershell command to delete specific folders.

We used to use folder redirection and have a lot of data on a server. I need to remove specific folders from each users profile folder. For example we have a folder called users in it is UserA, User B etc in each folder there is a Pictures, Music folder. Its these Music and Pictures folder I need to remove but for thousands of users.

I was looking at something like this But it doesn't jsut do the Users\userA\pictures folder it also removes any folder called pictures in any sub folder in the users folder.
Get-ChildItem -path E:\users -Include 'Pictures' -Recurse -force | Remove-Item -force -Recurse -whatif

I had found a command that would do this 6 months ago when we stopped redirecting another folder but I cannot find the command.

I need something that goes to userA folder deletes either the folder called Pictures or the content of it then moves on to the User B folder.

Can anyone help
Jason KomendatAsked:
Who is Participating?
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
Get-ChildItem -dir c:\Users | Get-ChildItem -Directory | ? { $_.Name -in 'Music','Pictures' } | Remove-Item -Force -WhatIf

Open in new window

0
 
Jason CrawfordTransport NinjaCommented:
Try adding the -Depth and -Directory parameters to Get-ChildItem:

Get-ChildItem C:\users -Recurse -Depth 3 -Directory -Include 'Pictures','Music' | Remove-Item -Force -WhatIf

Open in new window

0
 
Jason KomendatAuthor Commented:
I get this am I missing a comma somewhere? also what number for depth would I use? is 3 the right number?

Get-ChildItem : A parameter cannot be found that matches parameter name 'Depth'.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
-Depth requires PowerShell 5.
0
 
Jason CrawfordTransport NinjaCommented:
Ah that's unfortunate...the -Depth parameter requires PowerShell 5.0.  Do you have any other computer you can use?  You can use Get-ChildItem with UNC paths.  Check the PowerShell version by running $PSVersionTable.  

I believe 3 would be the correct depth.  I created the following folders on my computer:

C:\test\UserA        
C:\test\UserB        
C:\test\UserA\Music  
C:\test\UserA\Pictures
C:\test\UserB\Music  
C:\test\UserB\Pictures

Here is what the first portion of the script brings back:

Capture.PNG
0
 
Jason KomendatAuthor Commented:
This works perfectly, thanks for the help

Get-ChildItem -dir c:\Users | Get-ChildItem -Directory | ? { $_.Name -in 'Music','Pictures' } | Remove-Item -Force -WhatIf
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You might want to add -Recurse to the last command, so it is not asking for confirmation. -WhatIf needs to be removed to perform the deletion, with it only simulates.
0
 
Jason KomendatAuthor Commented:
Is there any way to get this to work without it prompting for every deletion?
Get-ChildItem -dir f:\Users | Get-ChildItem -Directory | ? { $_.Name -in 'music' } | Remove-Item -Force

I have tried -confirm:$false
and
-Recurse -ErrorAction SilentlyContinue
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Adding -Recurse did it for me, and it is what the confirmation suggests to do.
0
 
Jason KomendatAuthor Commented:
Got it, thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.