powershell delete subfolders

I am still learning my way round Powershell and not using the pipeline or foreach command in circumstances I probably should be, so hoping for a little layman guidance here. I am migrating some data from an old domain to a new one and the old data I have been given is layed out in folder structure like this:
D:\Homes\Users\%OldUsername%\My Documents
D:\Homes\Users\%OldUsername%\My Work
D:\Homes\Users\%OldUsername%\appdata
I want to delete the D:\Homes\Users\%OldUsername%\appdata directory before I move the data to its new home
before I probably would have opened excel and created a line by line .bat file looking like this
RD D:\Homes\Users\fred\appdata
RD D:\Homes\Users\Sam\appdata
RD D:\Homes\Users\Sally\appdata
etc. etc.
Is there a better way I do this using PowerShell using the pipeline and / or a for-eachobject loop?
Can you please give any answers as verbose as possible so I can better understand the syntax. thanks in advance
Dead_EyesAsked:
Who is Participating?
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.

SubsunCommented:
You can try this code.. Input the user names in C:\users.txt file..
GC C:\users.txt | 
 ?{$_.Trim() -ne $null -and $_.Trim() -ne ""}| %{
  $User = $_
	Try{
	Remove-Item "D:\Homes\Users\$user\Appdata" -Force -Recurse -EA Stop
	}Catch{
	Write-Host "Error Deleting $user Data"
	}
}

Open in new window

Sample input file..
fred
Sam
Sally

Open in new window

PS: I have not tested the code, you can test the code before run in to production..You need to run the script with a user who have permission to delete the folders..
0
Dead_EyesAuthor Commented:
Hi subsun thanks for the quick reply but this was the sort of answer I was not after. As I stated in my question I really need the answer to be as verbose as possible (preferably with an explanation, so I stand a chance of seeing how it works) and I am trying to do this within a console so if there is no way of accounting for unknown folder name then maybe something that reads more like this:
Get-Childitem -Path D:\User\Home | Format-Table -Property Name | Out-File -FilePath C:\temp.txt
then some sort of ForEach-Object loop using a line at a time from C:\temp.txt to act as a variable in conjunction with Remove-Item
0
SubsunCommented:
Not so clear about the request.. Do you want to delete all appdata folders inside D:\Homes\Users? Or you you want to create a report first and then delete it using a verified report? Or are you just seeking help to understand the script?

If you need to take report and then delete the files using the report then you can try two part code...

It's very simple code, which I didn't use any alias so that it will be easy for you to understand the code..
Following code will search in D:\Homes\Users for folders named appdata and dump the report to C:\temp.txt
Get-ChildItem D:\Homes\Users -Filter "Appdata" -Recurse | Where-Object {$_.PSIsContainer}| ForEach-Object {$_.FullName}| Out-File C:\temp.txt

Open in new window

Once you confirm the folder for deletion, you can ruin the following code and it will read the folder names from C:\temp.txt and delete them..
Get-Content C:\temp.txt | ForEach-Object {Remove-Item $_ -Force -Recurse}

Open in new window

PS: let me know if you have any trouble in understanding the code..
0
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

Dead_EyesAuthor Commented:
Hi Subsun, thanks that made a lot more sense but I have run into an issue. It looks like some of the appdata subfolders have the hidden and read only folder attributes and that is preventing the first part of the script from detecting the appdata folder. for example: if I had
D:\Homes\Users\Ben\appdata
D:\Homes\Users\Sarah\appdata
D:\Homes\Users\Tom\appdata
but D:\Homes\Users\Sarah\appdata had the hidden and read only folder attributes selected
then the C:\temp txt file would read:
D:\Homes\Users\Ben\appdata
D:\Homes\Users\Tom\appdata
0
SubsunCommented:
Use -Force switch with Get-ChildItem
Get-ChildItem D:\Homes\Users -Filter "Appdata" -Recurse -Force

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
Dead_EyesAuthor Commented:
Thanks that worked great and v helpful explanations
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.

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.