Skip to main content

Posts

Showing posts from September, 2017

Useful Powershell stuff

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
$idsString = $ids -replace "(\w+)",'''$1'''  -join ','
or if it's one large string separated by line breaks (e.g. copied from a spreadsheet):
$idsString = ($ids -replace "(\w+)",'''$1''').Split("…