Public/Reset-Password.ps1

Function Reset-Password {
    <#
        .Synopsis
            Reset password for ActiveDirectory users
        .Description
            With this function you can reset the password and force a password change for a useraccount
        .Parameter Identity
            Accountname for the account
        .Parameter NewPassword
            Change the password for the account
        .Parameter ChangePasswordAtLogon
            Force password change for the account
        .Example
            Reset-Password -Identity l.ocked -unlock
            Unlocks the user account l.ocked
        .LINK
            about_functions_advanced
        .LINK
            about_CommonParameters
    #>

    [CmdletBinding(
        ConfirmImpact = 'Medium',
        PositionalBinding = $false,
        SupportsShouldProcess = $true,
        DefaultParameterSetName = 'NewPassword'
    )]
    [OutputType('None')]
    Param (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = 'Provide the accountname'
        )]
        [ValidateNotNullOrEmpty()]
        [string]$Identity,
        [Parameter(
            ParameterSetName = 'NewPassword',
            HelpMessage = 'Provide the new password'
        )]
        [securestring]$NewPassword,
        [Parameter(ParameterSetName = 'ChangePasswordAtLogon')]
        [switch]$ChangePasswordAtLogon
    )

    If ($PSBoundParameters.ContainsKey('NewPassword')) {
        If ($PSCmdlet.ShouldProcess("ActiveDirectory", "Change password of $identity")) {
            Try {
                # $Pass = Read-Host "Enter the new password" -AsSecureString
                Set-ADAccountPassword @PSBoundParameters
                Write-Verbose "New password for $identity set"
            } Catch {
                Write-Warning $_.Exception.Message
                Break
            }
        }
    }
    If ($PSBoundParameters.ContainsKey('ChangePasswordAtLogon')) {
        If ($PSCmdlet.ShouldProcess("ActiveDirectory", "Force ChangePasswordAtLogon for $identity")) {
            Try {
                Set-ADUser @PSBoundParameters
                Write-Verbose "$identity must change password at logon"
            } Catch {
                Write-Warning $_.Exception.Message
                Break
            }
        }
    }
}
Set-Alias -Name Change-Password -Value Reset-Password -Description "Reset Password" -Option ReadOnly -PassThru -ErrorAction SilentlyContinue