Functions/Security/Get-CdsUserBusinessUnit.ps1

<#
    .SYNOPSIS
    Retrieve user business unit
#>

function Get-CdsUserBusinessUnit {
    [CmdletBinding()]    
    [OutputType("Microsoft.Xrm.Sdk.Query.QueryExpression")]
    param
    (        
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [Guid]
        $UserId,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [String[]]
        $Columns = @("*")
    )
    begin {
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);       
    }    
    process {
       $user = Get-CdsRecord -Logicalname "systemuser" -Id $UserId -Columns "businessunitid";
       $businessUnit = Get-CdsRecord -Logicalname "businessunit" -Id $user.businessunitid_Value.Id -Columns $Columns;
       $businessUnit;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}
Export-ModuleMember -Function Get-CdsUserBusinessUnit -Alias *;