Remove Folders with use of csv list

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

 

 

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\

 

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"

 

 

but i think i made a fault with

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

any Idea ?

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

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.

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.

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

Thanks

[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

 

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.

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

 

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.