Why this blog...
I've been writing powershell - off and on - almost since its inception. I discovered it when one of the architects at work offered to do a session on this sparkly new scripting language, back in ...
Oh, I can't remember when. But I've got all my powershell code from those days, so maybe I can quickly check for the oldest bit of powershell I've got. I've actually got all that code in subfolders of C:\Projects
dir ..\*\*.ps1 | Sort-Object -Property lastwritetime | Select-Object -First 1
That's a bit scummy, because I've used the old MS-DOS alias dir
(which I wouldn't ever use in production code, of course) so that there's some familiarity for folks new to powershell.
I've used a pipeline, because powershell traces its ancestry back to the Unix Korn Shell (which I used as soon as I outgrew the Bourne Shell) and that's part of its beauty - simple commands that do one thing well, that you can link together. Unlike the Korn Shell, powershell works with objects, so what's going on is:
Step 1 - get a list of all my powershell scripts:
dir ..\*\*.ps1
The output of that is huge, so we need to sort it and filter it. Let's sort it by time modified:
dir ..\*\*.ps1 | Sort-Object -Property lastwritetime
The objects that dir
produces are of type FileInfo
, which is derived from the .NET System.IO.FileSystemInfo
class. So I can sort them by the property LastWriteTime
, which corresponds to the last modification time. Production code, I'd write with the proper, cased name, but for my Windows workstation, writing a throwaway one-liner, I won't bother.
And the final step should be obvious. The Sort-Object
cmdlet sorts in ascending order by default, so the oldest will be first. That's where Select-Object
comes in:
dir ..\*\*.ps1 | Sort-Object -Property lastwritetime | Select-Object -First 1
That -First 1
parameter keeps just the oldest record. All the other objects that have travelled though the pipeline this far are discarded.
So what was that oldest script, and when did I write it?
Directory: C:\Projects\WilliamChTraining
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 09/08/2011 09:17 223 get1.ps1
There we go. I've been writing powershell since August 2011.
What was it? It turns out it was a function to get an active directory user object from the User Principal Name.
Huh? So what?
And that was pretty much my reaction back then, because I could think of no reason why I'd need to find an active directory user object.
Times change, and in the years 2017-2022 I have queried active directory user objects more times than I can count. Just not that way, given that it's a one-liner in powershell v5, though back in 2011 it was more complex.
For the sake of tidiness, let's revisit that awful dir
. The better way would be:
Get-ChildItem .. -Include "*.ps1" -Recurse
dir
is an alias for Get-ChildItem
. In tidying it, I've switched to a proper recursive search, starting with the parent folder of my current folder, and looking for all files that match the pattern '*.ps1'. That's subtly different from my original dir
alias, which was very much more DOS-like, for the sake of familiarity.
And so, getting back on track, I'll be using this blog to cover how my use of powershell has grown over the years, using practical examples of code that I'm still using and developing.
Most likely it'll be tied into something I'm doing for personal interest or for work - the lines blur. I'll certainly be referencing some other tools that are part of my standard toolkit - regular expressions, APIs (particularly REST APIs) and Postman - and the Powershell ISE (debugger/IDE, whatever you call it).
Thank you for your patience.