Powershell script fails to remove items on network share


Have a powershell script that would like to run on a shared folder to delete files older than 30days, however getting access denied on most of the folders and need to get rid of them but cant get the script to remove them even when changing the parameters to DELETE instead of Remove-Item


Here is the script


#—– define parameters —–#
#—– get current date —-#
$Now = Get-Date
#—– define amount of days —-#
$Days = "30"
#—– define folder where files are located —-#
$TargetFolder = "Share Name"
#—– define extension —-#
$Extension = "*.*"
#—– define LastWriteTime parameter based on $Days —#
$LastWrite = $Now.AddDays(-$Days)
#—– get files based on lastwrite filter and specified folder —#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
#—– E-mail parameters
$Messageparameters = @{                        
    Subject = "Deleted Files From Sharefolder"
    From = "x@x.com"                        
    To = "x@x.com"                  
    SmtpServer = "x.com"                  

if ($Files -ne $NULL)
        foreach ($File in $Files) {
                write-host "Removing file $File" -ForegroundColor "DarkRed"
                Remove-Item $File.FullName
                $Files2 = $Files | Out-String
           Write-Host "No (more) Files to remove!" -foregroundcolor "Green"
           $Files2 = "No files have been removed today!" | Out-String
Send-MailMessage @messageParameters -Body $Files2


Thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CDRetiredCommented:
getting access denied on most of the folders In order to do anything on a file/folder you need the appropriate permissions.. getting access denied means that the account that is doing the process doesn't have the appropriate permissions.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sydlegAuthor Commented:
Yes, thanks understood,

I guess what im asking is, is there a way for the script to take full ownership aswell >??
David Johnson, CDRetiredCommented:
you would need to have the 'full control' attribute.

Have you tried running the script as, say, domain admin?

Does that work?

David Johnson, CDRetiredCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.