Using the Get-AdUser cmdlet in PowerShell, you can get aduser object GUID. Active Directory user has ObjectGUID property as the default set of properties on the aduser.
GUID is a globally unique identifier created by the Windows OS to identify user accounts, software, or any hardware components.
In this article, we will discuss how to get aduser object GUID in PowerShell using the Get-AdUser cmdlet.
Get AdUser Object GUID in PowerShell
Use the Get-AdUser to get user GUID using the property ObjectGUID.
Get-AdUser -Identity Toms | Select ObjectGUID
In the above PowerShell script, the Get-AdUser uses the Identity
parameter to specify the aduser name and pipe the output to the Select command to get aduser GUID using the ObjectGUID property.
The output of the above PowerShell script to get user object guid is:
ObjectGuid property returns the 32 hexadecimal digits guid of the user.
Cool Tip: How to find ad object by Guid in PowerShell!
PowerShell Get GUID for Current User
To get the current user GUID in PowerShell, we will use environment variable $env:USERNAME to get the current user and use the ADSISearcher type accelerator to search for the user.
Refer to the following code to get the current user GUID in PowerShell.
[guid]::New(([adsisearcher]"SamAccountName=$env:USERNAME").FindOne().Properties.objectguid[0]).Guid
In the above PowerShell script, we have used the ADSISearcher filter to get the current user guid.
It Checks that the samaccountname in the active directory is equal to the current user retrieved using the environment variable.
The output of the above PowerShell script to get the current user GUID is:
PS C:\> [guid]::New(([adsisearcher]"SamAccountName=$env:USERNAME").FindOne().Properties.objectguid[0]).Guid
cf53add1-6119-4950-b195-080a99e565a9
PS C:\>
Cool Tip: How to convert guid to string in PowerShell!
Conclusion
I hope the above article on how to get the object guid of users in PowerShell using the Get-AdUser cmdlet is helpful to you.
ADSISearcher type accelerator can be used to search for the user and get guid for the user in PowerShell.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.