powershell delete subfolders

Posted on 2014-08-17
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
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
Question by:Dead_Eyes
    LVL 40

    Expert Comment

    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 = $_
    	Remove-Item "D:\Homes\Users\$user\Appdata" -Force -Recurse -EA Stop
    	Write-Host "Error Deleting $user Data"

    Open in new window

    Sample input file..

    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..

    Author Comment

    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
    LVL 40

    Expert Comment

    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..

    Author Comment

    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
    but D:\Homes\Users\Sarah\appdata had the hidden and read only folder attributes selected
    then the C:\temp txt file would read:
    LVL 40

    Accepted Solution

    Use -Force switch with Get-ChildItem
    Get-ChildItem D:\Homes\Users -Filter "Appdata" -Recurse -Force

    Open in new window


    Author Closing Comment

    Thanks that worked great and v helpful explanations

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Group Policy Delegation Recently, I was tasked with delegating permissions for a specific group of administrators to a specific list of Group Policy Objects(GPO). A couple of things need to happen here for this to work. If you want the Administrat…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now