customize powershell script

Hi power shell experts

we have CIFS shared file system hold the citrix profiles (( users roaming profiles ))

and our Citrix users they use CRM application and every user create TEMP data in specific location in his won profile (( please check the attached file )) and this data stay and eat a lot of space so i need to find a way to delete this data that older more than 10 days

the shared file system path like this

\\fileserver\ctx-profiles\ts-UserData$\userone\AppData\Roaming\CRM\OCR\Repository

note : userone  is the user name and this value will change per user name  

so i want script can do this daily . i searched online i found this script but i don't know how to modify this script to meet my case

can you please modify this script for me to cover my needs



#delete files and (folders(+subfolders))  on creation date.
#folder
$Now = Get-Date
$Days = "10"
$TargetFolder = "directory"
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -include *.*  -recurse -force
     Where {$_.CreationTime -le "$LastWrite"}
      foreach ($i in Get-ChildItem $TargetFolder -recurse)
{
    if ($i.CreationTime -lt ($(Get-Date).AddDays(-10)))
    {
        Remove-Item $Files -recurse -force
    }
}
      Write-Output $Files >> c:\powershell\.delete.log


thanks in advance
Sword
CRM.jpg
LVL 1
sword12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

footechCommented:
You should examine the situation and decide on some criteria.
- Do you want to look at CreationTime or the LastWriteTime, i.e. a file may have been created a year ago but is frequently modified.
- Do you want to delete just files, or folders?  There's a danger with deleting folders based on creation time, as a long-standing folder could contain new files.

Once you've identified what the risks are you can proceed to deal with them.  Here's an example of dealing with just files based on lastwritetime.
$Now = Get-Date
$Days = "10"
$TargetFolder = '\\fileserver\ctx-profiles\ts-UserData$\userone\AppData\Roaming\CRM\OCR\Repository'
$LastWrite = $Now.AddDays(-$days)
$Files = Get-ChildItem $TargetFolder -Recurse -Force -File | Where {$_.LastWriteTime -le $LastWrite}
foreach ($File in $Files)
{
    Remove-Item $File.FullName -Force
    Write-Output $File.FullName | Add-Content "c:\powershell\.delete.log"
}

Open in new window

sword12Author Commented:
Hi Footech

thank you for your answer

i want to delete the directories based on modification date . it no risk here because these type of data temp data


but my major problem how can i let the script to do this for more than 400 users

as i told you in my first post  in the path    userone  is just the user name  like max or  tomas or x or y etc  i have more than 400 users

so i need script can empty this path user after user  for example for 5 users i have like this

\\fileserver\ctx-profiles\ts-UserData$\Max\AppData\Roaming\CRM\OCR\Repository
\\fileserver\ctx-profiles\ts-UserData$\tom\AppData\Roaming\CRM\OCR\Repository
\\fileserver\ctx-profiles\ts-UserData$\hans\AppData\Roaming\CRM\OCR\Repository
\\fileserver\ctx-profiles\ts-UserData$\jojo\AppData\Roaming\CRM\OCR\Repository
\\fileserver\ctx-profiles\ts-UserData$\fabio\AppData\Roaming\CRM\OCR\Repository


so please modify the script for me to delete the directories which modified before 10 days for every user profile

thanks a lot again for your kind support


sword
Tony JohncockLead Technical ArchitectCommented:
Hi there

As I see this you have a couple of options without reverting to things like AD lookups (which you don't mention is in use or not).

The first is using a list of users and you run the script on a schedule on a server (probably the files server in question):

[/# Set up some common variables
    # set age in number of days 
    $age = "-10" 

    # get the current date 
    $date = Get-Date 

    # determine how far back we go based on current date 
    $DelDate = $date.AddDays($age) 

    # Using a list of users: 

$Users = get-content "C:\List_of_users.txt"
ForEach ($User in $Users) {
    $Path = "\\fileserver\ctx-profiles\ts-UserData$\"+$USer+"\AppData\Roaming\CRM\OCR\Repository\"
    Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DelDate } | Remove-Item -force
    }
    
  

Open in new window


Althernatively, assuming the user name in the path matches the user name when they logon, you could run it as a logon script for each user so they only delete their own folders:

# Set up some common variables
    # set age in number of days 
    $age = "-10" 

    # get the current date 
    $date = Get-Date 

    # determine how far back we go based on current date 
    $DelDate = $date.AddDays($age) 

    # Using just the currently logged in user: 

$User = [Environment]::UserName
    $Path = "\\fileserver\ctx-profiles\ts-UserData$\"+$User+"\AppData\Roaming\CRM\OCR\Repository"
    Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DelDate } | Remove-Item -force

Open in new window


Just a caveat - I've banged these out quick and haven't had time to thoroughly test so please do test before use
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

sword12Author Commented:
Hi tony 10

thank you for your answer

the first option

i have no problem to use schedule task to do this job but if you can advice me how the list of users txt file should look like . if you can just give me example for 3 users will be perfect


the second option will be amazing to have it as login script but the only problem this will daily the login and maybe this will carry direct effect for the end user

what you think


thanks again
Sword
Tony JohncockLead Technical ArchitectCommented:
Sure thing - the users file looks like this:

User1
User2
User3
...
Userx

It's literally just a list of users with one user per line in the text file.

It's been a while since I messed with optimizing login scripts. You could look into running them asynchronous which basically means subsequent scripts run without waiting for previous ones to finish but this can cause problems.

Alternatively, you could use something like the following as a cmd based login script which will launch the powershell script at login but not wait for it to complete:

start "Removing old temporary files" /DC:\ "path to powershell.exe" -File "path to .PS1 script\script.ps1

Open in new window

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
David Johnson, CD, MVPOwnerCommented:
run as scheduled task doesn't require list of users will use the directory information
# Set up some common variables
    # set age in number of days 
    $age = "-10" 

    # get the current date 
    $date = Get-Date 

    # determine how far back we go based on current date 
    $DelDate = $date.AddDays($age) 

    # Using a list of users: 

$Users = get-childitem "\\fileserver\ctx-profiles\ts-UserData$\"
ForEach ($User in $Users) {
    $Path = $user.fullname +"\AppData\Roaming\CRM\OCR\Repository\"
    Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DelDate } | Remove-Item -force
    }
    

Open in new window

sword12Author Commented:
i used this script but how can modify this script in order to let it run without asking any question
just let it delete everything under  

\\fileserver\ctx-profiles\ts-UserData$\"+$USer+"\AppData\Roaming\ITML\SBC\Repository\  


this is the out put after i issued the script

PS C:\Users\sword\Desktop> .\CRM.ps1

Confirm
The item at
Microsoft.PowerShell.Core\FileSystem::\\fileserver\ctx-profiles\ts-UserData$\TOM\AppData\Roaming\CRM\OCR\Repository\10
000235076_CRM_00 has children and the Recurse parameter was not specified. If you continue, all children will be
removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):


thank you
Sword
Tony JohncockLead Technical ArchitectCommented:
Add -force or -Confirm:$false to the end of any remove-item
sword12Author Commented:
i used this


# Set up some common variables
    # set age in number of days
    $age = "-10"

    # get the current date
    $date = Get-Date

    # determine how far back we go based on current date
    $DelDate = $date.AddDays($age)

    # Using a list of users:

$Users = get-childitem "\\fileserver\ctx-profiles\ts-UserData$\"
ForEach ($User in $Users) {
    $Path = $user.fullname +"\AppData\Roaming\CRM\OCR\Repository\"
    Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DelDate } | Remove-Item -force -Confirm:$false
    }


and i got the same

please check the attached file and advice
case.jpg
Tony JohncockLead Technical ArchitectCommented:
Remove-Item -force

OR

Remove-item -Confirm:$false

Not both
sword12Author Commented:
i test them both i got the same error

# Set up some common variables
    # set age in number of days
    $age = "-10"

    # get the current date
    $date = Get-Date

    # determine how far back we go based on current date
    $DelDate = $date.AddDays($age)

    # Using a list of users:

$Users = get-childitem "\\emc2dm\ctx-profiles\ts-UserData$\"
ForEach ($User in $Users) {
    $Path = $user.fullname +"\AppData\Roaming\ITML\SBC\Repository\"
    Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DelDate } | Remove-Item -Confirm:$false
    }


please check the attached file
powershell.jpg
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
Powershell

From novice to tech pro — start learning today.