r/PowerShell • • Jul 05 '17

Supercharge your PowerShell scripts with these five easy tips.

I am writing this from my phone, so Google is your friend here when looking for examples on how to quickly implement these. If enough folks are drawing a blank I will update this post with examples for each.

1: Replace Get-Content and Import-CSV with .NET's StreamReader.

Examples:

https://www.google.com/amp/s/amp.reddit.com/r/PowerShell/comments/2ey9fk/using_ps_to_search_a_huge_file_for_matching_lines/

http://www.happysysadm.com/2014/10/reading-large-text-files-with-powershell.html?m=1

2: Replace Out-File and Export-CSV with .NET's StreamWriter.

Examples:

https://stackoverflow.com/questions/13855788/powershell-fastest-way-to-write-large-body-of-text-to-a-file

3: Replace Compare-Object with the Match operator using the GetHashCode() method for strings being compared, or for a RegEx generated at runtime.

Examples:

https://blogs.technet.microsoft.com/heyscriptingguy/2011/02/18/speed-up-array-comparisons-in-powershell-with-a-runtime-regex/

https://blogs.technet.microsoft.com/heyscriptingguy/2015/04/01/use-powershell-to-compare-two-files/

4: Replace Get-AD* with ADFind from JoeWare.NET.

Examples:

http://powershell-guru.com/fastest-powershell-1-count-all-users-active-directory/

5: Replace Set-AD* with ADMod from JoeWare.NET.

EDIT: Yay, downvotes because knowledge is power and power is bad.

2 Upvotes

13 comments sorted by

View all comments

3

u/replicaJunction Jul 06 '17

"PowerShell isn't about optimizing computer resources. PowerShell is about optimizing human resources." -J. Snover*

In many cases, the time saved using these external calls and methods is offset by the time taken to research and implement them in a script. PowerShell shines because it's easy to write, once you've wrapped your mind around some of the basic syntax and concepts.

I don't disagree that any of these tips make things faster. I've messed with StreamReader and StreamWriter myself and know firsthand that they're leaps and bounds faster. Where I disagree is that they're easy - for the introductory PowerShell user, .NET calls can be intimidating, especially working with a third-party library like JoeWare.NET. Even experienced developers need to get used to PowerShell's syntax for creating objects and referencing type names.

Your point is certainly valid for high-performance situations, but I wouldn't suggest people turn to these first.

On the other hand, if you believe in them strongly enough, write a module for them! Encapsulate the hard stuff into something that feels like native PowerShell and works similarly to tools people have come to expect. If you remove the barrier of entry, there's no reason not to use the better-performing option.

All of this is just my two cents. At the end of the day, write what PowerShell you will, so long as it gets the job done. :)

*Note: I'm positive I heard him say this because I remember it being so insightful, and I suspect it was in a live Web conference, but for the life of me I can't find the source. If anyone else knows this quote and can point me to a source for it, I'd be very grateful.

0

u/[deleted] Jul 06 '17

In many cases, the time saved using these external calls and methods is offset by the time taken to research and implement them in a script.

Cool. Arbitrary, irrelevant, vacuous, but sure, we'll call it cool.

I don't disagree that any of these tips make things faster.

Cool.

Your point is certainly valid for high-performance situations, but I wouldn't suggest people turn to these first.

Are strawmen on sale at Walmart or something?

On the other hand, if you believe in them strongly enough, write a module for them! Encapsulate the hard stuff into something that feels like native PowerShell and works similarly to tools people have come to expect. If you remove the barrier of entry, there's no reason not to use the better-performing option.

Working on it.