Get Oldest File in a Directory

Posted by Drothism on Mar 17th, 2009

Looking for a quick way to get the oldest file in a directory? Here is a quick and dirty method to do it with PowerShell…
dir f:\archive -recurse| ?{!($_.PSIsContainer)} | sort -property lastwritetime | select -first 1
The ?{!($_.PSIsContainer)} filter will leave sub-directories out of the search.


Run PowerShell Scripts from Task Scheduler

Posted by Drothism on Feb 26th, 2009

Those familiar with configuring batch command scripts in the Windows Server 2008 or Windows Vista Task Scheduler may be a little perplexed in how to run PowerShell scripts from the scheduler. The main difference is in the Action form parameters…
Program/script: Set to the PowerShell.exe path. This is typically C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
Add arguments: -nolog -command “&{D:\Scripts\MyScript.ps1 Param1 Param2}”. Note that [...]


PowerShell Special Characters

Posted by Drothism on Dec 12th, 2008

Here are some of the special characters that can be used in Windows PowerShell output:

`0 — Null
`a — Alert
`b — Backspace
`n — New line
`r — Carriage return
`t — Horizontal tab
`’ — Single quote
`” — Double quote

Some of these characters are intended for use only from the Windows PowerShell prompt. For example, the special character `a [...]


Searching a Text File in PowerShell

Posted by Drothism on Sep 18th, 2008

If you are looking for a quick and simple way to search a text file for a certain string PowerShell has a very efficient method called Select-String. In its simplest form just use the following format…
if (select-string -pattern “findme” -path “D:\Scripts\test.txt”)
    Write-Host “Found It!”
In the case above I am searching the text file “text.txt” for the [...]


Creating PowerShell Exit Codes

Posted by Drothism on Sep 10th, 2008

I needed to create a PowerShell script today that would return exit codes. Simply look it up on the internet you say. Well after 30 minutes of searching I did find the trick and thought embedding on here would ensure I could find it next time it was needed…


$host is a PowerShell reserved variable so [...]