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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
-Depth requires PowerShell 5.
0
Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

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
Qlemo"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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Qlemo"Batchelor", 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
Qlemo"Batchelor", 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.