We’re not all the same, but if we were, and you were like me, you’d have a huge number of tabs open in your browser of choice. Each of them, would have some relation to PowerShell and each would be sitting by idle, and waiting to be read. No idea where it came from, but in one of them last week, I saw this:
Get-Date -Format o | foreach {$_ -replace ":", "."}
I stared at it for a moment, and thought, why is someone piping Get-Date to foreach? Get-Date only returns a single value. Why would that need to be handed off to a looping construct? The point here is, it wouldn’t need to be, even though it works. I’ve decided I should bring this up, in case someone else is potentially going to make this same mistake. Even if you’re a Systems Administrator, you’re still going to want to write efficient, and well thought out code. Here’s how I would have expected to see this written:
(Get-Date -Format o) -replace ":", "."
Again, Get-Date is only going to provide a single returned value, and therefore, we can trust that we don’t need to loop through a set of results. It’s cleaner code, it’s tighter code, and it gives the rest of us some confidence that you’ve thought things through.
One of the things I enjoying doing is testing the speed of various ways to do the same thing. Don’t think for a minute that I didn’t do that here. This next example indicates the time to run the foreach version of this Get-Date command. The second example, indicates the time to run when foreach is not used.
Milliseconds Ticks ------------ ----- 1 19094 1 10543 0 7044 0 4212 0 3944 0 3849 0 4133 0 4349 0 4099 0 3948
Milliseconds Ticks ------------ ----- 1 12386 0 5277 0 4438 0 2479 0 2345 0 2325 0 4003 0 2352 0 2335 0 2311
The times are close; they’re indistinguishable to us humans, but the numbers don’t lie. There’s a better way based both on time to complete, and competency. If you wanted to see it, here’s what I used to measure these two different commands. Keep these things in mind, and keep on learning!
1..10 | ForEach-Object { Measure-Command -Expression { Get-Date -Format o | foreach {$_ -replace ":", "."} } | Select-Object -Property Milliseconds,Ticks } 1..10 | ForEach-Object { Measure-Command -Expression { (Get-Date -Format o) -replace ":", "." } | Select-Object -Property Milliseconds,Ticks }