Skip to main content

Posts

Showing posts from September, 2017

Useful Powershell stuff

Group objects into batches of X items with a constructed property, credit to Dave Wyatt:

$bigList = 1..1000
$counter = [pscustomobject] @{ Value = 0 }
$groupSize = 100
$groups = $bigList | Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) }

-----
Use "PipelineVariable" to pass data down the pipeline rather than using a foreach:

Get-ADUser <username> -PipelineVariable user -Properties memberof |
    Select-Object -ExpandProperty memberof |
        Select-Object @{ n = 'Name'; e = { $user.Name }}, @{ n = 'MemberOf' ; e = { $PSItem -replace 'CN=|,(OU|CN)=.+' }}

Also if a cmdlet does not support -pipelineVariable you can pass it to Where-Object
Get-Mailbox -ResultSize Unlimited | Where-Object {$true} -PipelineVariable mbx | whatever
-----
Convert a list of strings into a single line string delimited by quotes and separated by commas, suitable for using in a "IN" statement in SQL/SOQL

$ids is a list of ID strings
$idsStr…