# Remove Folders with use of csv list

**URL:** <https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490>\
**Category:** PowerShell Help\
**Created:** [January 17, 2021, 9:35pm UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490 "2021-01-17T21:35:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![prinske](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/prinske/32/10_2.png) [@prinske](https://forums.powershell.org/u/prinske)\
**Post date:** [January 17, 2021, 9:35pm UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/1 "2021-01-17T21:35:15Z")

</div>

The idea.

I took out my AD a list of Disabled users .

```
Get-ADUser -Filter * -Properties SamAccountName | where Enabled -EQ $false | ? {$_.DistinguishedName -like "*,OU=disabled accounts,*"} 
| select SamAccountName | Export-CSV "C:\Temp\Disabled users\Disabled_users.csv
```

&nbsp;

&nbsp;

Now i would like to use this list with USERID’s to delete there folder with there userId on a Global location.

For example they have there folder with userid name on C:\Temp\profiles\

&nbsp;

The script that i was trying to use:

```
$files = Get-Content "C:\Temp\Disabled users\Disabled_users.csv"

foreach ($file in $files) {
Remove-Item -Path C:\temp\profiles\$file -Force
}

Write-Host -foregroundcolor yellow "Delete action complete"
```

&nbsp;

&nbsp;

but i think i made a fault with

```
Remove-Item -Path C:\temp\profiles\$file -Force
```

any Idea ?

---

<div class="post-metadata">

**Author:** ![kardock](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@kardock](https://forums.powershell.org/u/kardock)\
**Post date:** [January 18, 2021, 2:28am UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/2 "2021-01-18T02:28:18Z")

</div>

if the folder has child folders, use -recurse to delete it.

---

<div class="post-metadata">

**Author:** ![prinske](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/prinske/32/10_2.png) [@prinske](https://forums.powershell.org/u/prinske)\
**Post date:** [January 18, 2021, 2:57am UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/3 "2021-01-18T02:57:48Z")

</div>

that is not my issue … I have userid’s listed in CSV that i would like to use as source to delete folders named as those userID’s  
Every user in the company have a folder on the server named as there userid.

---

<div class="post-metadata">

**Author:** ![pwiggers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwiggers/32/1568_2.png) [@pwiggers](https://forums.powershell.org/u/pwiggers)\
**Post date:** [January 18, 2021, 4:30am UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/4 "2021-01-18T04:30:34Z")

</div>

For some reason my previous reply has gone missing…

I have to agree with Kardock, you need to specify the -Recurse switch to delete profile folders which have child-items (as most profile folders do). If you do not, you will be asked to delete the folder and it’s child-items which is something you might not want. If you change your script to this it should work just fine.

```
$files = Get-Content "C:\Temp\Disabled users\Disabled_users.csv"

foreach ($file in $files) {
Remove-Item -Path C:\temp\profiles\$file -Force -Recurse
}

Write-Host -foregroundcolor yellow "Delete action complete"
```

Please let us know if this is what you are looking for and if we can help you with anything else.

---

<div class="post-metadata">

**Author:** ![faris](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/faris/32/18_2.png) [@faris](https://forums.powershell.org/u/faris)\
**Post date:** [January 18, 2021, 10:51am UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/5 "2021-01-18T10:51:49Z")

</div>

> [@](#):
>
> but i think i made a fault with

Hi all  
Would you please tell us more about the fault, is it an error or what’s going on

Thanks

---

<div class="post-metadata">

**Author:** ![prinske](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/prinske/32/10_2.png) [@prinske](https://forums.powershell.org/u/prinske)\
**Post date:** [January 18, 2021, 9:17pm UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/6 "2021-01-18T21:17:29Z")

</div>

[quote quote=286363]For some reason my previous reply has gone missing…

I have to agree with Kardock, you need to specify the -Recurse switch to delete profile folders which have child-items (as most profile folders do). If you do not, you will be asked to delete the folder and it’s child-items which is something you might not want. If you change your script to this it should work just fine.

PowerShell

\<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick"\>$files = Get-Content "C:\Temp\Disabled users\Disabled\_users.csv"

foreach ($file in $files) {  
Remove-Item -Path C:\temp\profiles$file -Force -Recurse  
}

Write-Host -foregroundcolor yellow “Delete action complete”\</textarea\>

| 

1

2

3

4

5

6

7

 | 

$files = Get-Content "C:\Temp\Disabled users\Disabled\_users.csv"

foreach ($file in $files) {

Remove-Item -Path C:\temp\profiles\$file -Force -Recurse

}

Write-Host -foregroundcolor yellow "Delete action complete"

 |

Please let us know if this is what you are looking for and if we can help you with anything else.

[/quote]

i tried with the -recurse but like i set even before i was not prompt for deletion.  
For me there is a path issue .  
As in the csv it is a userid and not a c:.… path

error after adding -recurse:

```
Remove-Item : Illegal characters in path.
At C:\Users\jpri\Documents\delpro.ps1:4 char:1
+ Remove-Item -Path C:\temp\profiles\$file -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\temp\profiles\"geve":String) [Remove-Item], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![prinske](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/prinske/32/10_2.png) [@prinske](https://forums.powershell.org/u/prinske)\
**Post date:** [January 18, 2021, 9:58pm UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/7 "2021-01-18T21:58:26Z")

</div>

> [@](#):
>
> > [PWiggers wrote:](https://powershell.org/forums/topic/remove-folders-with-use-of-csv-list/#post-286363)
> > For some reason my previous reply has gone missing…
> > 
> > I have to agree with Kardock, you need to specify the -Recurse switch to delete profile folders which have child-items (as most profile folders do). If you do not, you will be asked to delete the folder and it’s child-items which is something you might not want. If you change your script to this it should work just fine.
> > 
> > PowerShell
> > 
> > \<textarea class=”urvanov-syntax-highlighter-plain print-no” style=”tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;” readonly=”readonly” data-settings=”dblclick”\>$files = Get-Content “C:\Temp\Disabled users\Disabled\_users.csv”
> > 
> > foreach ($file in $files) {
> > 
> > Remove-Item -Path C:\temp\profiles$file -Force -Recurse
> > 
> > }
> > 
> > Write-Host -foregroundcolor yellow “Delete action complete”\</textarea\>
> > 
> > | 
> > 
> > 1
> > 
> > 2
> > 
> > 3
> > 
> > 4
> > 
> > 5
> > 
> > 6
> > 
> > 7
> > 
> > | 
> > 
> > $files = Get-Content “C:\Temp\Disabled users\Disabled\_users.csv”
> > 
> > foreach ($file in $files) {
> > 
> > Remove-Item -Path C:\temp\profiles\$file -Force -Recurse
> > 
> > }
> > 
> > Write-Host -foregroundcolor yellow “Delete action complete”
> > 
> > |
> > 
> > Please let us know if this is what you are looking for and if we can help you with anything else.
> 
> i tried with the -recurse but like i set even before i was not prompt for deletion.
> 
> For me there is a path issue .
> 
> As in the csv it is a userid and not a c:\…. path
> 
> error after adding -recurse:
> 
> PowerShell
> 
> \<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick"\>Remove-Item : Illegal characters in path. At C:\Users\jpri\Documents\delpro.ps1:4 char:1 + Remove-Item -Path C:\temp\profiles\$file -Force -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (C:\temp\profiles\"geve":String) [Remove-Item], ArgumentException + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand\</textarea\>
> 
> | 
> 
> 1
> 
> 2
> 
> 3
> 
> 4
> 
> 5
> 
> 6
> 
> | 
> 
> Remove-Item : Illegal characters in path.
> 
> At C:\Users\jpri\Documents\delpro.ps1:4 char:1
> 
> + Remove-Item -Path C:\temp\profiles\$file -Force -Recurse
> 
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> + CategoryInfo : InvalidArgument: (C:\temp\profiles\"geve":String) [Remove-Item], ArgumentException
> 
> + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand
> 
> |

I think what i need is a way to locate the path and use csv to get the exact subfolder deleted .

In my csv i only have an extraction of samAccountName.

---

<div class="post-metadata">

**Author:** ![faris](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/faris/32/18_2.png) [@faris](https://forums.powershell.org/u/faris)\
**Post date:** [January 18, 2021, 10:46pm UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/8 "2021-01-18T22:46:42Z")

</div>

Based on what I see, you got a bit other issues, the path is passed to the FileSystem Driver with the double-quote **C:\</span\>temp\</span\>profiles\</span\>_“geve”:String_**

This is wrong and cannot be parsed, I am sure that the folder itself don’t include a “” marks

You will need to remove the “” by during parsing the folders

Can you please post a sample of the CSV.

Thanks

&nbsp;

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [January 19, 2021, 3:27am UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/9 "2021-01-19T03:27:28Z")

</div>

When you use Export-CSV the way you have, your list of usernames is going to look like this:

#TYPE \<object type\>  
“sAMAccountName”  
 “bob”  
“fred”  
“julie”  
“jane”

In version 7, the type information doesn’t appear by default (yay!) and you can omit the quotes by specifying -UseQuotes Never. If you don’t have version 7 available, because you only have one column and as you’re using Get-Content rather than Import-CSV (i.e. treating your data as String objects rather than the custom objects that Import-CSV generates) I would suggest you just export your data to a text file with Out-File or Set-Content.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/remove-folders-with-use-of-csv-list/15490/10 "2024-05-16T20:28:00Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


