I had a request recently to provide an inactive user report for the past 60 days. Basically, find out which accounts have not logged in for the past 60 days so action can be taken against them. The request was for a multi domain forest which queries every domain controller and gets the latest lastlogon…
Tag: automation
Deploying the SCCM Client with VMware Client Windows Guest Customization
Since SCCM is our configuration management tool of choice, the SCCM client needs to get installed on all of our newly provisioned VMs. I created a service account that only has read permission to the \\sccmserver\sms_sitecode\client share on the SCCM server. The client is installed from this location to ensure that we are always using the latest…
Delete Files and Folders Older Than X Days
Often times admin have to creates tasks like removing log files or some other files on a regular schedule. Here is an automated way of removing files / folders older than X days. Create a Batch file or Powershell script and add it to scheduled task. Batch File:
1 2 3 4 5 6 7 8 9 10 11 12 |
@echo off :: set folder path set dump_path=c:\shares\dump :: set min age of files and folders to delete set max_days=7 :: remove files from %dump_path% forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q @path" :: remove sub directories from %dump_path% forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF @isdir == TRUE rd /S /Q @path" |
Powershell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# set folder path $dump_path = "C:\shares\dump" # set min age of files $max_days = "-7" # get the current date $curr_date = Get-Date # determine how far back we go based on current date $del_date = $curr_date.AddDays($max_days) # delete the files Get-ChildItem $dump_path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item |