Avatar of Jason Komendat
Jason Komendat

asked on 

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
PowershellWindows Batch

Avatar of undefined
Last Comment
Jason Komendat
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America image

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

Avatar of Jason Komendat
Jason Komendat

ASKER

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'.
Avatar of Qlemo
Qlemo
Flag of Germany image

-Depth requires PowerShell 5.
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:

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jason Komendat

ASKER

This works perfectly, thanks for the help

Get-ChildItem -dir c:\Users | Get-ChildItem -Directory | ? { $_.Name -in 'Music','Pictures' } | Remove-Item -Force -WhatIf
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of Jason Komendat

ASKER

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
Avatar of Qlemo
Qlemo
Flag of Germany image

Adding -Recurse did it for me, and it is what the confirmation suggests to do.
Avatar of Jason Komendat

ASKER

Got it, thanks
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo