I received a comment on a post I did some time ago where the visitor asked for how to remove a User and Device relationship with PowerShell, instead of creating one. I thought that I’d put together a quick script to handle such scenarios, and here it is.

Script

<#
.SYNOPSIS
    Remove an User and Device relationship in ConfigMgr 2012
.DESCRIPTION
    This script will remove a relationship between an user and a device in ConfigMgr 2012. It supports an array of device names to be specified. 
.PARAMETER SiteServer    
    Site server name with SMS Provider installed
.PARAMETER DeviceName    
    Specify the device names that has a user machine relationship to be removed
.EXAMPLE    
    .\Remove-PrimaryUserDeviceRelationship.ps1 -SiteServer CM01 -DeviceName CL01    
    Removes a relationship between a device called 'CL01' and the primary user on a Primary Site server called 'CM01':
.NOTES    
    Script name: Remove-PrimaryUserDeviceRelationship.ps1    
    Author:      Nickolaj Andersen    
    Contact:     @NickolajA    
    DateCreated: 2015-04-20
#>
[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [parameter(Mandatory=$true, HelpMessage="Site server where the SMS Provider is installed")]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({Test-Connection -ComputerName $_ -Count 1 -Quiet})]
    [string]$SiteServer,
    [parameter(Mandatory=$true, HelpMessage="Specify a Device names that has a user machine relationship that will be removed")]
    [ValidateNotNullOrEmpty()]
    [string[]]$DeviceName
)
Begin {
    # Determine SiteCode from WMI
    try {
        Write-Verbose "Determining SiteCode for Site Server: '$($SiteServer)'"
        $SiteCodeObjects = Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation -ComputerName $SiteServer -ErrorAction Stop
        foreach ($SiteCodeObject in $SiteCodeObjects) {
            if ($SiteCodeObject.ProviderForLocalSite -eq $true) {
                $SiteCode = $SiteCodeObject.SiteCode
                Write-Debug "SiteCode: $($SiteCode)"
            }
        }
    }
    catch [System.UnauthorizedAccessException] {
        Write-Warning -Message "Access denied" ; break
    }
    catch [Exception] {
        Write-Warning -Message "Unable to determine SiteCode" ; break
    }
}
Process {
    try {
        foreach ($Device in $DeviceName) {
            $UserMachineRelations = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_UserMachineRelationship -ComputerName $SiteServer -Filter "ResourceName like '$($Device)'"
            if ($UserMachineRelations -ne $null) {
                if ($PSCmdlet.ShouldProcess($UserMachineRelations.__PATH, "Remove")) {
                    Remove-WmiObject -InputObject $UserMachineRelations
                }
            }
        }
    }
    catch [System.UnauthorizedAccessException] {
        Write-Warning -Message "Access denied"
    }
    catch [Exception] {
        Write-Error -Message "An error occured while trying to remove a WMI instance"
    }
}

Using the script

The script supports multiple device names to be specified in order to be more efficient when running the script.

1. Save the script above as Remove-PrimaryUserDeviceRelationship.ps1 to e.g. C:\Scripts.
2. Open an elevated PowerShell console and browse to C:\Scripts.
3. Run the following command:

.\Remove-PrimaryUserDeviceRelationship.ps1 -SiteServer CAS01 -DeviceName WS01 -Verbose

141_1_1

The relationship between the Primary User for Device WS01 has now been removed, I hope this helps!

Nickolaj Andersen
Principal Consultant and Enterprise Mobility MVP. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows deployments and Automation. In 2015 Nickolaj was awarded as PowerShell Hero by the community for his script and tools contributions. Author of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService and a frequent speaker at user groups.

(99)

comments
  • David
    Posted at 14:30 December 10, 2015
    David
    Reply
    Author

    I’m attempting to run the script using multiple machine names yet it fails after the first machine.

    Any way to have the script remove multiple machines?

    Thanks!

  • Leave a Reply