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-UserDat a$\userone \AppData\R oaming\CRM \OCR\Repos itory
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
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\
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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):
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:
Just a caveat - I've banged these out quick and haven't had time to thoroughly test so please do test before use
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
}
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
Just a caveat - I've banged these out quick and haven't had time to thoroughly test so please do test before use
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
}
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-UserDat a$\"+$USer +"\AppData \Roaming\I TML\SBC\Re pository\
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 ::\\filese rver\ctx-p rofiles\ts -UserData$ \TOM\AppDa ta\Roaming \CRM\OCR\R epository\ 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
just let it delete everything under
\\fileserver\ctx-profiles\
this is the out put after i issued the script
PS C:\Users\sword\Desktop> .\CRM.ps1
Confirm
The item at
Microsoft.PowerShell.Core\
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
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-UserDa ta$\"
ForEach ($User in $Users) {
$Path = $user.fullname +"\AppData\Roaming\CRM\OCR \Repositor y\"
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
# 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
ForEach ($User in $Users) {
$Path = $user.fullname +"\AppData\Roaming\CRM\OCR
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
OR
Remove-item -Confirm:$false
Not both
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\SB C\Reposito ry\"
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DelDate } | Remove-Item -Confirm:$false
}
please check the attached file
powershell.jpg
# 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-
ForEach ($User in $Users) {
$Path = $user.fullname +"\AppData\Roaming\ITML\SB
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DelDate } | Remove-Item -Confirm:$false
}
please check the attached file
powershell.jpg
ASKER
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\
\\fileserver\ctx-profiles\
\\fileserver\ctx-profiles\
\\fileserver\ctx-profiles\
\\fileserver\ctx-profiles\
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