Home » PowerShell » Get AdUser GUID in PowerShell

Get AdUser GUID in PowerShell

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:

Get AdUser GUID
Get AdUser GUID in PowerShell

Get Current User GUID in PowerShell

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:\>

Conclusion

I hope the above article on how to get current user GUID in PowerShell using the Get-AdUser cmdlet is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment