The Surly Admin

Father, husband, IT Pro, cancer survivor

Read Text Files Faster than Get-Content

This was a fun little script I threw together after a particular conversation came up at Spiceworks.  If you’ve worked with PowerShell long you’ve used Get-Content to read a file.  99% of the time, it’s fine and you just continue on with life.  This blog post is about that 1% when Get-Content is SLOW.  The .NET IO.Streamreader is where people turn to speed things up so I decided to create a  function around it that worked much like Get-Content does.  This is it’s story.

Continue reading

June 1, 2015 Posted by | Powershell - Performance | , , , | Leave a comment

Dynamic Properties in Objects…. and performance!

So last post I talked about dynamic properties in objects, and it was pretty cool.  Then a crazy thing happened, that same colleague who was playing around with a faster Get-ChildItem project?  He decided to get some real work done and began working on a Office365 script but the Get-MailboxStatistics cmdlet is a little bit different with O365 in the TotalItemSize property is deserialized and pretty much only has a lousy string output.  So how to get the raw number without the ToMB() method?  A little Googling and we found an Exchange blog post using RegEx to strip out all the extra crud, and a dynamic property!  In the years I’ve been using PowerShell I’ve never once seen a dynamic property and the very day I decided to learn it we actually found a real life usage!  What are the odds?  Anyway, the twist was the Exchange team was applying the Add-Member to an array of objects.  I didn’t realize you could do that.  And it worked!

Continue reading

January 27, 2015 Posted by | Powershell - Performance | , | Leave a comment

Getting Directory Information Fast

By now, you may have noticed I’m always on the lookout for better performing code.  This has turned out to be a good habit now that I’m working at athena health, as the pure scale of things is so much larger than places I’ve been at in the past.  One piece I’ve never been able to speed up, though, is iterating through folders and files.   Nicolas1847, a PowerShell scripter on Spiceworks, has come up with an ingenious method to get simple directory information using Robocopy (of all things), and a colleague at athena health likes to shell out to CMD.exe and use the old DIR command.  But are they faster?  And if so, which one?

Continue reading

August 4, 2014 Posted by | Powershell - Performance | , , , | Leave a comment

Multithreading Revisited – Using Jobs

I wrote about multithreading using Runspace here, but I also wanted to talk about running them the Powershell way using jobs.  I want to make sure I give credit where it’s due, because most of my work in Jobs is based on Ryan Witschger‘s work.  He writes a great Powershell blog called (great site name, too).  Anyway, read on for my riff on Powershell jobs and the 3 step process you need to use them.

Continue reading

March 4, 2013 Posted by | PowerShell, Powershell - Performance | , , , , , , | 12 Comments

Objects and Hashtables

I expect, if you got the right group of people together, you could have a good old Mac vs PC style argument over the use of Powershell Objects (PSObjects) and hashtables.  And I’ll be honest, while I’ve used hashtables a lot for splatting I’ve used them very little for anything else.  Time to look at the two and figure out which is better, once and for all!

Continue reading

February 21, 2013 Posted by | Powershell - Best Practices, Powershell - Performance | , , , | 4 Comments

Multithreading Powershell Scripts

In your scripting journey there will come a time that you have a script that is simply running too long. Perhaps you’re wanting to gather information hourly and the script is taking two hours to run. Maybe you’re a consultant and need a discovery script to run as fast as possible so you can get out of there? Whatever it is at some point you’ll consider multithreading. Powershell has this capability baked right into it using Powershell Jobs, but .Net has a way too, and initial testing shows it might be faster! Read on to see what I mean.

Continue reading

February 11, 2013 Posted by | PowerShell, Powershell - Performance | , , , , , , | 29 Comments

Building PSObject Performance

You know I love working with Objects in Powershell, but are some methods better at building them then others?  Ran into an interesting technique recently and wanted to test it against my normal way of doing things.  Read on to see which technique is faster

Continue reading

January 14, 2013 Posted by | Powershell - Performance | , , | 6 Comments

To Redirect or Not

Recently I was working with someone on Spiceworks and he kept insisting on using redirection to fill a log file.  This seemed messy with me, so I decided to test his code versus what I like to do.  Read on to see the results.

Continue reading

January 10, 2013 Posted by | Powershell - Performance | , | 1 Comment

Remove Empty Directories Recursively

An interesting problem came up at Spiceworks the other week, and it was all about deleting empty directories.  Locating empty folders and removing them is actually pretty easy but the complication comes when you have nested folders, all of which are empty.  The most obvious scripting method doesn’t work in that regard.  Let’s see how I accomplished this task.

Continue reading

January 7, 2013 Posted by | PowerShell, Powershell - Performance | , , , , , , | Leave a comment

ForEach Performance

It’s been a tough couple of weeks, let me tell you!  I had a cold, that eventually dropped into my lungs and became pneumonia.  I don’t know about you, but I don’t have much interest in doing anything when I’m sick, not even Powershell!  Also, at work, we’re ramping up to migrate from our current ERP system to one from SAP and I expect that will be eating up a ton of time too.  Not to mention the need to finish my Exchange 2010 migration, create a Sharepoint 2010 test environment from our production one and half a dozen other projects going on at the same time.  I hope to keep fitting my scripts in amongst all this, as I have to admit this is where my IT passion is right now.

Continue reading

December 3, 2012 Posted by | Powershell - Performance, Random Thoughts | , , | 2 Comments