Tag Archive: powershell

delete_key_eraser_inhandRecently I’ve found myself in a situation where I had to periodically delete files older than 7 days. I could have done it manually, but being busy, I often forgot to do it. The script I’m about to show you is a bit modified than the original, but, the principle is the same. Here I’ve added a posibilty to manually enter the directory name. In your script, you can define it, so it is being run automatically, without your intervention.

Here’s the code :

Function GetOldFile
{
param ($Dir = $(Read-host "Unesi putanju do direktorija")),
($Days = $(Read-Host "Brišem fileove starije od koliko dana?"))
$TargetFolder = $Dir
if (Test-Path $TargetFolder)

{
Write-host "DIREKTORIJ JE :" $TargetFolder -foregroundcolor "Red"
#Write-Host `a `a `a `a `a - odkomentirati ako zelimo da pc speaker vristi :)
Write-Host "UPS, krivi folder! Stisni 'Ctrl + C' za prekid - imas 5 sekundi za prekid" -foregroundcolor "Yellow"
Start-sleep -s 5
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -include *.log -recurse |Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{write-host "Brišem file $File" -foregroundcolor "Red"; Remove-Item $File | out-null}
}
Else
{Write-Host "Direktorij $TargetFolder ne postoji! Provjeri path!"}
}
GetOldFile

The questions in the script are in Croatian, but anyone with a little bit of programming experience will be able to change it, because the script is pretty straight forward. But, if you’re lazy as most of us are, here is the translated script :)

Read More »

cat1Working as a Windows server administrator many times I found myself in a situation where I have to check 50+ Mb log file for only one e-mail address or some other term. Ofcourse, many of you are going to think: “Yeah, like that’s a problem, just press CRTL+F and type in the term :) ” ,and you’re right. That way rocks when you have 10, or maximum 20 entries where that term is mentioned ( i.e some e-mail address) in the entire log file .  But, what to do when you’re in a situation that a client wants to see the logs for his entire domain. All sent e-mail, all incoming e-mail, everything…then, it gets a little too much to do “crtl+f”  and copy/paste the result in some other file which you are going to send to the client.  Linux users have their way to do that, simply by : cat filename.txt | grep term > outputfile.txt .  By starting to explore powershell, my first task was to find a similar command to do the same thing on a Windows server. Doing a little searching on the web I found the solution that is really simple and similar to linux command :) hooray for me :)

Read More »