[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

powershell delete subfolders

Posted on 2014-08-17
6
Medium Priority
?
1,321 Views
Last Modified: 2014-08-17
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
0
Comment
Question by:Dead_Eyes
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40266363
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
 

Author Comment

by:Dead_Eyes
ID: 40266425
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
 
LVL 40

Expert Comment

by:Subsun
ID: 40266461
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
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 

Author Comment

by:Dead_Eyes
ID: 40266498
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
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40266503
Use -Force switch with Get-ChildItem
Get-ChildItem D:\Homes\Users -Filter "Appdata" -Recurse -Force

Open in new window

0
 

Author Closing Comment

by:Dead_Eyes
ID: 40266510
Thanks that worked great and v helpful explanations
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question