Usename to machine name script

I am in a domain environment. I have MECM in my environment. I want a script when i type in the username, it will give me the machine name from MECM. Is this possible?

Yes this is possible:

Get-CMUserDeviceAffinity (ConfigurationManager) - Configuration Manager | Microsoft Learn

There is a module manifest for ConfigurationManager somewhere on your computer, likely in Program Files (x86). You have to do an Import-Module to the full path of that module manifest, e.g.

Import-Module "C:\Program Files (x86)\MECM Console\bin\ConfigurationManager\ConfigurationManager.psd1"

Then your “worksite” will be added as a drive on your computer. Use Get-PSDrive to identify it. The provider will be listed as “CMSite” so it should stand out. Then you have to change directories to that drive, e.g.

Push-Location CTS:

Now you can use the Get-CMUserDeviceAffinity cmdlet to retrieve a list of computer devices associated with a user, e.g.

Get-CMUserDeviceAffinity -UserName contoso\jsmith

with the output looking something like this

SmsProviderObjectPath  : SMS_UserMachineRelationship.RelationshipResourceID=25182183
CreationTime           : 5/25/2025 8:15:36 PM
IsActive               : True
RelationshipResourceID : 25182183
ResourceClientType     : 1
ResourceID             : 16786705
ResourceName           : len-desk-w11
Sources                : {4, 7, 9}
Types                  : {1}
UniqueUserName         : contoso\jsmith

SmsProviderObjectPath  : SMS_UserMachineRelationship.RelationshipResourceID=25185173
CreationTime           : 2/7/2024 10:56:29 AM
IsActive               : True
RelationshipResourceID : 25185173
ResourceClientType     : 1
ResourceID             : 16787870
ResourceName           : tp-lap-w11
Sources                : {7, 9}
Types                  : {1}
UniqueUserName         : contoso\jsmith

That should be everything you need to get started.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.