0

I`m a beginner PowerShell user.

I need a powershell script. It must do:

In

\\routefolders\xxxxx
\\routefolders\jhon
\\routefolders\mike
\\routefolders\sandra
.
.
.
etc...

find files newer than 2 years

if exist some file in \routefolders\xxxxx "XXXXX" (searching in ALL SUBFOLDERS) do nothing

if there is no file newer than 2 years print in a file and delete the folder and subfolders \routefolders\xxxxx

The "XXXXX" folders are user-personal folders, if the user not use this i want to save a log and then to delete this folder

I have this, to delete a specific folder. I need some PS like this:

$RootDir="c:\temp\test"
$Users = Get-ChildItem $RootDir -Name
$FolderToDelete = "subcarpeta1"


foreach ($user in $Users){

  $n = $n +1
  write-host $n $user

  #Remove-Item -path $rootdir\$user\$FolderToDelete -Force -Recurse  
} 
1

1 Answer 1

0

You could try something like this:

function Safely-Delete($fileItem)
{ 

    if($fileItem.FullName.StartsWith("your path"))
    {           
        Set-Location $fileItem.FullName
        Write-Host "Removing Item: $fileItem"
        Remove-Item $fileItem.FullName -Recurse 
    }
}

function Remove-File($path, $ageInYears)
{
    foreach($childItem in Get-ChildItem -Path $path)
    {
        if($childItem.Lastwritetime -lt (date).AddYears(-$ageInYears))
        {
            Safely-Delete $childItem
        }
    }
}

$age = 2
Remove-File "your path" $age
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.