1

Trying to change a remote Windows Password using explicit credentials with PowerShell.

I've been able to make this work using the ADSI interface, but the script is running from a non-elevated account, and therefore does not have permissions. I need to ask the user for an elevated account, and then use that account when making the connection.

This works:

$server = "Server1"
$adminID = "Administrator"
$Password = "NewPassword"
$OldPassword = "OldPassword"

([ADSI] "WinNT://$server/$adminID").SetPassword($Password)

But, I need to also include the old password for the account, so that I have permissions to make the change.

Is there a way to make the connection to the server as $AdminD with the $OldPassword, and then change it to $NewPassword?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
J. DuBois
  • 41
  • 1
  • 10

1 Answers1

0

Found a solution.

        $credential = New-Object System.Management.Automation.PSCredential($($server + "\" +$AdminID),$Oldpassword)

        $pth = "\\$($server)\admin$"

        write-host "Making connection to server with creds." -nonewline

            # test for the drive, and remove if present.    

        if (![bool]([string]::IsNullOrEmpty($(get-psdrive x -ErrorAction SilentlyContinue).root)))
        {
            remove-PSDrive -Name X
            write-host "Drive unmapped," -nonewline
        }

        New-PSDrive -Name X -PSProvider filesystem  -Root $pth -Credential $credential | out-null
        write-host " PS Drive established.. " -nonewline


        ([ADSI] "WinNT://$server/$adminID").SetPassword($Password)


By making a connection to the server first, the second call to reset the password uses the same credentials that are already established.

When done, I call the following to disconnect the drive..

    # test for the drive, and remove if present.    
    if (![bool]([string]::IsNullOrEmpty($(get-psdrive x -ErrorAction SilentlyContinue).root)))
    {
        remove-PSDrive -Name X
        write-host "Drive unmapped."
    }

If there is a better way, I'd love to know what it is.. But this at least seemed to work.

J. DuBois
  • 41
  • 1
  • 10