Link to home
Start Free TrialLog in
Avatar of Dead_Eyes
Dead_EyesFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of SubSun
SubSun
Flag of India image

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..
Avatar of Dead_Eyes

ASKER

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
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..
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
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks that worked great and v helpful explanations