Sort-Member

August 26, 2008

When you work in the PowerShell console, exploring objects by piping them to Format-* cmdlets, outputted properties come out unsorted. One example is the type accelerator:  PS > "" | format-list * objectClass : {top, domain, domainDNS} distinguishedName : {DC=domain,DC=com} instanceType : {5} whenCreated ...
no comments

Randomizing collections: one-liners

August 11, 2008

I found a cool way HERE to shuffle/reorder array elements without using the random class. Generally you provide Sort-Object an object property to sort on, only now the property is in the form of a Globally Globally Unique Identifier (GUID), which is a random generated 128-bit integer.   PS > 1..5 | sort-object { ::newGuid() } 2 5 3 1 4   To select one or more random objects from the collection, pipe the above to Select-Object and use the -First (or -Last) parameter:PS > 1..5 | sort-object { ::newGuid() } | select-object -first 3 4 1 3   These techniques are also available on collections you use everyday with...
5 comments