Probably like many of you I keep almost all of my scripts in a single location. I'm also usually working on multiple items at the same time. Some times I have difficult remembering the name of a script I might have been working on a few days ago that I need to return to. The concept is simple enough: search my script directory for PowerShell files sorted by the last write time and look for the file I need.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
That's a lot to type so why not build a function to do the work for me? In fact, since I spend a lot of time in the PowerShell ISE, why not produce graphical output? The easiest way is to pipe results to Out-Gridview. So after a little tinkering, I came up with Get-LatestScript.
Function Get-LatestScript {
[cmdletbinding()]
Param(
[Parameter(Position=0)]
[ValidateScript({Test-Path $_})]
[string]$Path=$global:ScriptPath,
[Parameter(Position=1)]
[ValidateScript({$_ -ge 1})]
[int]$Newest=10
)
if (-Not $path) {
$Path=(Get-Location).Path
}
#define a list of file extensions
$include="*.ps1","*.psd1","*.psm1","*.ps1xml"
Write-Verbose ("Getting {0} PowerShell files from {1}" -f $newest,$path)
#construct a title for Out-GridView
$Title=("Recent PowerShell Files in {0}" -f $path.ToUpper())
Get-ChildItem -Path $Path -Include $include -Recurse |
Sort-Object -Property lastWriteTime -Descending |
Select-Object -First $newest -Property LastWriteTime,CreationTime,
@{Name="Size";Expression={$_.length}},
@{Name="Lines";Expression={(Get-Content $_.Fullname | Measure-object -line).Lines}},
Directory,Name,FullName |
Out-Gridview -Title $Title
}
I decided to try something different with the Path variable. I set the default to a global variable, ScriptPath. The idea is that in your PowerShell profile, you'll have a line like this:
$ScriptPath="C:\Scripts"
If the function finds this variable, it will use it. Otherwise it will use the current location. Notice I'm also using a validation attribute to verify the path. By default the function returns the 10 newest PowerShell files, based on the last write time. The number of files can be controlled by the -Newest parameter.
In the heart of the script is a one-line expression to find all matching files in the script folder and subfolders.
Get-ChildItem -Path $Path -Include $include -Recurse |
Sort-Object -Property lastWriteTime -Descending
These files are then sorted on the LastWriteTime in descending order and then I only select the first $newest number of files.
| Sort-Object -Property lastWriteTime -Descending |
Select-Object -First $newest ...
I am only interested in a few file properties so I select them. I also add a custom property to measure the file and get the number of lines in the script.
...-Property LastWriteTime,CreationTime,
@{Name="Size";Expression={$_.length}},
@{Name="Lines";Expression={(Get-Content $_.Fullname | Measure-object -line).Lines}},
Directory,Name,FullName
Finally the results are piped to Out-Gridview.
... | Out-Gridview -Title $Title
For now, I have to manually open the file. Perhaps I'll create a WinForm or use ShowUI to integrate it into the PowerShell ISE.
You can download Get-LatestScript which includes comment based help.
v3
Out-Gridview -Title $Title -PassThru | % {$_; ise $_.FullName}
PowerShell v3 certainly adds some nice features like this.