Back again with another example to share with the Windows PowerShell newbies. They’re out there: people that don’t know the practical power in PowerShell. The idea is to use real-world examples of things we simply wouldn’t want to do manually, in hopes that our lost friends will find the light. Maybe, that’s you.
Part I can be found here. In that post, we learned that we could use PowerShell to create 10,000 folders in about 10 seconds — that’s 1,000 folders per second, or 1 folder each millisecond.
Part II
What we’ll do today is read in the content of a file, sort the items we’ve read in, and then write our sorted results back out to the same file — it’s instant alphabetizing. Let’s start by reading in our file and see what we’re starting with.
PS> Get-Content -Path .\file.txt web02.mydomain.com web09.mydomain.com dc03.mydomain.com dc04.mydomain.com sql08.mydomain.com sql06.mydomain.com dc08.mydomain.com sql04.mydomain.com dc10.mydomain.com web01.mydomain.com web03.mydomain.com dc09.mydomain.com web05.mydomain.com web06.mydomain.com dc02.mydomain.com web07.mydomain.com dc05.mydomain.com web04.mydomain.com web10.mydomain.com sql01.mydomain.com dc01.mydomain.com sql03.mydomain.com sql10.mydomain.com web08.mydomain.com sql05.mydomain.com sql07.mydomain.com dc07.mydomain.com sql02.mydomain.com sql09.mydomain.com dc06.mydomain.com
Our file contains 30 fully-qualified servers names that are in no significant order. Sorting all 30 of these server names is probably going to be difficult, or at least, it’s going to be time consuming. Wrong. With one quick addition to the previous command — see the example below — our list is sorted and without any noticeable difference in the amount of time it took to complete.
PS> Get-Content -Path .\file.txt | Sort-Object dc01.mydomain.com dc02.mydomain.com dc03.mydomain.com dc04.mydomain.com dc05.mydomain.com dc06.mydomain.com dc07.mydomain.com dc08.mydomain.com dc09.mydomain.com dc10.mydomain.com sql01.mydomain.com sql02.mydomain.com sql03.mydomain.com sql04.mydomain.com sql05.mydomain.com sql06.mydomain.com sql07.mydomain.com sql08.mydomain.com sql09.mydomain.com sql10.mydomain.com web01.mydomain.com web02.mydomain.com web03.mydomain.com web04.mydomain.com web05.mydomain.com web06.mydomain.com web07.mydomain.com web08.mydomain.com web09.mydomain.com web10.mydomain.com
Yeah, that was tough. I ran this same command 10 times, measuring it with the Measure-Command cmdlet, so I could see the average amount of time it took to sort the list of computers. It completed this “challenge” in anywhere between 6 and 10 milliseconds. I’m sorry, but it would’ve taken me at least a few minutes do to this by hand.
Now that we know we can sort it, let’s write it back to disk. In this example, below, we’ll put our sorted results back into the file where it came from, overwriting the original contents. If you don’t want to do that, then be sure to alter the file name value for Set-Content’s -Path parameter.
PS> Get-Content -Path .\file.txt | Sort-Object | Set-Content -Path .\file.txt PS>
Let’s verify that our file now contains the sorted contents by rerunning our first Get-Content command from the beginning of this post.
PS> Get-Content -Path .\file.txt dc01.mydomain.com dc02.mydomain.com dc03.mydomain.com dc04.mydomain.com dc05.mydomain.com dc06.mydomain.com dc07.mydomain.com dc08.mydomain.com dc09.mydomain.com dc10.mydomain.com sql01.mydomain.com sql02.mydomain.com sql03.mydomain.com sql04.mydomain.com sql05.mydomain.com sql06.mydomain.com sql07.mydomain.com sql08.mydomain.com sql09.mydomain.com sql10.mydomain.com web01.mydomain.com web02.mydomain.com web03.mydomain.com web04.mydomain.com web05.mydomain.com web06.mydomain.com web07.mydomain.com web08.mydomain.com web09.mydomain.com web10.mydomain.com
That’s it. We ran a one-liner command to read in some data, sort it, and push it right back to the same file. So we were able to compare, I went ahead and sorted the same, 30-line file manually. It took me 3 minutes and 30 seconds (proof below). While this wasn’t the most complex, or lengthy document one might sort, the time difference is still quite drastic. PowerShell has plenty to offer, and spending some time to learn it is going to pay off. This line of work requires all the time you have to continue to learn, and stay relevant. PowerShell can give you some time back.
PS C:\> $StartTime = Get-Date PS C:\> $EndTime = Get-Date PS C:\> New-TimeSpan -Start $StartTime -End $EndTime Days : 0 Hours : 0 Minutes : 3 Seconds : 30 Milliseconds : 860 Ticks : 2108600828 TotalDays : 0.00244051021759259 TotalHours : 0.0585722452222222 TotalMinutes : 3.51433471333333 TotalSeconds : 210.8600828 TotalMilliseconds : 210860.0828