Remove-ADObject : Server parameter not interpreted

Julien 1 Reputation point
2022-10-31T11:03:58.09+00:00

Hello,

I'm making a script in order to clean old AD object from my domain.
The script is targetting object from another domain.
So I add the "-Server" parameter to "Remove-ADObject" command in order to target the right domain but it seems to be not interpreted in the script and the command fails, saying that object not found.

In the script, the command is the following (par of a loop) :
Remove-ADObject -Server $domainname -Identity $dn -Confirm:$false
$domainname and $dn are of course defined before
However, the error displayed (like server is not read) :
+ Remove-ADObject -Identity $dn -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (CN=XXXX,OU=XXXX,DC=XXX,DC=net:ADObject) [Remove-ADObject], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.RemoveADObject

Any idée why is it now interpreted ?

Thanks !

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-10-31T14:31:17.297+00:00

    Are you sure the value found in the variable "$dn" actually exists in the AD?

    Why does the error show the Remove-ADObject cmdlet without the "-Server $domainname" parameter?

    Remove-ADObject -Identity $dn -Confirm:$false

    Did you remember to save the script after adding the "-Server" parameter? Or perhaps saved the script using a different name or directory?

    0 comments No comments

  2. Julien 1 Reputation point
    2022-10-31T15:50:23.593+00:00

    Hello,

    Thank you for your answer.
    Everything is fine on that. No issue in saving and version of file used. :)

    However, I discovered one thing. Server parameter is used if I remove another parameter. For exemple, if I replace -Confirm by -Server, the command works (except that I've to manually confirm the action).

    It seems that there is a limitation in number of parameter that the command can use !

    Any clue on that ?
    Thanks !

    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2022-10-31T18:31:41.3+00:00

    There are no limitations on the number of parameters that you can use. Nor should there be a limit on the length of line on which those parameters are used.

    Try running the cmdlet like this:

    $par = @{  
        Identity = $dn  
        Confirm = $false  
        Server  = $domainname  
    }  
    Remove-ADObject @par
    
    0 comments No comments

  4. Zain Choudry 6 Reputation points
    2022-10-31T20:02:47.17+00:00

    Can you try this please?

    Get-ADObject -Identity $dn -Server $domainname | Remove-ADObject -Confirm:$false

    0 comments No comments

  5. Anonymous
    2022-11-01T05:19:14.157+00:00

    Hi,

    Did you run the script in PowerShell or PowerShell ISE? Can you try starting PowerShell without loading profiles?

    powershell.exe -noprofile  
    

    Best Regards,
    Ian Xue

    -----------------------------

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.