user3653189
user3653189

Reputation: 31

Powershell remove items from specific folder in path (for all users)

So I have this script. I enter a users username and it goes and looks in the given location using that username to remove all files fine the end directory.

I now need to schedule this for all users now I have proved it works. How can i get this to go through all user folders in given location and then through the path to the end directory to remove the files. Not sure how I can modify this to remove the need for the users username.

Any ideas?

"`n" 
$user = Read-Host "Enter Username"
write-Host "Finding" -ForegroundColor Green
$filePath = "\\myserver\d$\userprofiles\$user\AppData\Roaming\app\app2\app3\$user" 
"`n" 

$Response = Read-Host "Do you want to delete the contents of the app directory for '$user' ?(Y/N)"
if($Response -eq "Y"){
   get-childitem  $filepath -recurse | foreach ($_) {remove-item -verbose $_.fullname} 
} else{
Write-Host "No such user found or directory does not exist..."
}

write-Host "" 
write-Host "------------Process Complete Files Removed--------------------" -ForegroundColor DarkGreen

Thanks in advance.

Upvotes: 0

Views: 5447

Answers (1)

Tim Ferrill
Tim Ferrill

Reputation: 1674

Get-ChildItem "\\myserver\d$\userprofiles" -Exclude "Default" | ForEach-Object {
    $path = $_.FullName + "\AppData\Roaming\app\app2\app3\" + $_.Name + "\*"
    Remove-Item $path -Recurse
    }

Upvotes: 2

Related Questions