For System admin, Server Disk/drive management is one
of the tedious task when he manages hundreds of servers. System admin deletes
temporary files, log files, backup files, and junk files etc. to free disk
space when he face disk full issue.
Windows
PowerShell is powerful tool which can be used in
combination with scripts. Suppose you want to delete 10
days older files from a folder (say C:\IISLogs) recursively (it goes subfolders
recursively). Copy below script in notepad and save with “ps1” file extension (e.g. DelOlderFiles.ps1 saved in folder C:\script).
$a = Get-ChildItem
"C:\IISLogs" -recurse
foreach($x in $a)
{
$y
= ((Get-Date)
- $x.LastWriteTime).Days
if
($y -gt
10 -and $x.PsISContainer -ne
$True)
{
$x.Delete()
}
}
|
Execute
following command in PowerShell console to change Executions Policy as “RemoteSigned”.
Set-ExecutionPolicy
RemoteSigned
To run script from PowerShell console, go to the folder (C:\script)
and run the script as follow,
PS C:\script>
.\ DelOlderFiles.ps1
How to
schedule script with Windows Task scheduler?
Create task. Don’t forget to select “Run whether user is logged on or
not” option.
Schedule
task.
Define
action as follows.
Save Task.
You have done!
No comments:
Post a Comment