advapi32/ConvertSidToStringSid.ps1

function ConvertSidToStringSid
{
    <#
    .SYNOPSIS
 
    The ConvertSidToStringSid function converts a security identifier (SID) to a string format suitable for display, storage, or transmission.
 
    .DESCRIPTION
 
    The ConvertSidToStringSid function uses the standard S-R-I-S-S… format for SID strings.
     
    .PARAMETER SidPointer
 
    A pointer to the SID structure to be converted.
 
    .NOTES
 
    Author: Jared Atkinson (@jaredcatkinson)
    License: BSD 3-Clause
    Required Dependencies: None
    Optional Dependencies: None
     
    (func advapi32 ConvertSidToStringSid ([bool]) @(
        [IntPtr] #_In_ PSID Sid,
        [IntPtr].MakeByRefType() #_Out_ LPTSTR *StringSid
    ) -EntryPoint ConvertSidToStringSid -SetLastError)
 
    .LINK
 
    https://msdn.microsoft.com/en-us/library/windows/desktop/aa376399(v=vs.85).aspx
 
    .EXAMPLE
    #>


    param
    (
        [Parameter(Mandatory = $true)]
        [IntPtr]
        $SidPointer    
    )
    
    $StringPtr = [IntPtr]::Zero
    $Success = $Advapi32::ConvertSidToStringSid($SidPointer, [ref]$StringPtr); $LastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error()

    if(-not $Success) 
    {
        Write-Debug "ConvertSidToStringSid Error: $(([ComponentModel.Win32Exception] $LastError).Message)"
    }
    
    Write-Output ([System.Runtime.InteropServices.Marshal]::PtrToStringAuto($StringPtr))
}