Link to home
Start Free TrialLog in
Avatar of sword12
sword12

asked on

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
SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sword12
sword12

ASKER

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
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
Avatar of sword12

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of sword12

ASKER

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
Add -force or -Confirm:$false to the end of any remove-item
Avatar of sword12

ASKER

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
Remove-Item -force

OR

Remove-item -Confirm:$false

Not both
Avatar of sword12

ASKER

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