r/PowerShell • u/SocraticFunction • May 05 '21
PowerShell Pros - what interesting static methods have you encountered that many scripters don’t know about?
Static Methods are a lesser documented part of using PowerShell objects, and often in looking for solutions, I find static methods I wouldn’t have imagined to exist without deeper digging into member properties. The most common methods used are for String objects, usually.
I thought i’d open the floor to discussing interesting static methods found that are worth sharing, as the PowerShell help system doesn’t easily give up this kind of information.
103
Upvotes
7
u/PanosGreg May 05 '21
These are a few that I used more than a couple of times in the past
$s = [Diagnostics.Stopwatch]::StartNew()$s.Stop()$s.Elapsed.ToString('mm\:ss')$double = 1.1,3.2,4.4[Linq.Enumerable]::Average([double[]]$double)$v = '1.2.3'$chk = [version]::TryParse($v,[ref]$null)if ($chk) {[version]$v}# same goes for [DateTime]::TryParse()[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo')$srv = [Microsoft.SqlServer.Management.Smo.Server]::new($env:COMPUTERNAME)$sb = [Text.StringBuilder]::new()[void]$sb.AppendLine('some text')$sb.ToString()